Presentation is loading. Please wait.

Presentation is loading. Please wait.

Balanced Trees Balanced trees have height O(lg n).

Similar presentations


Presentation on theme: "Balanced Trees Balanced trees have height O(lg n)."— Presentation transcript:

1 Balanced Trees Balanced trees have height O(lg n).
Height-balanced trees At each node, height of left and right subtrees are “close”. e.g. AVL trees, B-trees, red-black trees, splay trees Weight-balanced trees At each node, number of nodes in left and right subtrees are “close”. Search, Predecessor, Successor, Minimum, Maximum O(lg n) time. Insert and Delete may have to rebalance the tree.

2 Rebalancing Heuristic -- Rotation
Right-Rotate(T, y) y x y C C Left-Rotate(T, x) x A A B B Preserves the inorder key sequence. Rotation Takes O(1) time. To be used by insertion and deletion on a red-black tree.

3 An Example of Rotation 7 6 2 3 4 x y 7 6 2 3 4 11 9 9 18 19 20 22 12
17 14 19 20 22 12 17 14 Left-Rotate(x) 18 11 y x

4 Red-Black Trees A “balanced” binary search tree with height (lg n).
Basic dynamic-set operations: Search, Predecessor, Successor, Minimum Maximum, Insert, Delete all take O(lg n) time.

5 Node of a Red-Black Tree
color key left right parent NIL as pointers to external nodes (leaves) of the tree. Key-bearing nodes as internal nodes of the tree.

6 Red-Black Properties 12 internal node 8 14 NILl 4 9 15 NILl NILl NILl NILl NILl 5 external node (requiring no extra storage) NILl NILl 1. Every node is either red or black. No path is more than twice as long as any other. 2. The root is black. 3. Every leaf (NIL) is black. 4. If a node is red, then both of its children are black. 5. Every simple path from the root to a descendant leaf contains the same number of black nodes.

7 Sentinel (Save Storage)
parent 12 8 14 4 9 15 5 nil(T)

8 All Nodes Are Black A complete binary tree! 12 8 14 4 9 13 20
NILl NILl NILl NILl NILl NILl NILl NILl Red nodes may be seen as “fill-ins” to a complete binary search tree.

9 Black-Height The black-height of a node x, denoted bh(x), is the number of black nodes on any path from x (excluded) to a leaf. bh = 3 17 2 1 bh = 2 14 21 10 23 19 16 7 20 NILl NILl 12 15 NILl NILl 3 NILl NILl NILl NILl NILl NILl NILl A node at height h has black-height ≥ h/2. NILl NILl

10 #Internal Nodes vs Black Height
Claim Subtree rooted at node x contains  2 – 1 internal nodes. bh(x) Proof By induction on the height h of x. h = 0 xx 2 – 1 = 0 internal node. h > 0 x x or ≥bh(x)–1 bh(x)–1 black height height ≤ h – 1 ≥ 2 – 1 bh(x)–1 ≥ 2 – 1 #internal nodes (by induction) ≥ 2 – 1 2 – – = 2 – 1 bh(x) bh(x)–1 internal node total:

11 #Internal Nodes vs Height
Lemma A red-black tree with n internal nodes has height at most 2lg (n+1). Proof The root of a red-black tree of height h has black-height  h/2. This is because on any path down from the root a red node is always followed by a black node (but not necessarily vice versa). h/2 By the claim there are at least – 1 internal nodes in the tree. Therefore n  – 1 and h  2 lg(n+1). Corollary Search, Minimum, Maximum, Successor, and Predecessor can be done in O(lg n) time.


Download ppt "Balanced Trees Balanced trees have height O(lg n)."

Similar presentations


Ads by Google