Tree

来源:互联网 发布:金蝶软件 编辑:程序博客网 时间:2024/06/14 01:47

A property of a binary tree is important is that the depth of an average binary tree is considerably smaller than N. Actually, the average depth is O(n). And for the the binary search tree, the average value of the depth is O(logN)

4.2.2 An Example: Expression Trees

4.3 The Search Tree ADT–Binary Search Trees

4.3.4 remove

The hardest operation is deletion.
1. If the node is a leaf, it can be deleted immediately.
2. If the node has one child, the node can be deleted after its parent adjusts a link to bypass the node.
3. If the node has two children, the general strategy is to replace the data of this node with the smallest of the right subtree. If the number of deletions is expected to be small, then a popular strategy to use is lazy deletion. This is especially popular if duplicate items are present, because then the filed that keeps count of the frequency of appearance can be decremented. Also, if a deleted item is inserted, the overhead of allocating a new cell is avoided.

4.3.5 Average-Case Analysis

The average depth over all nodes in a tree is O(logN) on the assumption that all insertion sequences are equally likely.
The sum of the depths of all nodes in a tree is known as the internal path length.
For binary search trees,

D(N)=D(i)+D(Ni1)+N1
D(N)=2N[N1j=0]+N1=O(NlogN)

4.4 AVL Trees

An AVL (Adelson-Velskii and Landis) tree is a binary tree with a balance condition. The balance condition must be easy to maintain, and it ensures that the depth of the tree is O(logN).

Tables Are left-left single rotation right-right single rotation left-right double rotation right-left

4.5 Splay Trees

A splay tree, that guarantees that any M consecutive tree operations starting from an empty tree take at most O(MlogN)time.

The basic idea of the splay tree is that after a node is accessed, it is pushed to the root by a series of AVL tree rotations.

operation

Let X be a node on the access path at which we are rotating.
1. If the parent of X is the root of the tree, we merely rotate X and the root.
This is the last rotation along the access path.
2. If X has both a parent(P) and a grandparent(G), and there are two cases, plus symmetries. to consider. The first case is the zig-zag, we perform a double rotation, exactly like an AVL double rotation.
3. Otherwise, we have a zig-zag case:X and P are both left children or right children.
这里写图片描述

这里写图片描述
The difference is that after an access of the node with item 1, which takes N-1 units, the access on the node with item 2 will only take about N2units instead of N-2 units.
这里写图片描述
这里写图片描述
这里写图片描述
这里写图片描述

4.6 Tree Traversals

4.7 B-Trees

A B-tree of order M is an M-ary tree with the following properties.
1. The data items are stored at leaves.
2. The nonleaf store up to M - 1 keys to guide the searching; key i represents the smallest key in subtree i+1.
3. The root is either a leaf or has between two and M children.
4. All nonleaf nodes(except the root) has between M/2 and M children.
5. All leaves are at the same depth and had between L/2 and L data items.

这里写图片描述
这里写图片描述

An operation of insertion causes a split into two leaves an then a split of the parent node.
这里写图片描述

An operation of deletion
这里写图片描述
Since the leaf has only two items, and its neighbor is already at it minimum of three, we combine the items into a new leaf of five items. As a result, the parent has only two children. However, ti can adopt form a neighbor because the neighbor has four children. As a result, both have three children.

4.8 Sets and Maps in the Standard Library

Top-down red-black trees are used to implement TreeSet and TreeMap in Java whose contains operations in logarithmic worst-caser time.

原创粉丝点击