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

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,
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,
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,
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,
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,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
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,
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,
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,
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,
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,
Problems with Floating-Point Representations Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright.
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,
Decimal Numbers Douglas Wilhelm Harder Department of Electrical and Computer Engineering University of Waterloo Copyright © 2007 by Douglas Wilhelm Harder.
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,
Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca.
Heaps & Priority Queues
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Introduction In this course, we will look at: –Algorithms for solving problems efficiently –Data structures for efficiently storing, accessing, and modifying.
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,
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 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,
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.
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
1 Complete binary trees Outline Introducing complete binary trees –Background –Definitions –Examples –Logarithmic height –Array storage.
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.
Outline Introducing perfect binary trees Definitions and examples
Multiway Search Trees.
Presentation transcript:

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 © by Douglas Wilhelm Harder. Some rights reserved. Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo, Ontario, Canada ece.uwaterloo.ca © by Douglas Wilhelm Harder. Some rights reserved. Complete binary trees

2 Outline Introducing complete binary trees –Background –Definitions –Examples –Logarithmic height –Array storage

3 Complete binary trees Background A perfect binary tree has ideal properties but restricted in the number of nodes: n = 2 h – 1 1, 3, 7, 15, 31, 63, 127, 255, 511, 1023,.... We require binary trees which are –Similar to perfect binary trees, but –Defined for all n 5.3

4 Complete binary trees Definition A complete binary tree filled at each depth from left to right: 5.3.1

5 Complete binary trees Definition The order is identical to that of a breadth-first traversal 5.3.1

6 Complete binary trees Recursive Definition Recursive definition: a binary tree with a single node is a complete binary tree of height h = 0 and a complete binary tree of height h is a tree where either: –The left sub-tree is a complete tree of height h – 1 and the right sub- tree is a perfect tree of height h – 2, or –The left sub-tree is perfect tree with height h – 1 and the right sub-tree is complete tree with height h –

7 Complete binary trees Height Theorem The height of a complete binary tree with n nodes is h = ⌊ lg(n) ⌋ Proof: –Base case: When n = 1 then ⌊ lg(1) ⌋ = 0 and a tree with one node is a complete tree with height h = 0 –Inductive step: Assume that a complete tree with n nodes has height ⌊ lg(n) ⌋ Must show that ⌊ lg(n + 1) ⌋ gives the height of a complete tree with n + 1 nodes Two cases: –If the tree with n nodes is perfect, and –If the tree with n nodes is complete but not perfect 5.3.2

8 Complete binary trees Height Case 1 (the tree is perfect): –If it is a perfect tree then Adding one more node must increase the height –Before the insertion, it had n = 2 h + 1 – 1 nodes: –Thus, –However, 5.3.2

9 Complete binary trees Height Case 2 (the tree is complete but not perfect): –If it is not a perfect tree then –Consequently, the height is unchanged: ⌊ lg( n + 1 ) ⌋ = h By mathematical induction, the statement must be true for all n ≥

10 Complete binary trees Array storage We are able to store a complete tree as an array –Traverse the tree in breadth-first order, placing the entries into the array 5.3.3

11 Complete binary trees Array storage We can store this in an array after a quick traversal: 5.3.3

12 Complete binary trees Array storage To insert another node while maintaining the complete-binary-tree structure, we must insert into the next array location 5.3.3

13 Complete binary trees Array storage To remove a node while keeping the complete-tree structure, we must remove the last element in the array 5.3.3

14 Complete binary trees Leaving the first entry blank yields a bonus: –The children of the node with index k are in 2k and 2k + 1 –The parent of node with index k is in k ÷ 2 Array storage

15 Complete binary trees Leaving the first entry blank yields a bonus: –In C++, this simplifies the calculations: Array storage parent = k >> 1; left_child = k << 1; right_child = left_child | 1;

16 Complete binary trees Array storage For example, node 10 has index 5: –Its children 13 and 23 have indices 10 and 11, respectively

17 Complete binary trees Array storage For example, node 10 has index 5: –Its children 13 and 23 have indices 10 and 11, respectively –Its parent is node 9 with index 5/2 =

18 Complete binary trees Array storage Question: why not store any tree as an array using breadth-first traversals? –There is a significant potential for a lot of wasted memory Consider this tree with 12 nodes would require an array of size 32 –Adding a child to node K doubles the required memory 5.3.4

19 Complete binary trees Array storage In the worst case, an exponential amount of memory is required These nodes would be stored in entries 1, 3, 6, 13, 26, 52,

20 Complete binary trees Summary In this topic, we have covered the concept of a complete binary tree: –A useful relaxation of the concept of a perfect binary tree –It has a compact array representation

21 Complete 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, MMath