Presentation is loading. Please wait.

Presentation is loading. Please wait.

More Trees 5/9/2-017 Be able to dry run a program that uses trees

Similar presentations


Presentation on theme: "More Trees 5/9/2-017 Be able to dry run a program that uses trees"— Presentation transcript:

1 More Trees 5/9/2-017 Be able to dry run a program that uses trees
Finding someone in the tree.

2 Show the trees after adding the following
20, 10, 30, 15, 20, 5, 100 30, 8, 40, 20, 30, 15

3 Show the tree, and the screen
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); four(top^.left); four(top^.right); top:=nil; for count:= 1 to 5 do new(temp); readln(temp^.num); temp^.left:=nil; temp^.right:=nil; one(top, temp); two(top); three(top); four(top); end. Use the following values for input, in this order: 30, 10, 50, 15, 3. procedure one(var top, temp:ptrtype); begin if top=nil then top:=temp else if top^.num<temp^.num then one(top^.right, temp) else one(top^.left, temp); end; procedure two(top:ptrtype); if top^.left <> nil then two(top^.left); if top^.right <> nil then two(top^.right); writeln(top^.num);

4 Tree 1 Menu Push: Show all (High to low using the same tree)
Add a name Show All (In order: Low to high) Push: Show all (High to low using the same tree) Push: Add a procedure to check is someone is in the tree. (If they are in the tree it says they are, if they are not, it says that are not.) Push: Make this a boolean function that returns true if they are in the tree and false if they are not. Push: Delete someone from the tree. Push: On the Show from a tree, have it show the information like a tree.

5 Finding someone in a Binary Search Tree
How would you find the 800 in the tree? Starting at the root (top). What possible conditions are there when going through the tree?

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 Procedure FindValue(top: ptrtype: 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.’); End else if name < top^.name then FindValue(top^.left, nam) Else FindValue(top^.right, nam); End.

8 Tree 2 Menu (while loop) Add name and phone
Show all names and phone numbers Find: Given a name, show the name and number Push: Delete someone from the list Push: Change someone’s information. Phone, name


Download ppt "More Trees 5/9/2-017 Be able to dry run a program that uses trees"

Similar presentations


Ads by Google