perfect binary trees

20
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca [email protected] © 2006-2013 by Douglas Wilhelm Harder. Some rights reserved. Perfect binary trees

Upload: jewel

Post on 24-Feb-2016

52 views

Category:

Documents


0 download

DESCRIPTION

Perfect binary trees. Outline. Introducing perfect binary trees Definitions and examples Number of nodes: 2 h + 1 – 1 Logarithmic height Number of leaf nodes: 2 h Applications. Definition. 5.2.1. Standard definition: A perfect binary tree of height h is a binary tree where - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Perfect binary  trees

ECE 250 Algorithms and Data Structures

Douglas Wilhelm Harder, M.Math. LELDepartment of Electrical and Computer EngineeringUniversity of WaterlooWaterloo, Ontario, Canada

[email protected]

© 2006-2013 by Douglas Wilhelm Harder. Some rights reserved.

Perfect binary trees

Page 2: Perfect binary  trees

2Perfect binary trees

Outline

Introducing perfect binary trees– Definitions and examples– Number of nodes: 2h + 1 – 1– Logarithmic height– Number of leaf nodes: 2h

– Applications

Page 3: Perfect binary  trees

3Perfect binary trees

Definition

Standard definition:– A perfect binary tree of height h is a binary tree where

• All leaf nodes have the same depth h• All other nodes are full

5.2.1

Page 4: Perfect binary  trees

4Perfect binary trees

Definition

Recursive definition:– A binary tree of height h = 0 is perfect– A binary tree with height h > 0 is a perfect if both sub-trees are prefect

binary trees of height h – 1

5.2.1

Page 5: Perfect binary  trees

5Perfect binary trees

Examples

Perfect binary trees of height h = 0, 1, 2, 3 and 4

5.2.1

Page 6: Perfect binary  trees

6Perfect binary trees

Examples

Perfect binary trees of height h = 3 and h = 4– Note they’re the wrong-way up…

Page 7: Perfect binary  trees

7Perfect binary trees

Theorems

We will now look at four theorems that describe the properties of perfect binary trees:– A perfect tree has 2h + 1 – 1 – The height is Q(ln(n))– There are 2h leaf nodes– The average depth of a node is Q(ln(n))

The results of these theorems will allow us to determine the optimal run-time properties of operations on binary trees

5.2.2

Page 8: Perfect binary  trees

8Perfect binary trees

2h + 1 – 1 Nodes

TheoremA perfect binary tree of height h has 2h + 1 – 1 nodes

Proof:We will use mathematical induction:

1. Show that it is true for h = 02. Assume it is true for an arbitrary h3. Show that the truth for h implies the truth for h + 1

5.2.2.1

Page 9: Perfect binary  trees

9Perfect binary trees

2h + 1 – 1 Nodes

The base case:– When h = 0 we have a single node n = 1– The formula is correct: 20 + 1 – 1 = 1

5.2.2.1

Page 10: Perfect binary  trees

10Perfect binary trees

2h + 1 – 1 Nodes

The inductive step:– If the height of the tree is h, then assume that the number of nodes is

n = 2h + 1 – 1

h

5.2.2.1

Page 11: Perfect binary  trees

11Perfect binary trees

2h + 1 – 1 Nodes

We must show that a tree of height h + 1 hasn = 2(h + 1) + 1 – 1 = 2h + 2 – 1 nodes

h + 1

5.2.2.1

Page 12: Perfect binary  trees

12Perfect binary trees

2h + 1 – 1 Nodes

Using the recursive definition, both sub-trees are perfect trees of height h– By assumption, each sub-tree has 2h + 1 – 1 nodes– Therefore the total number of nodes is

(2h + 1 – 1) + 1 + (2h + 1 – 1) = 2h + 2 – 1

h h

5.2.2.1

Page 13: Perfect binary  trees

13Perfect binary trees

2h + 1 – 1 Nodes

ConsequentlyThe statement is true for h = 0 and the truth of the statement for anarbitrary h implies the truth of the statement for h + 1.

Therefore, by the process of mathematical induction, the statementis true for all h ≥ 0

5.2.2.1

Page 14: Perfect binary  trees

14Perfect binary trees

Logarithmic Height

TheoremA perfect binary tree with n nodes has height lg(n + 1) – 1

Proof

Solving n = 2h + 1 – 1 for h:

n + 1 = 2h + 1

lg(n + 1) = h + 1h = lg(n + 1) – 1

5.2.2.2

Page 15: Perfect binary  trees

15Perfect binary trees

Logarithmic Height

Lemmalg(n + 1) – 1 = Q(ln(n))

Proof

11 ln(2)lg( 1) 1 1 1lim lim lim lim1ln( ) 1 ln(2) ln(2) ln(2)n n n n

nn nn n

n

5.2.2.2

Page 16: Perfect binary  trees

16Perfect binary trees

2h Leaf Nodes

TheoremA perfect binary tree with height h has 2h leaf nodes

Proof (by induction):When h = 0, there is 20 = 1 leaf node.Assume that a perfect binary tree of height h has 2h leaf nodes and observe that both sub-trees of a perfect binary tree of height h + 1 have 2h leaf nodes.

Consequence: Over half all nodes are leaf nodes:

1

2 12 1 2

h

h

5.2.2.3

Page 17: Perfect binary  trees

17Perfect binary trees

The Average Depth of a Node

Consequence:– 50-50 chance that a randomly selected node is a leaf node

The average depth of a node in a perfect binary tree is

1 1 1 10

1 1 1

1

22 2 2 (2 1) (2 1) 1

2 1 2 1 2 111 1 ln( )

2 1

hk

h h h hk

h h h

h

kh h h

hh h n

Q

0

21

345

1

42

81632

Depth Count

Number of nodes

Sum of thedepths

5.2.2.4

Page 18: Perfect binary  trees

18Perfect binary trees

Applications

Perfect binary trees are considered to be the ideal case– The height and average depth are both Q(ln(n))

We will attempt to find trees which are as close as possible to perfect binary trees

5.2.3

Page 19: Perfect binary  trees

19Perfect binary trees

Summary

We have defined perfect binary trees and discussed:– The number of nodes: n = 2h + 1 – 1– The height: lg(n + 1) – 1– The number of leaves: 2h

– Half the nodes are leaves• Average depth is Q(ln(n))

– It is an ideal case

Page 20: Perfect binary  trees

20Perfect binary trees

Usage Notes

• These slides are made publicly available on the web for anyone to use

• If you choose to use them, or a part thereof, for a course at another institution, I ask only three things:– that you inform me that you are using the slides,– that you acknowledge my work, and– that you alert me of any mistakes which I made or changes which you

make, and allow me the option of incorporating such changes (with an acknowledgment) in my set of slides

Sincerely,Douglas Wilhelm Harder, [email protected]