BB[ a ] trees

33
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. BB[a] trees

description

BB[ a ] trees. Outline. This topic will Define Null sub-trees W eight Balance Introduce weight balance Define bounded-balance BB( a ) trees Compare weight and height balance. Background. Topic 5.5 Balanced trees discussed various schemes for defining balance in trees - PowerPoint PPT Presentation

Transcript of BB[ a ] trees

Page 1: BB[ a ] trees

ECE 250 Algorithms and Data Structures

Douglas Wilhelm Harder, M.Math. LELDepartment 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.

BB[a] trees

Page 2: BB[ a ] trees

2

BB(a) trees

Outline

This topic will– Define

• Null sub-trees• Weight Balance

– Introduce weight balance– Define bounded-balance BB(a) trees– Compare weight and height balance

Page 3: BB[ a ] trees

3

BB(a) 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

Page 4: BB[ a ] trees

4

BB(a) trees

Background

In this example, the ratio is 15:7

Page 5: BB[ a ] trees

5

BB(a) trees

Background

Here’s a tree that must be considered acceptable, but 100 % of the nodes are in the left sub-tree

Page 6: BB[ a ] trees

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

Page 7: BB[ a ] trees

7

BB(a) 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

Page 8: BB[ a ] trees

8

BB(a) 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 nL + nR = n, it follows nL ≤ n and nR ≤ n

• By assumption, both sub-trees have nL + 1 and nR + 1 null sub-trees

• Thus, the total number of null sub-trees is (nL + 1) + (nR + 1) = (nL + nR) + 2 = n + 2

Page 9: BB[ a ] trees

9

BB(a) trees

Null sub-tree

This binary search tree with n = 14 nodes

Page 10: BB[ a ] trees

10

BB(a) 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

Page 11: BB[ a ] trees

11

BB(a) trees

Weight balance

The weight balance r 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<Type>::weight_balance() const { if ( empty() ) { return NAN; }

double nst_left = static_cast<double>( left()->size() + 1.0 ); double nst_right = static_cast<double>( right()->size() + 1.0 );

return nst_left / (nst_left + nst_right);}

11

1

n

n

n

Page 12: BB[ a ] trees

12

BB(a) trees

Weight balance

Here, r = 10/15 ≈ 0.667

Page 13: BB[ a ] trees

13

BB(a) trees

Weight balance

The balance of a tree depends on r r ≈ 0 right-heavy node

r ≈ 0.5 approximately balanced node

r ≈ 1 left-heavy node

Page 14: BB[ a ] trees

14

BB(a) trees

BB(a) trees

A BB(a) tree requires that all nodes have bounded weight balance of

a ≤ r ≤ 1 – awhere 0 ≤ a ≤ ½

Page 15: BB[ a ] trees

15

BB(a) trees

BB(a) trees

Is it possible to construct a BB(½) tree?

This weight has r = ⅔

Only perfect trees are BB(½) trees– Use proof by induction on the height

r = ⅔

r = ½

Page 16: BB[ a ] trees

16

BB(a) trees

BB(a) 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(a) trees

Page 17: BB[ a ] trees

17

BB(a) trees

BB(a) trees

By restricting

it can be shown that both the height is Q(ln(n)) and the number of required rotations an amortized Q(1)

1 10.25 1 0.2929

4 2

Page 18: BB[ a ] trees

18

BB(a) trees

BB(a) trees

With our restriction, a BB(a) tree is bounded by

It follows that:

a = 0.5: h ≤ lg(n + 1)

a = 0.25:

1

1

1log ( 1) 1

1lg

1

h n

4 43 34

3

1log ( 1) 1 log ( 1) 1.41

lgh n n

Page 19: BB[ a ] trees

19

BB(a) trees

BB(a) trees

With our restriction, any sequence of m insertions into an initially empty BB(a) tree will require O(m) AVL-like rotations– This gives an amortized Q(1) rotations per insertion– If a node becomes unbalanced as a result of an insertion, only one

rotation is required to balance it

Page 20: BB[ a ] trees

BB(a) trees

First rotation:

Writing sB and sD in terms of rB and rD

eca

a

B

ec

c

D

eca

ca

D

ca

a

B

DDBBB DDBB

BD

Page 21: BB[ a ] trees

BB(a) trees

Second rotation:

Writing sB, sD , sF in terms of rB, rD , rF

geca

a

B

ec

c

D

geca

ca

D

ca

a

B

ge

e

Fgec

ec

F

FDBFDB

BB

FDBFDBD

1FD

FFDF

Page 22: BB[ a ] trees

22

BB(a) trees

BB(a) trees

As a → 1/3– The tree is very balanced– Imbalances cannot be corrected with simple AVL-like rotations while

recursing back to the root

As a → 0– The tree is unbalanced– The height is O(n)

Page 23: BB[ a ] trees

23

BB(a) trees

Worst-case BB(¼) Trees

A worst-case BB(¼) tree

r = 1/4

Page 24: BB[ a ] trees

24

BB(a) trees

Worst-case BB(¼) Trees

A worst-case BB(¼) tree

r = 2/8

Page 25: BB[ a ] trees

25

BB(a) trees

Worst-case BB(¼) Trees

A worst-case BB(¼) tree

r = 3/12

Page 26: BB[ a ] trees

26

BB(a) trees

Worst-case BB(¼) Trees

A worst-case BB(¼) tree

r = 4/16

Page 27: BB[ a ] trees

27

BB(a) trees

Weight versus Height Balance

Is every AVL tree a BB(a) 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

Page 28: BB[ a ] trees

28

BB(a) trees

Weight versus Height Balance

A worst-case weight-imbalanced AVL tree with

h = 4 and r = 5/21 = 0.238 < 0.25

Page 29: BB[ a ] trees

29

BB(a) trees

Weight versus Height Balance

The number of nodes in– The worst-case AVL tree is Q(fh)– A perfect tree is Q(2h)

Therefore r = Q((f/2)h) where f/2 ≈ 0.809– That is, r → 0 as h → ∞

Page 30: BB[ a ] trees

30

BB(a) trees

Weight versus Height Balance

Is every BB(a) tree an AVL tree?– Consider this BB(1/3) tree

Page 31: BB[ a ] trees

31

BB(a) trees

Summary

This topic – Defined null links and weight balance– Introduce weight balance– Define BB(a) trees– Compared weight and height balance

• Neither is a subset of the other

Page 32: BB[ a ] trees

32

BB(a) trees

References

Roger Whitney, San Diego State University, Lecture Noteshttp://www.eli.sdsu.edu/courses/fall95/cs660/notes/BB/BBtrees.html

Page 33: BB[ a ] trees

33

BB(a) 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

[email protected]