What is the benefit of using BST?

# What is the benefit of using BST?

## What is the benefit of using BST?

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
##### Presentation Transcript

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. Insert a node to an NBk ITK 275

10. Insert a node to an NBk ITK 275

11. Delete a node from an NBk ITK 275

12. Delete a node from an NBk ITK 275

13. Analysis Average Heights on n Random Keys BST 1. 2. Devroye and Reed,, SIAM J. Comput. ‘95 ITK 275

14. Analysis of NBk with nRandom Keys n the worst case a a2 X2 a3 X X3 ITK 275

15. Analysis of NBk with nRandom Keys the worst case At depth h   ITK 275

16. Analysis of NBk with nRandom Keys the worst case At depth h  BST: AVL: ITK 275

17. Experimental results Computational Cost • AVL --- algorithm is complicated • NBk ---shiftingoperations • BST ---traveling long paths ITK 275

18. Random and no duplication in data log(n) 1.2 M ITK 275

19. 2.66 GHz P4 1GB M MS XP Visual C++ Random and no duplication in data ITK 275

20. Random, each key has n/100 duplicates log(n) 1.2 M ITK 275

21. 2.66 GHz P4 1GB M MS XP Visual C++ Random, each key has n/100 duplicates ITK 275

22. Random, arrive in batches of 32 sorted records log(n) ITK 275 1.2 M

23. 2.66 GHz P4 1GB M MS XP Visual C++ Random, arrive in batches of 32 sorted records ITK 275

24. 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

25. 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

26. Rotations: RR +1 +2 +1 ITK 275

27. Rotations: RL +1 +2 0 h+1 +1 -1 0 0 h+1 +0 -1 h h ITK 275

28. 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

29. Possible complications Re-assign the links +2 h+1 -1 h+1 -1 h h Tracking the heights and balance-factors ITK 275

30. AVL h: Average Heights n Random Keys ITK 275