Outline Introducing perfect binary trees Definitions and examples

Slides:



Advertisements
Similar presentations
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Advertisements

ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Laplace Transform Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2008 by Douglas Wilhelm Harder.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Topics in Applied Mathematics Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Lecture 5: Linear Time Sorting Shang-Hua Teng. Sorting Input: Array A[1...n], of elements in arbitrary order; array size n Output: Array A[1...n] of the.
CS 253: Algorithms Chapter 8 Sorting in Linear Time Credit: Dr. George Bebis.
Self-Reference - Induction Cmput Lecture 7 Department of Computing Science University of Alberta ©Duane Szafron 1999 Some code in this lecture is.
1 Red-Black Trees. 2 Black-Height of the tree = 4.
Chapter 10 Sequences, Induction, and Probability Copyright © 2014, 2010, 2007 Pearson Education, Inc Mathematical Induction.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Shortest Paths Introduction to Algorithms Shortest Paths CSE 680 Prof. Roger Crawfis.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
WATERLOO ELECTRICAL AND COMPUTER ENGINEERING Intellectual Property 1 WATERLOO ELECTRICAL AND COMPUTER ENGINEERING Intellectual Property Douglas Wilhelm.
Fixed-Point Iteration Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007 by Douglas Wilhelm.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Welcome to ECE 204 Numerical Methods for Computer Engineers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo.
Proof by Induction.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Binary Numbers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007 by Douglas Wilhelm Harder.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Analysis techniques Pasi Fränti Ordo O(g) – Upper Bound f(n) ≤ c∙g(n) Omega  (g)– Lower Bound f(n) ≥ c∙g(n) Theta Θ(g)– Exact limit: c 1 ∙g(n)
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
9.4 Mathematical Induction
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
Problems with Floating-Point Representations Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright.
Decimal Numbers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007 by Douglas Wilhelm Harder.
Mathematical Induction Digital Lesson. Copyright © by Houghton Mifflin Company, Inc. All rights reserved. 2 Mathematical induction is a legitimate method.
Double-Precision Floating-Point Numbers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
CMSC 341 Introduction to Trees. 2/21/20062 Tree ADT Tree definition –A tree is a set of nodes which may be empty –If not empty, then there is a distinguished.
1 Introduction In this course, we will look at: –Algorithms for solving problems efficiently –Data structures for efficiently storing, accessing, and modifying.
Classifications LanguageGrammarAutomaton Regular, right- linear Right-linear, left-linear DFA, NFA Context-free PDA Context- sensitive LBA Recursively.
MATH 212 NE 217 Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada Copyright © 2011.
WATERLOO ELECTRICAL AND COMPUTER ENGINEERING Complaints, Discipline and Enforcement 1 WATERLOO ELECTRICAL AND COMPUTER ENGINEERING Professional Engineers.
1 The tree data structure Outline In this topic, we will cover: –Definition of a tree data structure and its components –Concepts of: Root, internal, and.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Binary search trees Outline This topic covers binary search trees: –Abstract Sorted Lists –Background –Definition and examples –Implementation: Front,
1 N -ary trees This topic quickly looks at a generalization of a binary tree, where each node has up to N children –Definition –Perfect N -ary trees –Complete.
1 Binary trees Outline In this talk, we will look at the binary tree data structure: –Definition –Properties –A few applications Ropes (strings) Expression.
Mathematical Induction. The Principle of Mathematical Induction Let S n be a statement involving the positive integer n. If 1.S 1 is true, and 2.the truth.
Recurrences It continues… Jeff Chastine. Recurrences When an algorithm calls itself recursively, its running time is described by a recurrence. A recurrence.
1 Complete binary trees Outline Introducing complete binary trees –Background –Definitions –Examples –Logarithmic height –Array storage.
1 Red-Black Trees. 2 A Red-Black Tree with NULLs shown Black-Height of the tree = 4.
Outline In this topic, we will: Define a binary min-heap
Outline In this topic we will look at:
Open Addressing: Quadratic Probing
AVL Trees.
Multiway Search Trees.
CS200: Algorithm Analysis
Red-Black Trees.
Trees (Part 1, Theoretical)
Σ 2i = 2 k i=0 CSC 225: Proof of the Day
Mathematical Induction
Tutorial 2 - Daniel Razavi
Introduction to Proofs
Presentation transcript:

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

Definition Standard definition: 5.2.1 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

Definition Recursive definition: 5.2.1 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

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

Theorems 5.2.2 We will now look at four theorems that describe the properties of perfect binary trees: A perfect tree has 2h + 1 – 1 nodes 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

2h + 1 – 1 Nodes Theorem Proof: 5.2.2.1 A perfect binary tree of height h has 2h + 1 – 1 nodes Proof: We will use mathematical induction: Show that it is true for h = 0 Assume it is true for an arbitrary h Show that the truth for h implies the truth for h + 1

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

2h + 1 – 1 Nodes h The inductive step: 5.2.2.1 If the height of the tree is h, then assume that the number of nodes is n = 2h + 1 – 1 h

2h + 1 – 1 Nodes 5.2.2.1 We must show that a tree of height h + 1 has n = 2(h + 1) + 1 – 1 = 2h + 2 – 1 nodes h + 1

2h + 1 – 1 Nodes 5.2.2.1 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

2h + 1 – 1 Nodes Consequently 5.2.2.1 The statement is true for h = 0 and the truth of the statement for an arbitrary h implies the truth of the statement for h + 1. Therefore, by the process of mathematical induction, the statement is true for all h ≥ 0

Logarithmic Height n + 1 = 2h + 1 lg(n + 1) = h + 1 h = lg(n + 1) – 1 5.2.2.2 Theorem A 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 + 1 h = lg(n + 1) – 1

Logarithmic Height 5.2.2.2 Lemma lg(n + 1) – 1 = Q(ln(n)) Proof

2h Leaf Nodes Theorem Proof (by induction): 5.2.2.3 Theorem A 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:

The Average Depth of a Node 5.2.2.4 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 Depth Count 1 1 2 2 4 3 8 4 16 5 32 Sum of the depths Number of nodes

Applications Perfect binary trees are considered to be the ideal case 5.2.3 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

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

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, MMath dwharder@alumni.uwaterloo.ca