Lecture 10 Balanced Search Trees

来源:互联网 发布:知乎 哈布斯堡 编辑:程序博客网 时间:2024/04/30 05:53

Search tree data structure maintaining dynamic set of n elements using tree of height O(logn).

Examples:

    >AVL trees

    >2-3 trees

    >2-3-4 trees

    >B-trees

    >Red-black trees

    >Skip lists

    >Treaps


Red-black trees

BST data structure with extra color field for each node, satisfying:

>Red-black properties:

1. Every node is either red or black;

2. The root and leaves(nil's) are black;

3. Every red node has black parent;

4. All simple paths from a node x to a descendant leaf of x have same #black nodes=black-height(x)


Example:



Height of red black tree:





Corollary:

Queries (Search  Min  Max  Successor  Predecessor) run in O(log(n)) in a red-black tree.

Updates (Insert  Delete) must modify the tree.

    ---BST operation

    ---color changes

    ---restructruing of links via rotations  O(1) time operations


Rotation:



RB-insert(x):

Idea

--Tree-Insert(x)

--color node red

---problem: parent might be red. So violate 3

---move violation up the tree via recoloring until we can fix violation via rotation & recoloring.







RB-Insert adds x to set & preserves red-blackness in O(logn) time.

#Case1 <= logn  #Case2 & 3 <= 2   O(1) rotations

Case 2 and 3 means termination. What we mainly to do is case 1.


0 0
原创粉丝点击