TREES Lecture 11 CS2110 Summer 2019 Announcements 2 Confusion - - PowerPoint PPT Presentation

trees
SMART_READER_LITE
LIVE PREVIEW

TREES Lecture 11 CS2110 Summer 2019 Announcements 2 Confusion - - PowerPoint PPT Presentation

TREES Lecture 11 CS2110 Summer 2019 Announcements 2 Confusion about submission due dates/times can be cleared by reading the syllabus (https://courses.cs.cornell.edu/cs2110/2019su/syll abus.html). Remember to make groups before


slide-1
SLIDE 1

TREES

Lecture 11 CS2110 – Summer 2019

slide-2
SLIDE 2

Announcements

§

Confusion about submission due dates/times can be cleared by reading the syllabus (https://courses.cs.cornell.edu/cs2110/2019su/syll abus.html).

§

Remember to make groups before submission deadline.

§

Grades have been released for Assignment 1 and Discussions 1-3. Please submit (private) questions about either of these on Piazza.

2

slide-3
SLIDE 3

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

slide-4
SLIDE 4

Data Structures

¨ Data structure

¤ Organization or format for storing or managing data ¤ Concrete realization of an abstract data type

¨ Operations

¤ Always a tradeoff: some operations more efficient,

some less, for any data structure

¤ Choose efficient data structure for operations of

concern

4

slide-5
SLIDE 5

Example Data Structures

Data Structure add(val v) get(int i) Array Linked List

5

2 1 3 0

2 1 3

𝑃(𝑜) 𝑃(1) 𝑃(𝑜) 𝑃(1) add(v): append v get(i): return element at position i contains(v): return true if contains v

contains(val v)

𝑃(𝑜) 𝑃(𝑜)

slide-6
SLIDE 6

Tree

6

Singly linked list:

2 1 1

Node

  • bject

pointer int value

Today: trees!

4 1 1 2 1 1

slide-7
SLIDE 7

Trees

7

In CS, we draw trees “upside down”

slide-8
SLIDE 8

Tree Overview

8

Tree: data structure with nodes, similar to linked list

¤ Each node may have zero or

more successors (children)

¤ Each node has exactly one

predecessor (parent) except the root, which has none

¤ All 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?

slide-9
SLIDE 9

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

slide-10
SLIDE 10

Tree Terminology (2)

10

M G W P J D N H B S

descendants

  • f W

ancestors of B

slide-11
SLIDE 11

Tree Terminology (3)

11

subtree of M

M G W P J D N H B S

slide-12
SLIDE 12

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

slide-13
SLIDE 13

Tree Terminology (5)

13

Multiple trees: a forest

G W P J D N H B S

slide-14
SLIDE 14

General vs. Binary Trees

14

General tree: every node can have an arbitrary number of children Binary 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

slide-15
SLIDE 15

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

slide-16
SLIDE 16

Special kinds of binary trees

16

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

depth 1 2 Height 2, minimum number of nodes Height 2, maximum number of nodes

2 9 8 3 5 2 5 Complete binary tree Every level, except last, is completely filled, nodes on bottom level as far left as possible. No holes.

slide-17
SLIDE 17

Trees are recursive

a binary tree

slide-18
SLIDE 18

Trees are recursive

value left subtree right subtree

slide-19
SLIDE 19

Trees are recursive

value

slide-20
SLIDE 20

Trees are recursive

Binary Tree

Left subtree, which is also a binary tree Right subtree (also a binary tree) 2 9 8 3 5 7

slide-21
SLIDE 21

Trees are recursive

21

A binary tree is either null

  • r an object consisting of a value, a left binary tree, and a right

binary tree.

slide-22
SLIDE 22

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

slide-23
SLIDE 23

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

slide-24
SLIDE 24

Binary Tree

Comparing Searches

Data Structure add(val v) get(int i) Array Linked List

24

2 1 3 0

2 1 3

𝑃(𝑜) 𝑃(1) 𝑃(𝑜) 𝑃(1)

contains(val v)

𝑃(𝑜) 𝑃(𝑜)

2 1 3

𝑃(𝑜) Node could be anywhere in tree

slide-25
SLIDE 25

>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, and

  • All values in the right subtree are greater.

(assume no duplicates) 5 2 8 3 7 9 Demo

slide-26
SLIDE 26

Binary Search Tree (BST)

Contains:

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

26

5 2 8 3 7 9

slide-27
SLIDE 27

BST Insert

27

To insert a value:

¤Search for value ¤If 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.)

slide-28
SLIDE 28

BST Insert

28

insert: January

slide-29
SLIDE 29

BST Insert

29

January

insert: February

slide-30
SLIDE 30

BST Insert

30

January February

insert: March

slide-31
SLIDE 31

BST Insert

31

January February March

insert: April…

slide-32
SLIDE 32

BST Insert

32

January February March April May June July August September October November December

slide-33
SLIDE 33

Binary Tree BST

Comparing Data Structures

Data Structure add(val x) get(int i) Array Linked List

33

2 1 3 0

2 1 3

𝑃(𝑜) 𝑃(1) 𝑃(𝑜) 𝑃(1)

contains(val x)

𝑃(𝑜) 𝑃(𝑜)

1 2 3

𝑃(𝑜)

2 1 3

𝑃(ℎ𝑓𝑗𝑕ℎ𝑢) 𝑃(ℎ𝑓𝑗𝑕ℎ𝑢) How big could height be?

slide-34
SLIDE 34

Worst case height

34

April Insert in alphabetical order…

slide-35
SLIDE 35

Worst case height

35

April August Insert in alphabetical order…

slide-36
SLIDE 36

Worst case height

36

April August December February January Insert in alphabetical order… Tree degenerates to list!

slide-37
SLIDE 37

Need Balance

¨ Takeaway: BST search is O(n) time

¤ Recall, big O notation is for worst case running time ¤ Worst case for BST is data inserted in sorted order

¨ Balanced binary tree: subtrees of any node are

about the same height

¤ In balanced BST, search is O(log n) ¤ Deletion: tricky! Have to maintain balance ¤ [Optional] See JavaHyperText “Extensions to BSTs” ¤ Also see CS 3110

37