Presentation is loading. Please wait.

Presentation is loading. Please wait.

Deleting from a binary tree

Similar presentations


Presentation on theme: "Deleting from a binary tree"— Presentation transcript:

1 Deleting from a binary tree
Computer Science 2 Deleting from a binary tree

2 Learning Objectives Be able to improve in your ability to dry ru a program that uses Binary Trees Be able to delete and item from a binary search tree for the following cases The item is a leaf The item has one child The item has two children

3 Show the tree, and the screen
procedure two(top:ptrtype); begin if top^.left <> nil then two(top^.left); if top^.right <> nil then two(top^.right); writeln(top^.num); end; procedure three(top:ptrtype); three(top^.left); three(top^.right); procedure four(top:ptrtype); four(top^.left); four(top^.right); Show the tree, and the screen Top Dry run given the tree on the left. upon once time a big was tree there a Begin {** Code that created the above tree**} two(top); three(top); four(top); end.

4 Tree Demonstration Binary Tree Applet
Experiment with the applet to find out how to delete from a binary tree Which cases do you need to consider?

5 Getting Started Open your tree program
We will modify it when developing the find and delete.

6 Finding an item on a tree
Pseudo Code for Finding an item Cases Nil: Not in the tree Name matches: Found Name is less than top’s name, look left Else Look Right

7 Translating to code for Deleting
Procedure FindValueToDelete(Var top: pointerType; nam:string); Begin If top = nil then Writeln(name, ' is not in the list'); End else if top^.name = nam then Writeln(Name, ' has been found.'); readln; Delete(top, nam); //We need to develop this procedure End else if name < top^.name then FindValueToDelete(top^.left, nam) Else FindValueToDelete(top^.right, nam); End;

8 Tree Demonstration Binary Tree Applet
Experiment with the applet to find out how to delete from a binary tree Which cases do you need to consider?

9 Binary Search Trees: Operations — Delete [1/3]
Deletion is complex. We assume the key (value we want to delete) to be deleted is in the tree. Otherwise, the spec’s will tell us what to do. Flag an error? Do nothing? There are three cases: The node to be deleted has no children (it is a leaf). The node has 1 child. The node has 2 children. 10 4 16 13 30 20 42 25 22 28

10 No Children The no-children (leaf) case is easy:
Just delete the node, using the appropriate binary tree operation. Example: Delete key 42. How would you code this? If (top^.right = nil) and (top^.left = nil) then begin Dispose(top); Top:=nil; End; 10 4 16 13 30 20 42 25 22 28

11 To Code procedure delete(var top:PointerType; name:string); var temp:pointerType; begin If (top^.right = nil) and (top^.left = nil) then writeln('No Children'); Dispose(top); Top:=nil; End else

12 Binary Search Trees: Operations — Delete [2/3]
If the node to delete has exactly one child, then we replace the subtree rooted at it with the subtree rooted at its child. This is a constant-time operation, once the node is found. Example: Delete key 20. 10 10 10 4 16 4 16 4 16 = 13 30 13 30 13 30 20 42 42 25 42 25 25 22 28 22 28 22 28

13 Translating to code If (Top^.left = nil) then Begin
temp:= top; Top:= top^.right; Dispose(temp); End else if (top^.right = nil) then Temp:= top; Top:= top^.left; end

14 Translating to code Procedure FindValueToDelete(Var top: pointerType; nam:string); Begin If top = nil then Writeln(name, ' is not in the list'); End else if top^.name = nam then Writeln(Name, ' has been found.'); readln; Delete(top, nam); //For deleting option. End else if name < top^.name then FindValueToDelete(top^.left, nam) Else FindValueToDelete(top^.right, nam); End;

15 Now Delete 20 from the subtree
temp:= top^.right; while temp^.left<> nil do temp:= temp^.left; top^.name:= temp^.name; FindValueToDelete(top^.right, top^.name); Two Children The tricky case is when the node to delete has two children. Replace the node’s data with the data in its inorder successor. By copying or swapping. The inorder successor cannot have two children. Delete it as before. Example: Delete key 16. Now Delete 20 from the subtree 13 20 16 30 42 25 28 22 10 4 13 20 42 25 28 22 10 4 20? 30 10 4 20 As on previous slides 13 30 25 42 22 28

16 writeln('Two children'); temp:= top^. right; while temp^
writeln('Two children'); temp:= top^.right; while temp^.left<> nil do temp:= temp^.left; top^.name:= temp^.name; FindValueToDelete(top^.right, top^.name); Procedure FindValueToDelete is in the code AFTER Delete. But… How can you call this procedure? By forward declaring it.

17 Forward Declaring a Procedure
This lets you call a procedure that is in the code below the current procedure. How? Place the following above all of the procedures. Procedure FindValueToDelete(Var top: pointerType; nam:string); forward;

18 Tree Project Add the find one and Delete to your tree project (name and phone number) Push: Show the information as a tree. Push: Made a Trinary Search Tree where the third pointer (down) is used to resolve when the same value is entered more than once. Push: Add a function to count the number of nodes in a tree. Push : Add a function to count the number of levels in a tree.


Download ppt "Deleting from a binary tree"

Similar presentations


Ads by Google