problem of the day you drive a bus from rotterdam to delft. at the 1 st stop, 33 people get in. ...

33
Problem of the Day You drive a bus from Rotterdam to Delft. At the 1 st stop, 33 people get in. At the 2 nd stop, 7 add people & 11 passengers leave. The 3 rd stop, sees 5 people leave and 2 get in. After one hour, the bus arrives in Delft. What is the name of the driver?

Upload: mavis-ellis

Post on 14-Dec-2015

214 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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?

Page 2: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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!

Page 3: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

LECTURE 33:TREE AND BINARY TREE ADT

CSC 212 – Data Structures

Page 4: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 5: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 6: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 7: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 8: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 9: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 10: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Trees Recursion

Trees are recursive structure Subtree defined by a node

A

B DC

G HE F

I J K

Page 11: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 12: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 13: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 14: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 15: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 16: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 17: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 18: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 19: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Height and Size of a Tree

Level 0 – max. 1 node

Maximum number of nodes doubles each level

Page 20: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Height and Size of a Tree

Level 1 – max. 2 nodes

Level 0 – max. 1 node

Maximum number of nodes doubles each level

Page 21: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 22: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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

Page 23: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Independent of Implementation

A

B

Z F

I Q M

Page 24: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Binary Tree, Tree, or Other?

Page 25: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Binary Tree, Tree, or Other?

Page 26: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Binary Tree, Tree, or Other?

Page 27: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Binary Tree, Tree, or Other?

Page 28: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Binary Tree, Tree, or Other?

Page 29: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Binary Tree, Tree, or Other?

Page 30: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Binary Tree, Tree, or Other?

A B C D

Page 31: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Binary Tree, Tree, or Other?

Page 32: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

Your Turn

Get into your groups and complete activity

Page 33: Problem of the Day  You drive a bus from Rotterdam to Delft.  At the 1 st stop, 33 people get in.  At the 2 nd stop, 7 add people & 11 passengers leave

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