Docsity

Balanced Trees and their functions, Lecture notes of Computer Architecture and Organization

Toggle sidebar
bg1
Definition:
Balanced binary trees are also known as Height Balanced Binary trees, is defined as a binary tree in which the height of
the left and right subtree of any node differ by not more than 1.
โ—Š
Height balanced binary trees can be denoted by HB(k), where k is the difference between heights of left and right subtrees. โ€˜kโ€™ is known as
the
balance factor
.
If for a tree, the balance factor (k) is equal to zero, then that tree is known as a fully balanced binary tree. It can be denoted as HB(0).
There are several types of balanced trees:
AVL tree
1.
Red-Black tree
2.
2-3 tree
3.
AVL tree:
โ—Š
The AVL Trees implement the self-balancing property by attaching extra information known as the balance factor to each
node of the tree, which is calculated by subtracting the height of its right sub-tree from that of its left sub-tree(bf= hl
-
hr).
If balance factor of any node is 1, it means that the left sub-tree is one level higher than the right sub-tree.
If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal height.
If balance factor of any node is -1, it means that the left sub-tree is one level lower than the right sub-tree.
AVL Trees were developed to achieve logarithmic time complexity in BSTs irrespective of the order in which the elements
were inserted.
Height of AVL tree:
โ—‹
Nh= Nh
-
1+ Nh
-
2+ 1, N0= 0 and N1= 1
Complexity:
Algo
Average case
Worst case
Space
O(n)
O(n)
Search
O(log n)
O(log n)
Insert
O(log n)
O(log n)
Delete
O(log n)
O(log n)
โ—‹
AVL Tree Rotations:
โ—‹
L L rotation: In LL rotation, every node moves one position to left from the current position. It is a clock-wise rotation.
โ†’
R R rotation : In RR Rotation, every node moves one position to right from the current position. It is a anti-clockwise
rotation.
โ†’
L R rotation : The LR Rotation is a sequence of single left rotation followed by a single right rotation. In LR Rotation, at
first, every node moves one position to the left and one position to right from the current position.
โ†’
R L rotation : The RL Rotation is sequence of single right rotation followed by single left rotation. In RL Rotation, at first
every node moves one position to right and one position to left from the current position.
โ†’
AVL Insertion operation:
โ—‹
In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree, a new node is always inserted
as a leaf node. The insertion operation is performed as follows...
Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
โ–ซ
Step 2 - After insertion, check the Balance Factor of every node.
โ–ซ
Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
โ–ซ
BALANCED TREES
14 December 2022
17:19
DAA Page 1
pf2

Partial preview of the text

Download Balanced Trees and their functions and more Lecture notes Computer Architecture and Organization in PDF only on Docsity!

Definition: Balanced binary trees are also known as Height Balanced Binary trees, is defined as a binary tree in which the height of

the left and right subtree of any node differ by not more than 1.

Height balanced binary trees can be denoted by HB(k), where k is the difference between heights of left and right subtrees. โ€˜kโ€™ is known as

the balance factor.

If for a tree, the balance factor (k) is equal to zero, then that tree is known as a fully balanced binary tree. It can be denoted as HB(0).

There are several types of balanced trees:

  1. AVL tree
  2. Red-Black tree
  3. 2 - 3 tree

โ—Š AVL tree:

An AVL tree is a height balanced binary search tree such that, for every node, the difference between the heights of its left and

right subtrees is either 1 or - 1 (the height of an empty tree is defined as 0). It is a self-balancing tree i.e., AVL Trees

automatically attain the minimal possible height of the tree after the execution of any operation.

The AVL Trees implement the self-balancing property by attaching extra information known as the balance factor to each

node of the tree, which is calculated by subtracting the height of its right sub-tree from that of its left sub-tree(bf = hl

hr).

If balance factor of any node is 1, it means that the left sub-tree is one level higher than the right sub-tree.

If balance factor of any node is 0, it means that the left sub-tree and right sub-tree contain equal height.

If balance factor of any node is - 1, it means that the left sub-tree is one level lower than the right sub-tree.

AVL Trees were developed to achieve logarithmic time complexity in BSTs irrespective of the order in which the elements

were inserted.

โ—‹ Height of AVL tree:

Let Nh be the minimum number of nodes in an AVL tree height h. In the worst case height of one of the subtrees is h

1 and the

height of the other is h - 2. Both of these subtrees are also AVL trees. Hence,

Nh = Nh

1 + Nh

2 + 1, N 0 = 0 and N 1 = 1

Complexity:

Algo Average case Worst case

Space O(n) O(n)

Search O(log n) O(log n)

Insert O(log n) O(log n)

Delete O(log n) O(log n)

โ—‹ AVL Tree Rotations:

We perform rotation in AVL tree only in case if Balance Factor is other than

1, 0, and 1. There are basically four types of

rotations which are as follows:

โ†’ L L rotation: In LL rotation, every node moves one position to left from the current position. It is a clock-wise rotation.

R R rotation : In RR Rotation, every node moves one position to right from the current position. It is a anti - clockwise

rotation.

L R rotation : The LR Rotation is a sequence of single left rotation followed by a single right rotation. In LR Rotation, at

first, every node moves one position to the left and one position to right from the current position.

R L rotation : The RL Rotation is sequence of single right rotation followed by single left rotation. In RL Rotation, at firs t

every node moves one position to right and one position to left from the current position.

โ—‹ AVL Insertion operation:

In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree, a new node is always inserte d

as a leaf node. The insertion operation is performed as follows...

โ–ซ Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.

โ–ซ Step 2 - After insertion, check the Balance Factor of every node.

Step 3 - If the Balance Factor of every node is 0 or 1 or - 1 then go for next operation.

BALANCED TREES

14 December 2022 17:

DAA Page 1

โ–ซStep 3 - If the Balance Factor of every node is 0 or 1 or - 1 then go for next operation.

Step 4 - If the Balance Factor of any node is other than 0 or 1 or - 1 then that tree is said to be imbalanced. In this case,

perform suitable Rotation to make it balanced and go for next operation.

โ—‹ AVL Deletion operation:

DAA Page 2