1 / 10

Tutorial 8

Tutorial 8. Question 4. Part A. Insert 20, 10, 15, 5 ,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree. 20. 10. 30. 25. 37. 5. 15. 40. 7. 12. 18. Part B. Part B requires us to determine the new BST after each delete operation.

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

  2. Part A • Insert 20, 10, 15, 5 ,7, 30, 25, 18, 37, 12 and 40 in sequence into an empty binary tree. 20 10 30 25 37 5 15 40 7 12 18

  3. Part B • Part B requires us to determine the new BST after each delete operation. • 3 cases to consider when deleting a node from a BST • Case 1: Leaf node (no children) • Case 2: Node has 2 children • Case 3: Node has only one child

  4. Delete 30 from the BST • 30 has 2 children • Replace node with inorder successor of 30, which is 37. 20 20 10 10 37 30 25 25 37 5 5 15 15 40 40 7 7 12 12 18 18

  5. Delete 10 from BST • 10 has two children. • Replace with inorder successor, which is 12 20 20 10 12 37 37 25 25 5 5 15 15 40 40 7 7 12 18 18

  6. Delete 15 from BST • 15 currently has one child, 18. • So just replace 15 with 18. 20 20 12 12 37 37 25 25 5 5 15 40 40 18 7 7 18

  7. Part C :Is the binary tree full ??? • A full binary tree is a tree where all nodes other than leaves have 2 children, and all leaves are at the same height. • Tree is not full. 20 12 37 25 5 40 18 7

  8. Part C :Is the Binary Tree Complete ??? • A binary tree is complete when all levels except POSSIBLY the last is completely filled, and the nodes are filled from left to right. • This tree is not complete. 20 12 37 25 5 40 18 7

  9. If we delete 7 • The resultant tree is both full and complete. 20 12 37 25 5 40 18 7

  10. After deleting 7, if We add 4 • Resulting tree is not full. • However, the tree is complete. 20 12 37 25 40 18 5 4

More Related