problem of the day you drive a bus from rotterdam to delft. at the 1 st stop, 33 people get in. ...
TRANSCRIPT
Problem of the Day
You drive a bus from Rotterdam to Delft. At the 1st stop, 33 people get in. At the 2nd stop, 7 add people & 11
passengers leave. The 3rd stop, sees 5 people leave and 2 get
in. After one hour, the bus arrives in Delft.
What is the name of the driver?
Problem of the Day
You drive a bus from Rotterdam to Delft. At the 1st stop, 33 people get in. At the 2nd stop, 7 add people & 11
passengers leave. The 3rd stop, sees 5 people leave and 2 get
in. After one hour, the bus arrives in Delft.
What is the name of the driver?It’s your name!
LECTURE 33:TREE AND BINARY TREE ADT
CSC 212 – Data Structures
Trees
Represent hierarchical relationships Parent-child basis of this abstract data type
Real-world example: directories & types
csc213/
homeworks/
ideas.todo
1M
programs/
LLQueue.java
10K
DLNode.java
25K
hard.txt
3K
impossible.txt
2K
Tree.java
20K
Trees
Represent hierarchical relationships Parent-child basis of this abstract data type
Real-world example: organism classificationMamma
l
Cat
Ape
Lion
Human
Chimpanzee
Professors
Yankees Fan
Tree Basics
Yet another Collection Nodes are Positions # of elements is its size
Some things are familiar Using Position
to hold elements Others are very different
Node class has little overlap Cannot discuss next & prev
A
B DC
G HE F
I J K
Tree Terms
Defined by relationships Talk about parents,
siblings, descendant
Nodes have no gender Will often mix in terms
like mother, father, niece, aunt, uncle,cousin, …
A
B DC
G HE F
I J K
Tree Terms
Tree rooted at node A “Top” or “Start” of tree
B, C, D are children of A A is parent of B, C, D B, C, D are siblings E, F, G, H are
grandchildren of A
A
B DC
G HE F
I J K
Tree Relationships
Nodes either leaf or interior Both or neither not options
A, B, C, F are parents At least 1 child for each Also called “interior nodes”
"Leaves" name of others Leaf node has no children
A
B DC
G HE F
I J K
Trees Recursion
Trees are recursive structure Subtree defined by a node
A
B DC
G HE F
I J K
Trees Recursion
Trees are recursive structure Subtree defined by a node C is root of this subtree
A
B DC
G HE F
I J K
Trees Recursion
Trees are recursive structure Subtree defined by a node C is root of this subtree D root of this subtree
A
B DC
G HE F
I J K
Trees Recursion
Trees are recursive structure Subtree defined by a node C is root of this subtree D root of this subtree B root of this subtree
A
B DC
G HE F
I J K
Trees Recursion
Trees are recursive structure Subtree defined by a node C is root of this subtree D root of this subtree B root of this subtree
Trees are like professors Some are tall & thin
Trees Recursion
Trees are recursive structure Subtree defined by a node C is root of this subtree D root of this subtree B root of this subtree
Trees are like professors Some are tall & thin Short & boring a possibility
Trees Recursion
Trees are recursive structure Subtree defined by a node C is root of this subtree D root of this subtree B root of this subtree
Trees are like professors Some are tall & thin Short & boring a possibility Others can be empty
Trees vs. Binary Trees
Both represent parent-child relationships Both consist of single "root" node & its
descendants Nodes can have at most one parent
Root nodes are orphans -- do not have a parent
All others, the non-root nodes must have parent
Children not required for any node in the tree No limit to number of children for non-
binary nodes 2 children for node in binary tree is the
maximum
Binary Tree
Extends Tree interface & adds following property 2 or fewer children per node
Typically drawing of tree sets childrens' names “Left child” is name of child to left of the
parent Child towards the right is called “Right
child”
R
B D
HE F
I
Height and Size of a Tree
Level 0 – max. 1 node
Maximum number of nodes doubles each level
Height and Size of a Tree
Level 1 – max. 2 nodes
Level 0 – max. 1 node
Maximum number of nodes doubles each level
Height and Size of a Tree
Level 2 – max. 4 nodes
Level 1 – max. 2 nodes
Level 0 – max. 1 node
Maximum number of nodes doubles each level
Height and Size of a Tree
Level 3 – max. 8 nodes
Level 2 – max. 4 nodes
Level 1 – max. 2 nodes
Level 0 – max. 1 node
Maximum number of nodes doubles each level
For a level i in a binary tree That level can have 2i nodes At most 2(i+1)-1 nodes for tree to that level
Independent of Implementation
A
B
Z F
I Q M
Binary Tree, Tree, or Other?
Binary Tree, Tree, or Other?
Binary Tree, Tree, or Other?
Binary Tree, Tree, or Other?
Binary Tree, Tree, or Other?
Binary Tree, Tree, or Other?
Binary Tree, Tree, or Other?
A B C D
Binary Tree, Tree, or Other?
Your Turn
Get into your groups and complete activity
For Next Lecture
Read parts of GT 7.2 & 7.3.6 for Friday Trees are Iterable, but how would it be
done? What are the types of traversals & how do
they differ? When do we do parent first & when do we
do our kid?
Week #12 assignment posted & due Tuesday
Programming Assignment #2 due Friday If you want help do not delay, I’m not
here tomorrow