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,
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
AVL Trees1 Part-F2 AVL Trees v z. AVL Trees2 AVL Tree Definition (§ 9.2) AVL trees are balanced. An AVL Tree is a binary search tree such that.
AVL Trees COL 106 Amit Kumar Shweta Agrawal Slide Courtesy : Douglas Wilhelm Harder, MMath, UWaterloo
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
AVL Trees Balancing. The AVL Tree An AVL tree is a balanced binary search tree. What does it mean for a tree to be balanced? It means that for every node.
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,
ECE 250 Algorithms and Data Structures Douglas Wilhelm Harder, M.Math. LEL Department of Electrical and Computer Engineering University of Waterloo Waterloo,
C++ Programming:. Program Design Including
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,
Algorithm design techniques
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,
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,
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.
AVL Trees 1. 2 Outline Background Define balance Maintaining balance within a tree –AVL trees –Difference of heights –Rotations to maintain balance.
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.
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.
CSE 3358 NOTE SET 13 Data Structures and Algorithms.
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,
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.
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 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. BB[  ] trees

2 BB(  ) trees Outline This topic will –Define Null sub-trees Weight Balance –Introduce weight balance –Define bounded-balance BB(  ) trees –Compare weight and height balance

3 BB(  ) trees Background Topic 5.5 Balanced trees discussed various schemes for defining balance in trees –AVL trees are height-balanced Is it possible to consider the ratio of the nodes in each sub-tree? –Ensure that the ratio between the nodes in the left and right sub-trees does not grow too large

4 BB(  ) trees Background In this example, the ratio is 15:7

5 BB(  ) trees Background Here’s a tree that must be considered acceptable, but 100 % of the nodes are in the left sub-tree

Background Thus, we need a slight different metric –We will define null sub-trees –Weight balancing will be based on the number of null sub-trees

7 BB(  ) trees Null sub-tree A null sub-tree as any location where a leaf node may be inserted –This tree with n = 2 nodes has three null sub-trees An empty tree ( n = 0 ) has one null link

8 BB(  ) trees Null sub-tree Theorem –A binary tree with n nodes has n + 1 null sub-tree Proof –True for n = 0 : it has one null sub-tree –Assume it is true for all trees with less than or equal to n nodes –A tree with n + 1 nodes has two sub-trees: As n L + n R = n, it follows n L ≤ n and n R ≤ n By assumption, both sub-trees have n L + 1 and n R + 1 null sub-trees Thus, the total number of null sub-trees is (n L + 1) + (n R + 1) = (n L + n R ) + 2 = n + 2

9 BB(  ) trees Null sub-tree This binary search tree with n = 14 nodes

10 BB(  ) trees Null sub-tree This binary search tree with n = 14 nodes and 15 null sub-trees In our Binary_search_node class, any sub-tree assigned nullptr is represents a null sub-tree

11 BB(  ) trees Weight balance The weight balance  of a tree is the ratio of null sub-trees in the left sub-tree over the total number of null sub-trees For a tree with n nodes double Binary_search_node ::weight_balance() const { if ( empty() ) { return NAN; } double nst_left = static_cast ( left()->size() ); double nst_right = static_cast ( right()->size() ); return nst_left / (nst_left + nst_right); }

12 BB(  ) trees Weight balance Here,  = 10/15 ≈ 0.667

13 BB(  ) trees Weight balance The balance of a tree depends on   ≈ 0 right-heavy node  ≈ 0.5 approximately balanced node  ≈ 1 left-heavy node

14 BB(  ) trees A BB(  ) tree requires that all nodes have bounded weight balance of  ≤  ≤ 1 –  where 0 ≤  ≤ ½

15 BB(  ) trees Is it possible to construct a BB(½) tree? This weight has  = ⅔ Only perfect trees are BB(½) trees –Use proof by induction on the height  = ⅔  = ½

16 BB(  ) trees As with AVL trees, rotations will correct the balance –Insertions with AVL trees require at most one rotation –More than one rotation may be necessary for BB(  ) trees

17 BB(  ) trees By restricting it can be shown that both the height is  (ln(n)) and the number of required rotations an amortized  (1)

18 BB(  ) trees With our restriction, a BB(  ) tree is bounded by It follows that:  = 0.5 : h ≤ lg(n + 1)  = 0.25 :

19 BB(  ) trees With our restriction, any sequence of m insertions into an initially empty BB(  ) tree will require O(m) AVL-like rotations –This gives an amortized  (1) rotations per insertion –If a node becomes unbalanced as a result of an insertion, only one rotation is required to balance it

BB(  ) trees First rotation: Writing  B and  D in terms of  B and  D

BB(  ) trees Second rotation: Writing  B,  D,  F in terms of  B,  D,  F

22 BB(  ) trees As  → 1/3 –The tree is very balanced –Imbalances cannot be corrected with simple AVL-like rotations while recursing back to the root As  → 0 –The tree is unbalanced –The height is O(n)

23 BB(  ) trees Worst-case BB(¼) Trees A worst-case BB(¼) tree  = 1/4

24 BB(  ) trees Worst-case BB(¼) Trees A worst-case BB(¼) tree  = 2/8

25 BB(  ) trees Worst-case BB(¼) Trees A worst-case BB(¼) tree  = 3/12

26 BB(  ) trees Worst-case BB(¼) Trees A worst-case BB(¼) tree  = 4/16

27 BB(  ) trees Weight versus Height Balance Is every AVL tree a BB(  ) tree? –Consider an AVL tree of height h with A worst-case AVL left sub-tree of height h – 2 A perfect right sub-tree of height h – 1

28 BB(  ) trees Weight versus Height Balance A worst-case weight-imbalanced AVL tree with h = 4 and  = 5/21 = < 0.25

29 BB(  ) trees Weight versus Height Balance The number of nodes in –The worst-case AVL tree is  (  h ) –A perfect tree is  (2 h ) Therefore  =  ((  /2) h ) where  /2 ≈ –That is,  → 0 as h → ∞

30 BB(  ) trees Weight versus Height Balance Is every BB(  ) tree an AVL tree? –Consider this BB(1/3) tree

31 BB(  ) trees Summary This topic –Defined null links and weight balance –Introduce weight balance –Define BB(  ) trees –Compared weight and height balance Neither is a subset of the other

32 BB(  ) trees References Roger Whitney, San Diego State University, Lecture Notes

33 BB(  ) 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