180 likes | 201 Views
Computer Science 2. Deleting from a binary tree. 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
E N D
Computer Science 2 Deleting from a binary tree
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
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
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? • https://www.cs.usfca.edu/~galles/visualization/BST.html
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(name, ' is not in the list'); End else if top^.name = nam then Begin 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;
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? • http://www.cs.jhu.edu/~goodrich/dsa/trees/btree.html
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
To Code procedure delete(var top:PointerType; name:string); var temp:pointerType; begin If (top^.right = nil) and (top^.left = nil) then begin writeln('No Children'); Dispose(top); Top:=nil; End else
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
Translating 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
Translating to code Procedure FindValueToDelete(Var top: pointerType; nam:string); Begin If top = nil then Begin Writeln(name, ' is not in the list'); End else if top^.name = nam then Begin 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;
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 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 10 4 20 As onprevious slides 13 30 25 42 22 28
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.
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 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.