170 likes | 193 Views
Computer Science 2. Deleting from a binary tree Dry run, discussion, online time to incorporate ‘Delete’ into the name/phone number tree program. Learning Objectives. Be able to improve in your ability to dry run a program that uses Binary Trees
E N D
Computer Science 2 Deleting from a binary tree Dry run, discussion, online time to incorporate ‘Delete’ into the name/phone number tree program
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.
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); begin writeln(top^.num); if top^.left <> nil then three(top^.left); if top^.right <> nil then three(top^.right); end; procedure four(top:ptrtype); begin if top^.left <> nil then four(top^.left); writeln(top^.num); if top^.right <> nil then four(top^.right); end; Begin {** Code that created the above tree**} two(top); three(top); four(top); end. Show the tree, and the screen Top Dry run given the tree on the left. upon once time a big tree was there a
Getting Started • Open your tree program • We will modify it when developing the find and delete.
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
Translating to code for Deleting Procedure FindValueToDelete(Var top: pointerType; nam:string); Begin If top = nil then Begin Writeln(nam, ' is not in the list'); End else if top^.name = nam then Begin 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;
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 nochildren (it is a leaf). • The node has 1 child. • The node has 2 children. 10 4 16 13 30 20 42 25 22 28
No Children The no-children (leaf) case is easy: Just delete the node, using theappropriate 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
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 begin writeln('No Children'); Dispose(top); Top:=nil; End else
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
One Child To code If (Top^.left = nil) then Begin temp:= top; Top:= top^.right; Dispose(temp); End else if (top^.right = nil) then Begin Temp:= top; Top:= top^.left; Dispose(temp); end
0 and 1 Child to Code procedure delete(var top:PointerType; name:string); var temp:pointerType; begin If (top^.right = nil) and (top^.left = nil) then //No children begin 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 Begin Temp:= top; Top:= top^.left; Dispose(temp); end
10 10 4 16 4 20 13 30 13 30 20 42 20? 42 25 25 22 28 22 28 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 10 4 20 As onprevious slides 13 30 25 42 22 28
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);
Putting Delete Together procedure delete(vartop:PointerType; name:string); var temp:pointerType; begin If (top^.right = nil) and (top^.left = nil) then //No children begin 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 Begin Temp:= top; Top:= top^.left; Dispose(temp); End else //Two children Begin 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.
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;
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.