您的位置:首页 > 其它

Advanced Data Structures and Implementation

2018-01-02 16:02 423 查看

12.1 Top-Down Splay Trees





An example of the top-down splaying algorithm is shown in Figure 12.4.





How to insert an item into a tree?

A new node is allocated(if necessary), and if the tree is empty, a one-node tree is created. Otherwise, we splay root around the inserted value x. If the data in the new root is equal to x, we have a duplicate; instead of reinserting x, we preserve newNode for a future insertion and return immediately. If the new root contains a value larger than x, then the new root and its right subtree become a right subtree of newNode, and the root’s left subtree becomes the left subtree of newNode.

The deletion in spray trees is easy, because a splay will place the target of the deletion at the root.

12.2 Red-Black Trees

Operations on red-black trees take O(logN) time in the worst case. And compared with AVL trees, a careful nonrecursive implementation can be done relatively effortlessly.

A red-blace tree is a binary search tree with the following coloring properties:

1. Every node is colored either red or black.

2. The root is black.

3. If a node is red, its children must be black.

4. Every path from a node to a null reference must contain the same number of black nodes.

A consequence of the coloring rules is that the height of a red-black tree is at most

2log(N+1).

12.2.1 Bottom-up Insertion

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法