Presentation is loading. Please wait.

Presentation is loading. Please wait.

Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH.

Similar presentations


Presentation on theme: "Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH."— Presentation transcript:

1 Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH

2 2 Balanced Binary Search Trees height is O(log n), where n is the number of elements in the tree AVL (Adelson-Velsky and Landis) trees red-black trees get, put, and remove take O(log n) time

3 3 Balanced Binary Search Trees Indexed AVL trees Indexed red-black trees Indexed operations also take O(log n) time

4 4 Balanced Search Trees weight balanced binary search trees 2-3 & 2-3-4 trees AA trees B-trees BBST etc.

5 5 5 AVL Tree Definition Binary tree. If T is a nonempty binary tree with T L and T R as its left and right subtrees, then T is an AVL tree iff 1. T L and T R are AVL trees, and 2. |h L – h R |  1 where h L and h R are the heights of T L and T R, respectively

6 6 Balance Factors 00 0 0 1 0 0 1 0 1 This is an AVL tree.

7 7 Height The height of an AVL tree that has n nodes is at most 1.44 log 2 (n+2). N h =N h-1 + N h-2 +1 – reminds you anything? F n =F n-1 + F n-2 N h =F h+2 -1 when

8 8 AVL Search Tree 00 0 0 1 0 0 1 0 1 10 7 83 1 5 30 40 20 25 35 45 60

9 9 put(9) 00 0 0 1 0 0 1 0 1 9 0 0 10 7 83 1 5 30 40 20 25 35 45 60

10 10 put(29) 00 0 0 1 0 0 1 0 1 10 7 83 1 5 30 40 20 25 35 45 60 29 0 -2 RR imbalance => new node is in right subtree of right subtree of blue node (node with bf = -2)

11 11 put(29) 00 0 0 1 0 0 1 0 1 10 7 83 1 5 30 40 25 35 45 60 0 RR rotation. 20 0 29

12 12 AVL Rotations RR LL RL LR

13 13 Imbalance Types After an insertion, when the balance factor of node A is –2 or 2, the node A is one of the following four imbalance types 1. LL: new node is in the left subtree of the left subtree of A 2. LR: new node is in the right subtree of the left subtree of A 3. RR: new node is in the right subtree of the right subtree of A 4. RL: new node is in the left subtree of the right subtree of A

14 14 Rotation Definition To switch children and parents among two or three adjacent nodes to restore balance of a tree. A rotation may change the depth of some nodes, but does not change their relative ordering.

15 15 AVL Rotations To balance an unbalanced AVL tree (after an insertion), we may need to perform one of the following rotations: LL, RR, LR, RL

16 16 An LL Rotation

17 17 An LR Rotation

18 18 RR and RL? Symmetric!

19 19 Inserting into an AVL Search Tree 29 Insert(29) 1 0 00 0 1 1 0 0 0 10 40 3045 2035 25 60 7 38 15 Where is 29 going to be inserted into? After the insertion, is the tree still an AVL search tree? (i.e., still balanced?)

20 20 Inserting into an AVL Search Tree What are the balance factors for 20, 25, 29? -2 0 Insert(29) 29 1 0 00 0 1 1 0 0 10 40 3045 2035 25 60 7 38 15 RR imbalance  new node is in the right subtree of right subtree of node 20 (node with bf = -2)

21 21 After RR Rotation 1 0 00 0 1 1 0 0 10 40 3045 3560 7 38 15 0 0 0 25 2029 What would the left subtree of 30 look like after an RR rotation? After the RR rotation, is the resulting tree an AVL search tree now?

22 22 Deletion from an AVL Search Tree Deletion of a node may also produce an imbalance Imbalance incurred by deletion is classified into the types R0, R1, R-1, L0, L1, and L-1 Rotation is also needed for rebalancing Read the observations after deleting a node from an AVL search tree Read Section 11.2.6 for Deletion from an AVL search tree

23 23 An R0 Rotation

24 24 An R1 Rotation

25 25 An R-1 Rotation

26 26 Complexity of Dictionary Operations: Search, Insert, and Delete Data StructureWorst CaseExpected Hash Table Binary Search Tree Balanced Binary Search Tree O(n) O(log n) O(1) O(log n) n is the number of elements in dictionary.

27 27 Complexity of Other Operations Ascend, Search(rank), and Delete(rank) Data Structureascendget and remove Hash Table Indexed BST Indexed Balanced BST O(D + n log n) O(n) O(D + n log n) O(n) O(log n) D is the number of buckets.

28 28 Red Black Trees Colored Nodes Definition Binary search tree. Each node is colored red or black. Root and all external nodes are black. No root-to-external-node path has two consecutive red nodes. All root-to-external-node paths have the same number of black nodes

29 29 Example Red Black Tree 10 7 8 1 5 30 40 20 25 35 45 60 3

30 30 Properties The height of a red black tree that has n (internal) nodes is between log 2 (n+1) and 2log 2 (n+1).

31 31 Lemma 16.2 r : rank of root

32 32 Insert New pair is placed in a new node, which is inserted into the red-black tree. New node color options. – Black node => one root-to-external-node path has an extra black node (black pointer). Hard to remedy. – Red node => one root-to-external-node path may have two consecutive red nodes (pointers). May be remedied by color flips and/or a rotation.

33 33 Classification Of 2 Red Nodes/Pointers XYz – X => relationship between gp and pp. pp left child of gp => X = L. – Y => relationship between pp and p. p right child of pp => Y = R. – z = b (black) if d = null or a black node. – z = r (red) if d is a red node. ab c d gp pp p

34 34 XYr Color flip. ab c d gp pp p ab c d gp pp p Continue rebalancing if changing gp to red causes problems

35 35 LLb Rotate. Done! Same as LL rotation of AVL tree. y x ab z cd ab c d gp pp p x y z

36 36 LRb Rotate. Done! Same as LR rotation of AVL tree. RRb and RLb are symmetric. y x ab z cd bc a d gp pp p y x z

37 37 Red-black Tree Animation http://www.ece.uc.edu/~franco/C321/html/RedBlac k/redblack.html http://www.ece.uc.edu/~franco/C321/html/RedBlac k/redblack.html java.util.TreeMap => red black tree

38 38 Coming Up Next READING: Ch 16.1~2 NEXT: Graphs (Ch 17.1~7)


Download ppt "Balanced Binary Search Tree 황승원 Fall 2010 CSE, POSTECH."

Similar presentations


Ads by Google