Binary Search Tree

# Binary Search Tree

## Binary Search Tree

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

1. Binary Search Tree A running demonstration of binary search data structure and algorithms

2. 87 Insert First value is the 'Root' of the tree.

3. 87 50 Insert Since 50 is Less than 87, we move to the left sub-tree **Since no sub-tree, 50 is now added as the left leaf**

4. 87 50 27 Insert Since 27 is Less than 87, we move to the left sub-tree Since 27 is Less than 50, we move to the left sub-tree **Since no sub-tree, 27 is now added as the left leaf**

5. 87 50 111 27 Insert Since 111 is Greater than 87, we move to the right sub-tree **Since no sub-tree, 111 is now added as the right leaf**

6. 87 50 111 27 99 Insert Since 99 is Greater than 87, we move to the right sub-tree Since 99 is Less than 111, we move to the left sub-tree **Since no sub-tree, 99 is now added as the left leaf**

7. 87 50 111 27 99 42 Insert Since 42 is Less than 87, we move to the left sub-tree Since 42 is Less than 50, we move to the left sub-tree Since 42 is Greater than 27, we move to the right sub-tree **Since no sub-tree, 42 is now added as the right leaf**

8. 87 50 111 27 99 42 90 Insert Since 90 is Greater than 87, we move to the right sub-tree Since 90 is Less than 111, we move to the left sub-tree Since 90 is Less than 99, we move to the left sub-tree **Since no sub-tree, 90 is now added as the left leaf**

9. 87 50 111 27 99 42 90 105 Insert Since 105 is Greater than 87, we move to the right sub-tree Since 105 is Less than 111, we move to the left sub-tree Since 105 is Greater than 99, we move to the right sub-tree **Since no sub-tree, 105 is now added as the right leaf**

10. 87 50 111 27 58 99 42 90 105 Insert Since 58 is Less than 87, we move to the left sub-tree Since 58 is Greater than 50, we move to the right sub-tree **Since no sub-tree, 58 is now added as the right leaf**

11. 87 50 111 27 58 99 42 90 105 32 Insert Since 32 is Less than 87, we move to the left sub-tree Since 32 is Less than 50, we move to the left sub-tree Since 32 is Greater than 27, we move to the right sub-tree Since 32 is Less than 42, we move to the left sub-tree **Since no sub-tree, 32 is now added as the left leaf**

12. 87 50 111 27 58 99 42 68 90 105 32 Insert Since 68 is Less than 87, we move to the left sub-tree Since 68 is Greater than 50, we move to the right sub-tree Since 68 is Greater than 58, we move to the right sub-tree **Since no sub-tree, 68 is now added as the right leaf**

13. 87 50 111 27 58 99 42 68 90 105 32 43 Insert Since 43 is Less than 87, we move to the left sub-tree Since 43 is Less than 50, we move to the left sub-tree Since 43 is Greater than 27, we move to the right sub-tree Since 43 is Greater than 42, we move to the right sub-tree **Since no sub-tree, 43 is now added as the right leaf**

14. 87 50 111 27 58 99 42 68 90 105 32 43 60 Insert Since 60 is Less than 87, we move to the left sub-tree Since 60 is Greater than 50, we move to the right sub-tree Since 60 is Greater than 58, we move to the right sub-tree Since 60 is Less than 68, we move to the left sub-tree **Since no sub-tree, 60 is now added as the left leaf**

15. 87 50 111 27 58 99 42 68 90 105 32 43 60 70 Insert Since 70 is Less than 87, we move to the left sub-tree Since 70 is Greater than 50, we move to the right sub-tree Since 70 is Greater than 58, we move to the right sub-tree Since 70 is Greater than 68, we move to the right sub-tree **Since no sub-tree, 70 is now added as the right leaf**

16. 87 50 111 27 58 99 42 51 68 90 105 32 43 60 70 Insert Since 51 is Less than 87, we move to the left sub-tree Since 51 is Greater than 50, we move to the right sub-tree Since 51 is Less than 58, we move to the left sub-tree **Since no sub-tree, 51 is now added as the left leaf**

17. 87 50 111 27 58 99 1 42 51 68 90 105 32 43 60 70 Insert Since 1 is Less than 87, we move to the left sub-tree Since 1 is Less than 50, we move to the left sub-tree Since 1 is Less than 27, we move to the left sub-tree **Since no sub-tree, 1 is now added as the left leaf**

18. 87 50 111 27 58 99 1 42 51 68 90 105 11 32 43 60 70 Insert Since 11 is Less than 87, we move to the left sub-tree Since 11 is Less than 50, we move to the left sub-tree Since 11 is Less than 27, we move to the left sub-tree Since 11 is Greater than 1, we move to the right sub-tree **Since no sub-tree, 11 is now added as the right leaf**

19. 87 50 111 27 58 99 1 42 51 68 90 105 11 32 43 60 70 Predecessor The node to delete is 50 Predecessor is found by finding the right most node of the 50 nodes left sub-tree

20. 87 50 111 27 58 99 1 42 51 68 90 105 11 32 43 60 70 Sucsessor Successor is found by finding the left most node of the 50 nodes right sub-tree

21. 87 50 111 27 58 99 1 42 51 68 90 105 11 32 43 60 70 How to delete 50? There is a left child so we find predecessor To learn more about finding predecessor insert <Fpred>starting value</Fpred> to data file

22. 87 43 111 27 58 99 1 42 51 68 90 105 11 32 60 70 Delete There is a left child so we find predecessor This is how the new tree would appear

23. 87 43 111 27 58 99 1 42 51 68 90 105 11 32 60 70 How to delete 105? This is a Leaf case We just remove this node from the tree

24. 87 43 111 27 58 99 1 42 51 68 90 11 32 60 70 Delete This is a Leaf case This is how the new tree would appear

25. 87 43 111 27 58 99 1 42 51 68 90 11 32 60 70 How to delete 87? There is a left child so we find predecessor To learn more about finding predicessor insert <Fpred>starting value</Fpred> to data file

26. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 Delete There is a left child so we find predecessor This is how the new tree would appear

27. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 How to delete 111? There is only one child, so replace the node with its only child

28. 70 43 99 27 58 90 1 42 51 68 11 32 60 After 111 is replaced by its only child There is only one child, so replace the node with its only child

29. PreOrder Traversal

30. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

31. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

32. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

33. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

34. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

35. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

36. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

37. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

38. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

39. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

40. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

41. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

42. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

43. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 PreOrder Traversal 1.Visit the root 2.Visit the left sub-tree 3.Visit the right sub-tree

44. InOrder Traversal

45. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree

46. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree

47. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree

48. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree

49. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree

50. 70 43 111 27 58 99 1 42 51 68 90 11 32 60 InOrder Traversal 1.Visit the left sub-tree 2.Visit the root 3.Visit the right sub-tree