Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computer Science 2: Trees

Similar presentations


Presentation on theme: "Computer Science 2: Trees"— Presentation transcript:

1 Computer Science 2: Trees

2 Goals/Learning Objectives/Learning Targets/…
Complete a dry run that includes, dynamics, recursion, and multiple pointers! Use notes to help capture today’s thoughts. Understand the definition of a Binary Tree. Be able to demonstrate adding to a tree. Develop pseudo-code and code for adding to a tree. Develop pseudo-code and code for showing from a tree.

3 procedure b(top:ptrtype); begin if top^.left<> nil then
program DryRunTree1; type ptrtype = ^nodetype; nodetype = record score:integer; left, right:ptrtype; end; var top:ptrtype; num:integer; procedure a(var top:ptrtype;count:integer); begin if top = nil then new(top); top^.score:=count; top^.left:=nil; top^.right:=nil; end else if count MOD 2 = 0 then a(top^.right, 2*count) else a(top^.left, 2*count); procedure b(top:ptrtype); begin if top^.left<> nil then b(top^.left); writeln(top^.score); if top^.right<> nil then b(top^.right); end; top:=nil; for num:= 1 to 5 do a(top,num); b(top); end.

4 Binary Trees What is it? Benefits
In computer science, a binary tree is a tree data structure in which each node has at most two children. Typically the child nodes are called left and right. One common use of binary trees is binary search trees. Benefits Dynamic size Fast access (Compared to an insertion linked list) Sorts data as it is entered.

5 Node 1,2,3 are children of root
Definitions root Nodes 1-6 are descendants of node 0 Node 0 Node 1,2,3 are children of root Node 1 Node 2 Node 3 Node 1 is parent of Nodes 4,5 Node 4 Node 5 Node 6 leaves Node 4 and 5 are siblings

6 Binary Search Trees A binary tree where every node's left sub tree has values less than the node's value, and every right sub tree has values greater. 76 39 80 32 47 79

7 Binary Search Trees Applet: animated BST
How to add to a binary search tree ? Applet: animated BST

8 Add Pseudo Code If top is nill
Practice Adding: 50, 8, 90, 45, 12, 63, 20, 14, 71 90, 80, 70, 60, 50,40 Pseudo Code If top is nill Put it in the tree Else if bigger than or equal to top’s value Add value to top’s right Else Add value to top’s left

9 Developing the code for adding
procedure add(var top:ptrtype;c:integer); var temp:ptrtype; begin if top=nil then new(temp); top:=temp; temp^.num:=c; temp^.left:=nil; temp^.right:=nil; end else if c>= top^.num then add(top^.right, c) else add(top^.left, c); end; Pseudo Code If top is nill Put it in the tree Else if >= top’s value Add value to top’s right Else Add value to top’s left Test it with… 30, 50, 20, 75, 15, 65 3, 6, 8, 30, 27, 90, 100

10 Take 3 minutes to summarize your information about Binary Trees.
Definitions Parts Adding algorithm

11 Showing the Information
What is the first number that should be shown? (Low to high) How can you get there? How about the second value? Third…? How can you do this? 76 39 80 32 47 79

12 Developing code for show
PseudoCode Show in order (Infix) If you can go left Go left Show If you can go right Go right procedure show(top:ptrtype); begin if top^.left<> nil then show(top^.left); writeln(top^.num); if top^.right<> nil then show(top^.right); end;

13 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.


Download ppt "Computer Science 2: Trees"

Similar presentations


Ads by Google