You are on page 1of 18

DATA STRUCTURES

Weight balanced binary tree

1
Content
Page
Introduction

Chapter I : Binary Trees Foundation 04

A- Definition 04
B- Types of binary trees 04
C- Binary Tree Traversals 05
D- Glossary 06

Chapter ii : Balanced tree Foundation 07

A- Definition 07
B- Performance of balanced binary tree 08
C- Big-O notation 09
D- Glossary 10

Chapter iii: Weight balanced binary tree 11

A- Definition 11
B- Properties of weight-balanced tree 11
C- Methods of weight-balanced tree 11
D- weight-balanced tree in depth 11
E- Advantages of weight-balanced tree 12
F- Implementation of weight-balanced tree 13

Chapter iv: weight Balanced Binary Tree in depth 15

A- Definitions 15
B- Theorem 16
C- Performance 16

References 17

2
INTRODUCTION

Data structure is a particular way of storing and organizing data in a computer


so that it can be used efficiently.

There are many books describing data structures around the world. Different
kinds of data structures are there, and some are highly specialized to specific
tasks. In Our work we have mission to report information about weight
balanced binary tree. The binary search tree (BST) data structure is
fundamental to computer science. Many variants of BSTs have been devised so
far. Weight BSTs achieve logarithmic worst-case cost by using th size of the
subtrees as balancing information [13].

So let us in this report talk about Weight BST

This report is subdivided in four sequences. Firstly we are going to present


foundation and generalization in the tree structures. The second sequence of
our work will talk about the balanced tree and the two last blocs should give
more information about the main topic of this work.

3
Chapter i: Binary Trees Foundation

A- Definition
A binary tree is a finite set of elements that is either empty or is partitioned
into three disjoint subsets. [1]

- The first subset contains a single element called the root of the tree.
- The other two subsets are themselves binary trees, called the left and right
subtrees of the original tree. A left or right subtree can be empty.

Each element of a tree is called a node of the tree.

A Binary tree
B c
D E
F
G H I
There is a basic operation we can do in the tree: Searching, Insertion,
Deletion, Traversal and Sort. [2]

B- Types of binary trees

 A rooted binary tree 


 A full binary tree [proper binary tree or 2-tree or strictly binary tree]
 A perfect binary tree
 A complete binary tree
 An infinite complete binary tree
 A balanced binary tree *
 A rooted complete binary tree 
 A degenerate tree 

Note that this terminology often varies in the literature, especially with respect
to the meaning "complete" and "full". [2]

4
C- Binary Tree Traversals
A binary tree can be traversed using different algorithms [3]

Let L, V, and R stand for moving left, visiting the node, and moving right.

There are six possible combinations of traversal


lRr, lrR, Rlr, Rrl, rRl, rlR

Adopt convention that we traverse left before right, only 3 traversals


remain
lRr, lrR, Rlr
inorder, postorder, preorder

For better understanding [4]

M is the Root Node


P is the Left Child Node
N is the Right Child Node

+ Pre-Order : MPN
+ In-Order : PMN
+ Post-Order : PNM
+ Level-Order : MPN

1. In-order: Left-Root-Right. Listing method in which the sequence of the nodes


is left most child node, root node followed by remaining child nodes in a left to
right order.

2. Post-order: Left-Right-Root. The sequence of the nodes is child notes in left


to right followed by the root.

3. Pre-order: Root-Left-Right, It employs Depth First Search. The sequence of


the nodes is root node followed by the child nodes taken in left to right order.

4. level-order, where we visit every node on a level before going to a lower


level. This is also called Breadth-first traversal.

5
C- Glossary

Tree - a non-empty collection of nodes & edges


Node - can have a name and carry other associated information
The maximum number of nodes on level i of a binary tree is 2 i-1, i>=1.
The maximum number of nodes in a binary tree of depth k is 2k-1, k>=1.
Path - a list of distinct nodes in which successive nodes are connected by
edges. Any two nodes must have one and only one path between them,
else it is not a tree
A directed edge refers to the link from the parent to the child
Root - starting point (top) of the tree
Parent (ancestor) of a node A - the node "above" node A
Child (descendent) of a node A - the node "below" node A
Siblings - nodes that have same parent
Leaf (terminal node) - a node with no children
Level of a node - the number of edges between this node and the root
Depth of a node - the number of edges from the root to that node.
The depth of the root is 0.
Height of a node - the largest number of edges from that node to a leaf.
The height of each leaf is zero
Height of a tree - the longest path from the root to a leaf node.
The size of a node is the number of descendants it has including itself.
In-degree of a node is the number of edges arriving at that node.
Out-degree of a node is the number of edges leaving that node

6
Chapter ii: Balanced Tree Foundation

A- Definition
A balanced tree is a tree which is balanced - it has roughly the same height on
each of its sub-nodes. A balanced tree will have the lowest possible overall
height [7]. A binary tree where no leaf is more than a certain amount farther
from the root than any other [8]. A balanced tree is a sorted collection of
key/value pairs optimized for searching and traversing in order.

A balanced tree can be divided by two (three) [8]:

Height balanced : AVL trees, 2-3 trees, 2-3-4 trees, B trees


Weight condition : BB[a]-Bäume
(*) Structural conditions : Bruder-, 2-3-, a-b-, B-Bäume

A balanced binary tree is a tree that is explicitly kept balanced. It has a lookup
and insertion complexity of O(log(n)), and an accumulative complexity of
O(n*log(n)). All of them worst case!

It turned out to be faster than the sorted array, by about a factor of 2. For more
(maximum) efficiency, a binary search tree stand be balanced.

7
B- Performance of balanced binary tree [10]
Binary search trees provide O(lg n) performance on average for important
operations such as item insertion, deletion, and search operations. Balanced
trees provide O(lg n) even in the worst case

Cons
ider the binary tree, at each level the number of the nodes is doubled. There
are three levels, and the total number of nodes is:

1 + 2 + 22 + 23 = 24 - 1 = 15

Note that 24 - 1 = 2*23 – 1 | also that 3 = log(23)

In general, if we have a tree with M levels, the number of the nodes would be:

1 + 2 + 22 + …. 2M = 2(M+1) - 1 = 2*2M – 1

Let N be the number of the nodes.

We will find now how M (the depth of the tree) depends on the number of the
nodes.
N = 2*2M – 1 --- > 2*2M = N + 1 --- >2M = (N+1)/2
M = log((N+1)/2) = O(logN)

Thus, given a balanced binary tree with N nodes, the height of the tree is
O( log(N)). Given a balanced binary tree with M levels, the number of the
nodes is O(2M)

Average height of a binary tree : O(sqrt(N)) - computed by considering all


possible cases of a binary tree with N nodes.
Worst case: when the tree is a list - all nodes except the leaf have only one
child: height: N-1.

8
9
C- Big-O notation

Definition: A theoretical measure of the execution of an algorithm, usually the


time or memory needed, given the problem size n, which is usually the number
of items. Informally, saying some equation f(n) = O(g(n)) means it is less than
some constant multiple of g(n). The notation is read, "f of n is big oh of g of n".

Formal Definition: f(n) = O(g(n)) means there are positive constants c and k,
such that 0 ≤ f(n) ≤ cg(n) for all n ≥ k. The values of c and k must be fixed for the
function f and must not depend on n. [16]

The importance of this measure can be seen in trying to decide whether an


algorithm is adequate, but may just need a better implementation, or the
algorithm will always be too slow on a big enough input. For instance,
quicksort, which is O(n log n) on average, running on a small desktop computer
can beat bubble sort, which is O(n²), running on a supercomputer if there are a
lot of numbers to sort. To sort 1,000,000 numbers, the quicksort takes
20,000,000 steps on average, while the bubble sort takes 1,000,000,000,000
steps! [17]

10
D- Glossary
Binary Search Tree – is binary tree and the following additional property:

Given a node T, each node to the left is “smaller” than T


And each node to the right is “large”

Some examples of search-tree data structures are [12] :

Red-black trees and splay trees, both of which are instances of self-
balancing binary search trees;
Ternary search trees, in which each internal node has exactly three
children;
B trees, commonly used in databases;
B+ trees, like B trees but with all data values stored in the leaves;
van Emde Boas trees, very efficient if the data values are fixed-size
integers.

(a) is not a BST. (b) and (c) are BSTs

11
Chapter iii: weight Balanced Binary Tree

A- Definition
A weight-balanced binary tree is a binary tree which is balanced based on
knowledge of the probabilities of searching for each individual node. Within
each subtree, the node with the highest weight appears at the root. This can
result in more efficient searching performance [11].
Also known as BB(α) tree [18]

B- Properties of weight-balanced tree


[13]Because the tree is weight-balanced, the distances between any node and
each of the leaf node descendents of that node are equal. So, for any leaf
nodes xy

C- Methods of weight-balanced tree


Weight balanced tree is balanced binary search trees because their “weight” is
used as the criterion for balancing.

The weight of the tree is defined as the number of external nodes in the tree
(this equals the number of null pointer in the tree) [1]. If the ratio of the weight
of the left subtree of every node to the weight of the subtre rooed at the node
is between some fraction a and a-1, the tree is a weight balanced tree of ratio a
or is said to be in the class wb[a].

12
D- Advantages of weight-balanced tree
A weight-balanced binary tree has several advantages over the other data
structures for large aggregates:

In addition to the usual element-level operations like insertion, deletion


and lookup, there is a full complement of collection-level operations, like
set intersection, set union and subset test, all of which are implemented
with good orders of growth in time and space. This makes weight-
balanced trees ideal for rapid prototyping of functionally derived
specifications.

An element in a tree may be indexed by its position under the ordering


of the keys, and the ordinal position of an element may be determined,
both with reasonable efficiency.

Operations to find and remove minimum element make weight-balanced


trees simple to use for priority queues.

The implementation is functional rather than imperative. This means


that operations like `inserting' an association in a tree do not destroy the
old tree, in much the same way that (+ 1 x) modifies neither the constant
1 nor the value bound to x. The trees are referentially transparent thus
the programmer need not worry about copying the trees. Referential
transparency allows space efficiency to be achieved by sharing subtrees.

These features make weight-balanced trees suitable for a wide range of


applications, especially those that require large numbers of sets or discrete
maps. Applications that have a few global databases and/or concentrate on
element-level operations like insertion and lookup are probably better off using
hash tables or red-black trees. A weight-balanced tree takes space that is
proportional to the number of associations in the tree.

E- Implementation of weight-balanced tree

13
The following table shows a simple BB[alpha] tree (with alpha=1/4) and the
balances of the nodes. [19]

1. TREE TRAVERSAL refers to the process of visiting (examining and/or


updating) each node in a tree data structure.

Algorithm traversal

Parameter : root and key


Initialization :
r receive the root
L receive the number of leaves
n receive r.key , r.left and r.right
l determine by floor((1-a)*L)
Work: Repeat of these instruction
o If r is null return null
o Else if key = n.key then return n
o Else if key < n.key then r receive n.left
o Else r receive n.right
Notes
o we obtain a simpler remainder function
W(pl) <= (1 - alpha) W(p)
W(pr) <= (1 - alpha) W(p)
(W(p) denotes the number of leaves of p, p is a node, pl and pr its left and right
child, respectively.)
2. TREE DELETION can be accomplished using the method: checking for the
separate cases with no children, two children, or one.

Algorithm delete

Parameter : root and key


Initialization :
t receive the root
Work:
 Progressing in the tree

14
o If t is null return null
o Else if t.key < key then return t.right receive delete (t.right,key)
o Else if t.key > key then return t.left receive delete(t.left,key)
 Deletion if descendant is null
o Else if t.left = null then t receive t.right
o Else if t.right = null then t receive t.left
 Deletion if descendant is not null
o Else if wt(t.left)>wt(t.right) then
[t=rrot(t) ; t.right = delete(t.right,key)]
o Else [t=lrot(t) ; t.left = delete(t.left,key)]
 Reconstruct weight information
o If t not null then
o t.weight receive wt(t.left)+wt(t.right)
o check(t)
 Return(t) // end of program
Notes
o wt is function wich give the weight of node
o rrot and lrot is respectly right and left rotation
o check controlled if the t is weight balanced

We have try to write essential implementation of weight balanced binary tree


in this section, you can find other function for adaptation in WBBT here:
a)http://www.cse.iitb.ac.in/~as/mit-scheme/scheme_12.html#SEC127
b) http://www-users.aston.ac.uk/~beaumoaj/AJBcs124/DSA/Page2.3.html

Chapter iv: weight Balanced Binary Tree in depth

Let Alpha be a real such that 1/4 < Alpha <= 1 - 


Let T be a binary search tree 
Let Tl (Tr ) be the left (right) subtree of the root of T

Definitions

The root balance of T, Rho , is given by:


Rho(T) = | Tl | / |T | = 1 - | Tr | / | T |
where | T | = number of leaves of tree T

A tree T is of bounded balance Alpha , 


if for every subtree T' of T we have:
Alpha <= Rho(T' ) <= 1 - Alpha

BB[ Alpha ] is the set of all trees of bounded balance Alpha

15
Example of BB[ Alpha ] tree Alpha <= 1/3

Nod Root balance


e
a
½
b 2/5
c 5/14
d 2/3

Let bi denote the depth of node xi in tree T.


The average path length of T is given by:

Theorem

Let T be a BB[ Alpha ] with n nodes. Then 

a) 

b) 

if   then theorem 2 states P <= 1.15(1 + 1/n) lg( n+1) - 1


and height(T) <= 2 lg( n +1 ) – 1

Performance

Lemma
Let  .

16
Let T' be a BB[ Alpha ] tree.
Let T be result of adding (deleting) a node from T'.
Assume that Tl and Tr are BB[ Alpha ] trees.
Assume that T is no longer a BB[ Alpha ] tree.
Then a single or double rotation will balance T

Theorem.
Let  . Then there is a constant c such that the total number of
single and double rotations required in a sequence of M insertions and deletions
into an empty BB[ Alpha ] tree is <= cM
For = 3/11 the prove gives the value of c = 19.59

Experiments suggest that c is near 1

Theorem.
Let  . Then insert, access, delete takes O(N) in a BB[ Alpha ]
tree with N leaves

It has been shown that the cost W of a weight balanced binary tree
satisfies the inequalities, H 5 W 5 H + 3, where H is the entropy of the set
of the leaves. For a class of “smooth” distributions the inequalities, H 5 W
5 H + 2, are derived. These results imply that for sets with large entropy
the search times provided by such trees cannot be substantially shortened
when binary decisions are being used.

J. Rissanen: Bounds for Weight Balanced Trees


17
References and Books reviews
[1] Data Structure Using C and C++ 2nd Edition; ISBN 81-317-0328-2

[3] University of North Texas , CSCE 3110 Data Structures & Algorithm Analysis

[14] Abstract, Salvador Roma, http://www.springerlink.com/content/d9191wje6j4rqr4b/

[15] Handbook of DATA STRUCTURES and APPLICATIONS; Edited by Dinesh P. Mehta


Colorado School of Mines Golden and Sartaj Sahni University of Florida Gainesville

[17] Jon Bentley, Programming Pearls: Algorithm Design Techniques, CACM, 27(9):868,
September 1984 for an example of a microcomputer running BASIC beating a
supercomputer running FORTRAN.

[2] http://en.wikipedia.org/wiki/Binary_tree

[4] http://www.laynetworks.com/cs04.htm

[5] http://sites.google.com/site/sumedhshende/binarytrees

[6]http://en.wikipedia.org/wiki/Binary_search_tree

[7] http://wiki.answers.com/Q/What_is_a_balanced_tree_in_data_structures

[8]: http://www.csie.ntu.edu.tw/~wcchen/algorithm/balanceTree/balanced.htm

[9] http://www.itl.nist.gov/div897/sqg/dads/HTML/balancedbitr.html

[10] http://faculty.simpson.edu/lydia.sinapova/www/cmsc250/LN250_Weiss/L07-
Trees.htm

[11] http://en.wikipedia.org/wiki/Weight-balanced_tree

[12] http://en.wikipedia.org/wiki/Search_tree

[13]
http://planetmath.org/encyclopedia/WeightBalancedBinaryTreesAreUltrametric.html

[16] http://www.itl.nist.gov/div897/sqg/dads/HTML/bigOnotation.html

[18] http://www.itl.nist.gov/div897/sqg/dads/HTML/bbalphatree.html

[19] http://www.auto.tuwien.ac.at/~blieb/woop/bbalpha.html

[20] http://www.eli.sdsu.edu/courses/fall95/cs660/notes/BB/BBtrees.html

18

You might also like