1 / 15

Tutorial 8

Tutorial 8. Algorithms Design and Analysis – CS411. Question 1. Perform Inorder , Preorder and Postorder traversal on the below tree:. 7. 9. 1. 10. 3. 8. 0. 2. 5. 4. 6. Answer. Inorder (LNR) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Preorder (NLR) 7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10

Download Presentation

Tutorial 8

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. Tutorial 8 Algorithms Design and Analysis – CS411

  2. Question 1 Perform Inorder, Preorder and Postorder traversal on the below tree: 7 9 1 10 3 8 0 2 5 4 6

  3. Answer Inorder (LNR) 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 Preorder (NLR) 7, 1, 0, 3, 2, 5, 4, 6, 9, 8, 10 Postorder (LRN) 0, 2, 4, 6, 5, 3, 1, 8, 10, 9, 7

  4. Question 2 Perform Inorder, Preorder, Postorder traversal on the below tree: + * A / B D C

  5. Answer Inorder (LNR) A + B * C / D Preorder (NLR) + A * B / C D Postorder (LRN) A B C D / * +

  6. Remember For the deletion in binary search tree, threepossible cases should be considered: 1. Deleting a leaf (node with no children): deleting a leaf is easy, as we can simply remove it from the tree. 2. Deleting a node with one child: remove the node and replace it with its child. 3. Deleting a node with two children:call the node to be deleted N. Do not delete N. Instead, choose its successor node, R. Replace the value of N with the value of R, then delete R.

  7. Remember • Node's successor: the left-most child of its right subTree. • Node's predecessor: the right-most child of its left subTree. In either case, this node will have zero or one children. Delete it according to one of the two simpler cases above.

  8. Question 3 Find: • The minimum node • The maximum node • Successor of node 8 • Predecessor of node 8 • Successor of node 7 8 10 3 14 1 6 13 4 7

  9. Answer • 1 • 14 • 10 smallest in the right subTreeof 8 • 7 largest in the left subTreeof 8 • 8 since 7 has no right subTree, 8 is the ancestor whose left child (3) is also an ancestor.

  10. Question 4 For the below binary search tree, do the following actions separately: • Insert node 2 • Insert node 9 • Delete node 14 • Delete node 3 • Delete node 8 8 10 3 14 1 6 13 4 7

  11. Answer (a) 8 10 3 6 14 1 13 2 4 7

  12. Answer (b) 8 10 3 6 9 14 1 13 4 7

  13. Answer (c) 8 10 3 6 13 1 4 7

  14. Answer (d) 8 10 4 6 14 1 13 7

  15. Answer (e) 10 14 3 6 1 13 4 7

More Related