Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 14

ADVANCED DATA STRUCTURES UNIT - II CSE

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.

 A leaf is any node with an out degree of zero. i.e. no successor


 A node that is not a root or leaf is known as an internal node
 A node is a parent if it has successor node, conversely a node with a predecessor is
called child
 A path is a sequence of nodes in which each node is adjacent to the next one
 The level of a node is its distance from the root
 The height of the tree is the level of the leaf in the longest path from the root plus 1
 A sub tree is any connected structure below the root. Sub tree can also be further
divided into sub trees

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.

Note: each sub tree is itself a binary 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.

We now turn our attention to operations: search, insertion, deletion

Narasaraopeta Engineering College Page 19


ADVANCED DATA STRUCTURES UNIT - II CSE

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.

Binary search tree:

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 - H R | < = 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.

NOTE: An AVL tree is a height balanced binary search tree.

Consider an example: AVL tree

Narasaraopeta Engineering College Page 20


ADVANCED DATA STRUCTURES UNIT - II CSE

AVL Tree Balance factor:

The Balance factor for any node in an AVL tree must be +1, 0, -1.

We use the descriptive identifiers


 LH for left high (+1) to indicate that the length sub tree is higher than the right sub
tree
 EH for even high (0) to indicate that the sub tree are the same height
 RH for right high (-1) to indicate that the left sub tree is shortest than the right sub
tree

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:

Narasaraopeta Engineering College Page 21


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.

Let’s begin with a Simple case:

Complex case:

NOTE:

In the above two cases, we have single rotation right.

Right of Right:

This case is the mirror of previous case. It contains a simple left rotation.

Narasaraopeta Engineering College Page 22


ADVANCED DATA STRUCTURES UNIT - II CSE

Simple case: here, simple left rotation

Complex case: here, complex left rotation

NOTE:

In the above two cases, we have single rotation left.

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.

Narasaraopeta Engineering College Page 23


ADVANCED DATA STRUCTURES UNIT - II CSE

Simple case: simple double rotation right.

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.

Complex case: complex double rotation right.

Left of Right:

This case is also complicated

Simple case: simple double rotation

Narasaraopeta Engineering College Page 24


ADVANCED DATA STRUCTURES UNIT - II CSE

Complex case:

NOTE:

In both cases, i.e. Right of left and Left of right, we have double rotations.

Maximum Height of an AVL Tree:

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 ?

Narasaraopeta Engineering College Page 25


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.

Adding 1 to both sides, we get


| Fh| + 1 = (| Fh - 1| + 1) + (| Fh - 2| + 1)

Thus the numbers | Fh| + 1 are Fibonacci numbers. Using the approximate formula for
Fibonacci numbers, we get

| Fh| + 1

h 1.44log| Fn|

The sparsest possible AVL tree with n nodes has height


h 1.44log n

The worst case height of an AVL tree with n nodes is


1.44log n

Figure 5.3: Fibonacci trees

Narasaraopeta Engineering College Page 26


ADVANCED DATA STRUCTURES UNIT - II CSE

Figure 5.4: Rotations in a binary search tree

2 – 3 TREES

The basic idea behind maintaining a search tree is to make the insertion, deletion and
searching operations efficient.

Narasaraopeta Engineering College Page 27


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 eliminate this complication a data structure was designed, called as 2 - 3 trees.

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:
Narasaraopeta Engineering College Page 28
ADVANCED DATA STRUCTURES UNIT - II CSE

 Insert new leaf in appropriate place

 Repeat until all non leaf nodes have 2 or 3 children


 If there is a node with 4 children, split the parent into two parent nodes, with 2
children each
 If you split the root, then add a new root

 Adjust search values along insertion path

Example:

Insert 5  5

Insert 21 

Insert 8 

Insert 63 

Insert 69 

Narasaraopeta Engineering College Page 29


ADVANCED DATA STRUCTURES UNIT - II CSE

Insert 32 

Insert 7, 9, 25 

Deletions in 2 – 3 Trees:

Narasaraopeta Engineering College Page 30


ADVANCED DATA STRUCTURES UNIT - II CSE
Deletion of a value from a 2 – 3 trees is exactly opposite to insertion

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:

Narasaraopeta Engineering College Page 31


ADVANCED DATA STRUCTURES UNIT - II CSE

Delete 47 

The Resultant 2 – 3 After deletion of 47 :

Narasaraopeta Engineering College Page 32

You might also like