270 likes | 410 Views
Binary Search Trees. data. compare. data. data. T. <. <. We can search the tree “ efficiently ”. log n. <. data. data. O(log 2 n). What is the real benefit of using BST?. Search 18, 2, 26. 20. 10. 30. 1. 17. 25. 35. 0. 5. 14. 19. 28. 34. 38. 23. 4. 7. 12. 15.
E N D
Binary Search Trees data compare data data T < < We can search the tree “efficiently” log n < data data ITK 179
O(log2n) What is the real benefit of using BST? Search 18, 2, 26 20 10 30 1 17 25 35 0 5 14 19 28 34 38 23 4 7 12 15 18 22 27 32 36 3 11 13 21 26 37 ITK 279
What is the benefit of using BST? The average height of a binary search tree of n nodes is O(log2n) if the n nodes are arrived based on a uniform distribution on the space of possible keys. This is as good as the binary search on a sorted array. Proof: not so difficult, not so trivial.. f(n) : The average internal path length of an n-node BST ITK 279
The average height of a BST : O(log2n) f(n) : The average internal path length of an n-node BST f(3) : (3+2+3) / 3 = 2.67 0 0 1 1 1 1 2 2 0+1+1=2 0+1+2=3 0+1+2=3 ITK 279
Analysis Average Heights on n Random Keys BST O(log2n) How bad is the constant? log2n + O(log2log2n) = 4.31101… Devroye and Reed,, SIAM J. Comput. ‘95 ITK 279
The sequence of node insertion will affect the shape of the BST 1 Highly unbalanced BST 7 12 1, 7, 12, 25, 27, 13, 23, 17, 15 25 13 27 This situation is not uncommon e.g., the data is roughly sorted. 23 17 15 ITK 179
Highly unbalanced BST 1 7 Balanced BST 12 17 25 13 27 13 25 7 15 23 27 23 17 1 12 15 ITK 179
AVL Trees(Ch 11.3, 11.4) -- an example of Dynamic trees • Adel’son-Vels’kii and Landis, Soviet Mathematic Doklady, 3:1259-1263, 1962 C. Crane, D. Knuth, et al in 1970’s We dynamicallymaintain the properties of AVL-tree when we insert (remove) a node by four different operations (rotations) Dynamic trees Static trees Huffman Code -- an example of Static trees We staticallyanalyze the code and build up an optimal tree for retrieving the code words. ITK 179
AVL Tree A BST in which the height difference between the two children of any node is always less than 2. +1 +1 -1 j k j+1 k+1 h h+1 ITK 179
performed at the bad node where the difference between the heights of its two children is bigger than 1 after insertion. Four operations : (rotations) If a node is bad caused by: then perform • Right-child’s Right-child • Left-child’s Left-child • Right-child’s Left-child • Left-Child’s Right-child • RR rotation • LL rotation • RL rotation • LR rotation ITK 179
No Rotation is Needed +1 0 -1 0 ITK 179
RR Rotations: +2 +1 ITK 179
RR Rotation: +2 R +0 R +0 +1 ITK 179
LL Rotation: L -2 +0 L -1 +0 ITK 179
Rotations: RL +1 +2 0 h+1 +1 -1 0 0 h+1 +0 -1 h h ITK 179
Rotations: RL RL Rotation: R 0 +2 L h+1 -1 0 +1 h+1 -1 h h h+1 h+1 h h ITK 179
Rotations: LR LR Rotation: L -2 0 1 R h+1 0 +1 h+1 -1 -1 h h h+1 h+1 h h ITK 179
Rotate this sub-tree first +2 R Could be RR or RL, depending on what happens in the blue sub-tree. R +2 Afterwards, examine the red node again to see is another rotation is needed. ITK 179
Rotations: LL +2 +1 +1 h+1 -1 -2 -1 h -2 -1 0 h-1 h -1 0 h-1 h-1 ITK 179
Example: 10 20 30 10 RR Rotation: 20 20 10 30 30 ITK 179
Example: 40 50 20 RR Rotation: 20 10 30 10 40 40 50 30 50 ITK 179
Example: 35 20 R 30 RL Rotation: 10 40 40 L 20 30 50 10 50 35 35 ITK 179
Possible complications Re-assign the links +2 h+1 -1 h+1 -1 h h Tracking the heights and balance-factors ITK 179
RR rotation in JAVA a=t +2 R // RR rotation on t; privatevoid RR(TNode<E> t){ TNode<E> a = t, b = t.right; a.right = b.left; b.left = a; a.height -= 2; return b; } b h R +1 h h height is an extra member variable in the TNode<E>. ITK 179
RL rotation in C++ a=t // RL rotation on t; privatevoid RL(TNode<E> t){ TNode<E>a, b, c; a = t; b = t.right; c = t.right.left; a.right = c.left; b.left = c.right; c.left = a; c.right = b; c.height++; b.height--; a.height-=2; return c; } b R +2 L c h+1 -1 h+1 -1 h h ITK 179
How to determine which rotation is needed? RR L L R L L R +2 -2 +2 -2 -1 +1 +1 -1 ITK 179
AVL h: Average Heights n Random Keys ITK 179