310 likes | 403 Views
What is the benefit of using BST?. N=30. Search 17, 29, 3. 19. 10. 28. 4. 14. 23. 35. 1. 7. 12. 17. 26. 32. 21. 37. 0. 3. 5. 11. 13. 15. 18. 20. 22. 25. 27. 30. 34. 36. 38. log 2 30 = 5. O(log 2 n). What is the real benefit of using BST?. Search 18, 2, 26.
E N D
What is the benefit of using BST? N=30 Search 17, 29, 3 19 10 28 4 14 23 35 1 7 12 17 26 32 21 37 0 3 5 11 13 15 18 20 22 25 27 30 34 36 38 log230 = 5 ITK 275
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 275
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. Proof: skip f(n) : The average internal path length of an n-node BST ITK 275
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 275
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 275
Highly unbalanced BST 1 7 17 12 13 25 25 7 15 23 27 13 27 1 12 23 Balanced BST 17 15 ITK 275
If R is too big, then Shift a Node from R to L 1. Insert to L is a BST 2. Find the min in R, 3. Copy to the root and delete it from R L R ITK 275
How to measure the unbalance? How big is “too big”? How unbalanced do we allow a BST to be? Chung-Chih Li. An immediate approach to balancing nodes in binary search trees. Journal of Computing Sciences in Colleges , 21(3):238--245 April 2006. ITK 275
Insert a node to an NBk ITK 275
Insert a node to an NBk ITK 275
Delete a node from an NBk ITK 275
Delete a node from an NBk ITK 275
Analysis Average Heights on n Random Keys BST 1. 2. Devroye and Reed,, SIAM J. Comput. ‘95 ITK 275
Analysis of NBk with nRandom Keys n the worst case a a2 X2 a3 X X3 ITK 275
Analysis of NBk with nRandom Keys the worst case At depth h ITK 275
Analysis of NBk with nRandom Keys the worst case At depth h BST: AVL: ITK 275
Experimental results Computational Cost • AVL --- algorithm is complicated • NBk ---shiftingoperations • BST ---traveling long paths ITK 275
Random and no duplication in data log(n) 1.2 M ITK 275
2.66 GHz P4 1GB M MS XP Visual C++ Random and no duplication in data ITK 275
Random, each key has n/100 duplicates log(n) 1.2 M ITK 275
2.66 GHz P4 1GB M MS XP Visual C++ Random, each key has n/100 duplicates ITK 275
Random, arrive in batches of 32 sorted records log(n) ITK 275 1.2 M
2.66 GHz P4 1GB M MS XP Visual C++ Random, arrive in batches of 32 sorted records ITK 275
NBk Conclusion • can build a near optimal BST even when k is small • easy to analyze • easy to implement • practical in most conditions While NBk's computational cost is much better than BST and close to or better than AVL, there is no guarantee on any data. In other words, it is not as robust as AVL. ITK 275
AVL Tree: A BST in which the height difference between the two children of any node is always less than 2. +1 +1 -1 h’ h’+1 h h+1 ITK 275
Rotations: RR +1 +2 +1 ITK 275
Rotations: RL +1 +2 0 h+1 +1 -1 0 0 h+1 +0 -1 h h ITK 275
Rotations: LL +2 +1 +1 h+1 -2 -1 -1 0 h+1 -2 -1 0 h-1 h -1 0 h-1 h-1 ITK 275
Possible complications Re-assign the links +2 h+1 -1 h+1 -1 h h Tracking the heights and balance-factors ITK 275
AVL h: Average Heights n Random Keys ITK 275