Advanced Data Structures and Implementation

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

猜你喜欢

转载自blog.csdn.net/lilele12211104/article/details/78783887