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 Dry run, discussion, online time to incorporate ‘Delete’ into the name/phone number tree program

2 Learning Objectives Be able to improve in your ability to dry run a program that uses Binary Trees Be able to delete an 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 Be able to have a procedure call a procedure that is coded afterwards in the program.

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 Getting Started Open your tree program
We will modify it when developing the find and delete.

5 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

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

7 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

8 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

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

10 One Child If the node to delete has exactly one child, then we replace the subtree rooted at it with the subtree rooted at its child. 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

11 One Child 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

12 0 and 1 Child to Code If (Top^.left = nil) then //One child on right
procedure delete(var top:PointerType; name:string); var temp:pointerType; begin If (top^.right = nil) and (top^.left = nil) then //No children writeln('No Children'); Dispose(top); Top:=nil; End else If (Top^.left = nil) then //One child on right Begin temp:= top; Top:= top^.right; Dispose(temp); End else if (top^.right = nil) then //One child on left Temp:= top; Top:= top^.left; end

13 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 in-order successor. By copying or swapping. The in-order 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

14 Two Children to Code writeln('Two children'); temp:= top^.right; while temp^.left<> nil do temp:= temp^.left; top^.name:= temp^.name; FindValueToDelete(top^.right, top^.name);

15 Putting Delete Together
procedure delete(var top:PointerType; name:string); var temp:pointerType; begin If (top^.right = nil) and (top^.left = nil) then //No children writeln('No Children'); Dispose(top); Top:=nil; End else If (Top^.left = nil) then //One child on right Begin temp:= top; Top:= top^.right; Dispose(temp); End else if (top^.right = nil) then //One child on left Temp:= top; Top:= top^.left; End else //Two children writeln('Two children'); temp:= top^.right; while temp^.left<> nil do temp:= temp^.left; top^.name:= temp^.name; FindValueToDelete(top^.right, top^.name); End; End;//Of procedure delete But … Procedure FindValueToDelete is in the code AFTER Delete. How can you call this procedure? By forward declaring it.

16 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;

17 Tree Project Add the Delete to your tree project (name and phone number. Now has add, show all, find one, delete one) 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: Change information 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