1 / 207

Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree

Lecture 12. Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree. Traversing a Binary Tree Inorder Traversal. The Scenario. Imagine we have a binary tree We want to traverse the tree It’s not linear We need a way to visit all nodes

vesta
Download Presentation

Traversing a Binary Tree Binary Search Tree Insertion Deleting from a Binary Search Tree

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Lecture 12 Traversing a Binary TreeBinary Search Tree Insertion Deleting from a Binary Search Tree

  2. Traversing a Binary TreeInorder Traversal

  3. The Scenario • Imagine we have a binary tree • We want to traverse the tree • It’s not linear • We need a way to visit all nodes • Three things must happen: • Deal with the entire left sub-tree • Deal with the current node • Deal with the entire right sub-tree

  4. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2

  5. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 13 – do left

  6. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 9 – do left At 13 – do left

  7. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 2 – do left At 9 – do left At 13 – do left

  8. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At NIL At 2 – do left At 9 – do left At 13 – do left

  9. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 2 – do current At 9 – do left At 13 – do left

  10. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 2 – do right At 9 – do left At 13 – do left

  11. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At NIL At 2 – do right At 9 – do left At 13 – do left

  12. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 2 - done At 9 – do left At 13 – do left

  13. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 9 – do current At 13 – do left

  14. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 9 – do right At 13 – do left

  15. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At NIL At 9 – do right At 13 – do left

  16. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 9 – done At 13 – do left

  17. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 13 – do current

  18. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 13 – do right

  19. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 42 – do left At 13 – do right

  20. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At NIL At 42 – do left At 13 – do right

  21. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 42 – do current At 13 – do right

  22. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 42 – do right At 13 – do right

  23. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At NIL At 42 – do right At 13 – do right

  24. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 42 – done At 13 – do right

  25. Use the Activation Stack • With a recursive module, we can make use of the activation stack to visit the sub-trees and “remember” where we left off. 13 9 42 2 At 13 – done

  26. Outline of In-Order Traversal • Three principle steps: • Traverse Left • Do work (Current) • Traverse Right • Work can be anything • Separate work from traversal

  27. Traverse the tree “In order”: • Visit the tree’s left sub-tree • Visit the current and do work • Visit right sub-tree

  28. In-Order Traversal Procedure procedure In_Order(cur iot in Ptr toa Tree_Node) // Purpose: perform in-order traversal, call // Do_Something for each node // Preconditions: cur points to a binary tree // Postcondition: Do_Something on each tree // node in “in-order” order • if( cur <> NIL ) then • In_Order( cur^.left_child ) • Do_Something( cur^.data ) • In_Order( cur^.right_child ) • endif endprocedure // In_Order

  29. 22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R

  30. 22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R

  31. 22 67 36 3 14 44 7 94 97 1 9 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L P R

  32. 7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R

  33. 7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R

  34. 7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L

  35. 7 97 94 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L

  36. 94 97 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L L

  37. 94 97 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L L

  38. 94 97 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L L L

  39. 94 97 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L L L

  40. 97 94 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 L L P

  41. 97 94 7 44 14 1 36 67 9 22 3 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 L L P

  42. 1 22 9 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 L L P R

  43. 1 22 9 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 L L P R

  44. 1 22 9 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 L L P R

  45. 1 22 9 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P L P R

  46. 1 22 9 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P L P R

  47. 9 22 1 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P R L P R

  48. 9 22 1 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P R L P R

  49. 9 22 1 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P R L L P R

  50. 9 22 1 67 36 3 14 44 7 94 97 Proc InOrderPrint(pointer) pointer NOT NIL? InOrderPrint(left child) print(data) InOrderPrint(right child) root L L P R L Output: 1 3 L P R L L P R

More Related