Professional Documents
Culture Documents
Ads-Unit Ii
Ads-Unit Ii
BALANCED TREES
Introduction:
Tree:
A Tree consists of a finite set of elements, called nodes, and set of directed lines called
branches, that connect the nodes.
The no. of branches associated with a node is the degree of the node.
i.e. in - degree and out - degree.
Binary tree:
A binary tree is a tree in which no node can have more than two sub trees designated as the
left sub tree and the right sub tree.
Balance factor:
The balance factor of a binary tree is the difference in height between its left and right sub
trees. i.e. Balance factor = HL - HR
In a balanced binary tree, the height of its sub trees differs by no more than one (its balance
factor is -1, 0, +1) and also its sub trees are also balanced.
In the design of the linear list structure, we had two choices: an array or a linked list
The array structure provides a very efficient search algorithm, but its insertion and
deletion algorithm are very inefficient.
The linked list structure provides efficient insertion and deletion, but its search
algorithm is very inefficient.
What we need is a structure that provides an efficient search, at the same time efficient
insertion and deletion algorithms.
The binary search tree and the AVL tree provide that structure.
A binary search tree (BST) is a binary tree with the following properties:
All items in the left sub tree are less than the root.
All items in the right sub tree are greater than or equal to the root.
Each sub tree is itself a binary search tree.
While the binary search tree is simple and easy to understand, it has one major problem:
It is not balance.
To over come this problem, AVL trees are designed, which are balanced.
AVL TREES
In 1962, two Russian mathematicians, G.M Adelson – velskii and E.M Landis, aerated the
balanced binary tree structure that is named after them – the AVL tree.
An AVL tree is a search tree in which the heights of the sub trees differ by no more than 1. It
is thus a balanced binary tree.
An AVL tree is a binary tree that either is empty or consists of two AVL sub tree, T L and TR,
whose heights differ by no more than 1.
| HL - HR | < = 1
Where HL is the height of the left sub tree, HR is the height of the right sub tree
The bar symbols indicate absolute value.
The Balance factor for any node in an AVL tree must be +1, 0, -1.
Balancing Trees:
When ever we insert a node into a tree or delete a node from a tree, the resulting tree may
be unbalanced then we must rebalance it.
AVL trees are balanced by rotating nodes either to the left or to the right.
Now, we are going to discuss the basic balancing algorithms. They are
1. Left of left:
A sub tree of a tree that is left high has also become left high
2. Right of right:
A sub tree of a tree that is right high has also become right high
3. Right of left:
A sub tree of a tree that is left high has become right high
4. Left of right:
A sub tree of a tree that is right high has become left high
Left of left:
ADVANCED DATA STRUCTURES UNIT - II CSE
When the out – of – balance condition has been created by a left high sub tree of a left high
tree, we must balance the tree by rotating the out – of – balance node to the right.
Complex case:
NOTE:
Right of Right:
This case is the mirror of previous case. It contains a simple left rotation.
ADVANCED DATA STRUCTURES UNIT - II CSE
NOTE:
Right of Left:
The above two types required single rotation to balance the trees. Now we study about two
out – of – balance conditions in which we need to rotate two nodes, one to the left and one
to the right to balance the trees.
Here, an out – of – balance tree in which the root is left high and left sub tree is right high –
a right of left tree.
To balance this tree, we first rotate the left sub tree to the left, then we rotate the root to
the right, making the left node the new root.
Left of Right:
Complex case:
NOTE:
In both cases, i.e. Right of left and Left of right, we have double rotations.
What is the maximum height of an AVL tree having exactly n nodes? To answer this
question, we will pose the following question:
What is the minimum number of nodes (sparsest possible AVL tree) an AVL tree of height h
can have ?
ADVANCED DATA STRUCTURES UNIT - II CSE
Let Fh be an AVL tree of height h, having the minimum number of nodes. Fh can be
visualized as in Figure.
Let Fl and Fr be AVL trees which are the left subtree and right subtree, respectively,
of Fh. Then Fl or Fr must have height h-2.
Suppose Fl has height h-1 so that Fr has height h-2. Note that Fr has to be an AVL tree
having the minimum number of nodes among all AVL trees with height of h-1.
Similarly, Fr will have the minimum number of nodes among all AVL trees of height
h--2. Thus we have
| Fh| = | Fh - 1| + | Fh - 2| + 1
where | Fr| denotes the number of nodes in Fr. Such trees are called Fibonacci trees.
See Figure. Some Fibonacci trees are shown in Figure 4.20. Note that | F0| = 1 and |
F1| = 2.
Thus the numbers | Fh| + 1 are Fibonacci numbers. Using the approximate formula for
Fibonacci numbers, we get
| Fh| + 1
h 1.44log| Fn|
2 – 3 TREES
The basic idea behind maintaining a search tree is to make the insertion, deletion and
searching operations efficient.
ADVANCED DATA STRUCTURES UNIT - II CSE
In AVL Trees the searching operation is efficient. However, insertion & deletion involves
rotation that makes the operation complicated.
To build a 2 – 3 tree there are certain rules that need to be followed. These rules are as
follows:
All the non – leaf nodes in a 2 – 3 tree must always have two or three non – empty
child nodes that are again 2 – 3 trees.
The level of all the leaf nodes must always be the same.
One single node can contain (left and right) then that node contains single data. The
data occurring on left sub tree of that node is less than the data of the node and the
data occurring on right sub tree of that node is greater than the data of the node.
If any node has three children (left, middle, right) then that node contains two data
values, let say i and j where i < j, the data of all the nodes on the middle sub tree are
greater than i but less than j and the data of all nodes on the right sub tree are
greater than j.
Example of 2 – 3 Trees:
Insertions in 2 – 3 Trees:
Let us now try to understand the process of insertion of a value in 2 – 3 trees. To insert a
value in a 2 – 3 trees we first need to search the position where the value can be inserted,
and then the value and node in which the value is to be inserted are adjusted.
Algorithm:
ADVANCED DATA STRUCTURES UNIT - II CSE
Example:
Insert 5 5
Insert 21
Insert 8
Insert 63
Insert 69
ADVANCED DATA STRUCTURES UNIT - II CSE
Insert 32
Insert 7, 9, 25
Deletions in 2 – 3 Trees:
In case of insertion the node where the data is to be inserted is split if it already contains
maximum no. of values. But in case of deletion, two nodes are merged if the node of the
value to be deleted contains minimum number of values (i.e. only one value)
Example 1:
Consider a 2 – 3 trees
Delete 47
Delete 63
Example 2:
ADVANCED DATA STRUCTURES UNIT - II CSE
Delete 47