150 likes | 226 Views
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
E N D
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 Postorder (LRN) 0, 2, 4, 6, 5, 3, 1, 8, 10, 9, 7
Question 2 Perform Inorder, Preorder, Postorder traversal on the below tree: + * A / B D C
Answer Inorder (LNR) A + B * C / D Preorder (NLR) + A * B / C D Postorder (LRN) A B C D / * +
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.
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.
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
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.
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
Answer (a) 8 10 3 6 14 1 13 2 4 7
Answer (b) 8 10 3 6 9 14 1 13 4 7
Answer (c) 8 10 3 6 13 1 4 7
Answer (d) 8 10 4 6 14 1 13 7
Answer (e) 10 14 3 6 1 13 4 7