Todays announcements: PA1 review Oct 2, 18:15-20:15 ICCS X050 HW2 - - PowerPoint PPT Presentation

today s announcements
SMART_READER_LITE
LIVE PREVIEW

Todays announcements: PA1 review Oct 2, 18:15-20:15 ICCS X050 HW2 - - PowerPoint PPT Presentation

Todays announcements: PA1 review Oct 2, 18:15-20:15 ICCS X050 HW2 due Oct 4, 23:59 MT1 Oct 10, 19:00-21:00 CIRS 1250 Todays Plan Rooted Trees Warm up: What is the maximum number of nodes in a binary tree of height h ? 1 / 9


slide-1
SLIDE 1

Today’s announcements:

◮ PA1 review Oct 2, 18:15-20:15 ICCS X050 ◮ HW2 due Oct 4, 23:59 ◮ MT1 Oct 10, 19:00-21:00 CIRS 1250

Today’s Plan

◮ Rooted Trees

Warm up: What is the maximum number of nodes in a binary tree

  • f height h?

1 / 9

slide-2
SLIDE 2

Restricted branching

L K J I H D E B F G C A

d-ary: every node has at most d children (binary if d = 2) Recursive def: full d-ary: every node has d or 0 children. perfect d-ary: as many nodes as possible for its height (each level

filled in)

complete d-ary: each level except possibly the deepest is filled in, all nodes in the deepest level are as far left1 as possible

1requires ordered tree 2 / 9

slide-3
SLIDE 3

Number of nodes in perfect binary tree of height h

r Ta a b Tb

What is a recursive definition of height of a binary tree? height(T) is: (What is a recursive definition of a binary tree?) Number of nodes N(h) in a perfect binary tree of height h?

3 / 9

slide-4
SLIDE 4

Ordered Binary Trees

C E B J G A F H I K L D ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅ ∅

An ordered binary tree is

◮ empty, or ◮ root with left and right ordered binary subtrees

How many empty trees hang onto an ordered binary tree with n nodes?

4 / 9

slide-5
SLIDE 5

Ordered binary tree ADT

root ∅ dog bee emu ant cat ∅ ∅ ∅ ∅ ∅

Tree ADT

◮ insert ◮ remove ◮ traverse

template<class T> class tree { public: ... private: struct Node { T data; Node * left; Node * right; }; Node * root; ... };

5 / 9

slide-6
SLIDE 6

Traversals

2 9 5 20 15 10 7 30 17

inOrder( Node * x ) { If (x != null) { inOrder(x->left); inOrder(x->right); }} In order: 2, 5, 7, 9, 10, 15, 17, 20, 30

6 / 9

slide-7
SLIDE 7

Euler Tree Tour: The all-in-one traversal

d b i g f c h a e R v L pre post in d b i g f c h a e d b i g f c h a e d b i g f c h a e

7 / 9

slide-8
SLIDE 8

Applications Expression Evaluation

2 − ∧ / lg × 8 2 16

double eval( Node * x ) { If (x != null) { double a = eval(x->left); double b = eval(x->right); }}

Tree Copy Tree Clear

8 / 9

slide-9
SLIDE 9

Level (Depth) order

e b f c a h i d g j

void levelOrder( ) { If( root == NULL) return; }}

9 / 9