1 / 11

Balanced Trees

Height-balanced trees. Weight-balanced trees. Search , Predecessor , Successor , Minimum , Maximum. Insert and Delete may have to rebalance the tree. Balanced Trees. Balanced trees have height O (lg n ). At each node, height of left and right subtrees are “close”.

platt
Download Presentation

Balanced Trees

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


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

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

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

  4. Red-Black Trees A“balanced” binary search treewith 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 toexternal nodes(leaves) of the tree. Key-bearing nodes asinternal nodesof 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 eitherredorblack. No path is more than twice as long as any other. 2. The root isblack. 3. Every leaf (NIL) isblack. 4. If a node isred, then both of its children areblack. 5. Every simple path from the root to a descendant leaf contains the same number ofblacknodes.

  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. 2 2 1 1 1 1 1 1 1 1 Black-Height Theblack-heightof a node x, denoted bh(x), is the number of black nodes on any path from x (excluded) to a leaf. bh = 3 17 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. Claim bh(x) Subtree rooted at node x contains  2 – 1 internal nodes. 0 2 – 1 = 0 internal node. xx x or x black height ≥bh(x)–1 ≥bh(x)–1 bh(x)–1 bh(x)–1 height ≤h –1 #internal nodes (by induction) bh(x)–1 bh(x)–1 bh(x)–1 bh(x)–1 ≥ 2 – 1 ≥ 2 – 1 ≥ 2 – 1 ≥ 2 – 1 bh(x)–1 bh(x)–1 bh(x) ≥ internal node total: 2 – 1 + 2 – 1 + 1 = 2 – 1 #Internal Nodes vs Black Height Proof By induction on the height h of x. h = 0 h> 0

  11. h/2 By the claim there are at least 2 – 1 internal nodes in the tree. Thereforen 2 – 1 and h 2 lg(n+1). h/2 Corollary Search, Minimum, Maximum, Successor, andPredecessor can be done in O(lg n) time. #Internal Nodes vs Height Lemma A red-blacktree with n internal nodes has height at most 2lg (n+1). Proof The root of ared-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).

More Related