/
Trees Lecture 12 CS2110 – Spring 2019 Trees Lecture 12 CS2110 – Spring 2019

Trees Lecture 12 CS2110 – Spring 2019 - PowerPoint Presentation

pamella-moone
pamella-moone . @pamella-moone
Follow
344 views
Uploaded On 2019-06-27

Trees Lecture 12 CS2110 – Spring 2019 - PPT Presentation

Announcements Submit P1 Conflict quiz on CMS by end of day Wednesday We wont be sending confirmations no news is good news Extra time people will eventually get an email from Lacy Please be patient ID: 760426

binary tree insert trees tree binary trees insert bst recursive data height search nodes case subtree node left january

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Trees Lecture 12 CS2110 – Spring 2019" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Trees

Lecture 12

CS2110 – Spring 2019

Slide2

Announcements

Submit P1 Conflict quiz on CMS by end of day Wednesday. We won’t be sending confirmations; no news is good news. Extra time people will eventually get an email from Lacy. Please be patient.

2

Slide3

Today’s Topics in JavaHyperText

Search for “trees”Read PDFs for points 0 through 5: intro to trees, examples of trees, binary trees, binary search trees, balanced trees

3

Slide4

Data Structures

Data structureOrganization or format for storing or managing dataConcrete realization of an abstract data typeOperationsAlways a tradeoff: some operations more efficient, some less, for any data structureChoose efficient data structure for operations of concern

4

Slide5

Example Data Structures

Data Structureadd(val v)get(int i)ArrayLinked List

5

2

1

3

0

2

1

3

0

 

 

 

 

add

(v): append v

get

(i): return element at position

i

contains

(v): return true if contains v

contains

(

val

v)

 

 

Slide6

Tree

6

Singly linked list:

2

1

1

0

Node object

pointer

int value

Today: trees!

0

4

1

1

0

2

1

1

Slide7

Trees

7

In CS, we draw trees “upside down”

Slide8

Tree Overview

8

Tree: data structure with nodes, similar to linked listEach node may have zero or more successors (children)Each node has exactly one predecessor (parent) except the root, which has noneAll nodes are reachable from root

A tree

Not a tree

Not a tree

A tree

5

4

2

7

8

9

5

4

2

7

8

9

5

6

7

5

4

7

8

A tree or not a tree?

Slide9

Tree Terminology (1)

9

M

G

W

P

J

D

N

H

B

S

the

root

of the tree

(no parents)

the

leaves

of the tree

(no children)

child

of M

child

of M

Slide10

Tree Terminology (2)

10

M

G

W

P

J

D

N

H

B

S

descendants

of W

ancestors

of B

Slide11

Tree Terminology (3)

11

subtree

of M

M

G

W

P

J

D

N

H

B

S

Slide12

Tree Terminology (4)

12

A node’s

depth

is the length of the path to the root.

A tree’s (or subtree’s) height is the length of the longest path from the root to a leaf.

depth 3

M

G

W

P

J

D

N

H

B

S

depth 1

height 0

height 2

Slide13

Tree Terminology (5)

13

Multiple trees:

a forest

G

W

P

J

D

N

H

B

S

Slide14

General vs. Binary Trees

14

General tree: every node can have an arbitrary number of childrenBinary tree: at most two children, called left and right…often “tree” means binary tree

General tree

Binary tree

5

4

2

7

8

9

5

4

2

7

9

Demo

Slide15

Binary trees were in A1!

You have seen a binary tree in A1.A PhD object has one or two advisors. (Note: the advisors are the “children”.)

15

David

Gries

Friedrich Bauer

Georg

Aumann

Fritz Bopp

Fritz Sauter

Erwin Fues

Heinrich Tietze

Constantin Carathodory

Slide16

Special kinds of binary trees

16

Max # of nodes at depth d: 2dIf height of tree is h:min # of nodes: h + 1max #of nodes: (Perfect tree)20 + … + 2h = 2h+1 – 1

depth

0

1

2

Height 2,

minimum number of nodes

Height 2,

maximum number of nodes

2

9

0

8

3

5

2

0

5

Complete binary tree

Every level, except last, is completely filled, nodes on bottom level as far left as possible.

No holes

.

Slide17

Trees are recursive

a binary tree

Slide18

Trees are recursive

value

left

subtree

right

subtree

Slide19

Trees are recursive

value

Slide20

Trees are recursive

Binary Tree

Left subtree,which is also a binary tree

Right subtree(also a binary tree)

2

9

0

8

3

5

7

Slide21

Trees are recursive

21

A

binary tree

is either

null

or an object consisting of a value, a left

binary tree

, and a right

binary tree

.

Slide22

22

Base case: If the input is “easy,” just solve the problem directly.Recursive case: Get a smaller part of the input (or several parts). Call the function on the smaller value(s). Use the recursive result to build a solution for the full input.

A Recipe for Recursive Functions

Slide23

A Recipe for Recursive Functions on Binary Trees

23

Base case: If the input is “easy,” just solve the problem directly.Recursive case: Get a smaller part of the input (or several parts). Call the function on the smaller value(s). Use the recursive result to build a solution for the full input.

an empty tree (null), or possibly a leaf

each subtree

Demo

Slide24

Binary Tree

Comparing Searches

Data Structureadd(val v)get(int i)ArrayLinked List

24

2

1

3

0

2

1

3

0

 

 

 

 

contains

(

val

v)

 

 

2

1

3

 

Node could be

anywhere

in tree

Binary search on arrays: O(log n)

Requires invariant: array sorted

…analogue for trees?

Slide25

>5

<5

Binary Search Tree (BST)

25

A

binary search tree

is a binary tree with a

class invariant

:All nodes in the left subtree have values that are less than the value in that node, andAll values in the right subtree are greater. (assume no duplicates)

5

2

8

0

3

7

9

Demo

Slide26

Binary Search Tree (BST)

Contains:Binary tree: two recursive calls: O(n)BST: one recursive call: O(height)

26

5

2

8

0

3

7

9

Slide27

BST Insert

27

To insert a value:Search for valueIf not found, put in tree where search ends

Example:

Insert month names in chronological order as Strings, (Jan, Feb…). BST orders Strings alphabetically (Feb comes

before

Jan, etc.)

Slide28

BST Insert

28

insert: January

Slide29

BST Insert

29

January

insert: February

Slide30

BST Insert

30

January

February

insert: March

Slide31

BST Insert

31

January

February

March

insert: April…

Slide32

BST Insert

32

January

February

March

April

May

June

July

August

September

October

November

December

Slide33

Binary TreeBST

Comparing Data Structures

Data Structureadd(val x)get(int i)ArrayLinked List

33

2

1

3

0

2

1

3

0

 

 

 

 

contains

(

val

x)

 

 

1

2

3

 

2

1

3

 

 

How big could height be?

Slide34

Worst case height

34

April

Insert in alphabetical order…

Slide35

Worst case height

35

April

August

Insert in alphabetical order…

Slide36

Worst case height

36

April

August

December

February

January

Insert in alphabetical order…

Tree degenerates to list!

Slide37

Need Balance

Takeaway: BST search is O(n) timeRecall, big O notation is for worst case running time Worst case for BST is data inserted in sorted orderBalanced binary tree: subtrees of any node are about the same heightIn balanced BST, search is O(log n)Deletion: tricky! Have to maintain balance[Optional] See JavaHyperText “Extensions to BSTs”Also see CS 3110

37