Top Banner
Question 4 Tutorial 8
10

Tutorial 8

Dec 30, 2015

Download

Documents

sierra-patel

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. - PowerPoint PPT Presentation
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Tutorial 8

Question 4

Tutorial 8

Page 2: Tutorial 8

Part AInsert 20, 10, 15, 5 ,7, 30, 25, 18, 37, 12 and 40 in

sequence into an empty binary tree.

20

15

12

18

25

10

37

5

7

30

40

Page 3: Tutorial 8

Part BPart B requires us to determine the new BST

after each delete operation.3 cases to consider when deleting a node

from a BSTCase 1: Leaf node (no children)Case 2: Node has 2 childrenCase 3: Node has only one child

Page 4: Tutorial 8

Delete 30 from the BST30 has 2 childrenReplace node with inorder successor of 30, which is 37.

20

15

12

18

25

10

37

5

7

30

40

20

15

12

18

25

10

37

5

7

40

Page 5: Tutorial 8

Delete 10 from BST10 has two children.Replace with inorder successor, which is 12

20

15

12

18

25

10

37

5

7

40

20

15

12

18

25

37

5

7

40

Page 6: Tutorial 8

Delete 15 from BST15 currently has one child, 18.So just replace 15 with 18.

20

15

12

18

25

37

5

7

40

20

12

18

25

37

5

7

40

Page 7: Tutorial 8

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

18

25

37

5

7

40

Page 8: Tutorial 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

18

25

37

5

7

40

Page 9: Tutorial 8

If we delete 7

The resultant tree is both full and complete.

20

12

18

25

37

5

7

40

Page 10: Tutorial 8

After deleting 7, if We add 4

Resulting tree is not full.However, the tree is complete.

20

12

18

25

37

5

40

4