0% found this document useful (0 votes)
63 views23 pages

Tree Balancing

The document discusses different types of balanced binary search trees, including AVL trees, red-black trees, and multiway search trees like 2-4 trees and B-trees. It provides details on how AVL trees maintain balance through balance factors and rotations. It also outlines the properties that red-black trees follow to stay balanced, including color properties and depth properties. Examples are given of insertion in 2-4 trees, showing how nodes can split and redistribute items when full.

Uploaded by

Dilmi Ranaweera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views23 pages

Tree Balancing

The document discusses different types of balanced binary search trees, including AVL trees, red-black trees, and multiway search trees like 2-4 trees and B-trees. It provides details on how AVL trees maintain balance through balance factors and rotations. It also outlines the properties that red-black trees follow to stay balanced, including color properties and depth properties. Examples are given of insertion in 2-4 trees, showing how nodes can split and redistribute items when full.

Uploaded by

Dilmi Ranaweera
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 23

TREE BALANCING

 What if the input to binary search tree comes in sorted


(ascending or descending) manner? It will then look like
this :

 So a need arises to balance out existing BST.


Tree Balancing
AVL Trees
 Named for Adel’son-Vel’skii and Landis, hence AVL

 The heights of any subtree can only differ by at most one.

 Each nodes will indicate balance factors.

 AVL tree checks the height of left and right sub-trees and
assures that the difference is not more than 1.

 This difference is called Balance Factor.


 Balance Factor=height(left subtree) - height(right subtree)
 Each time the tree structure is changed, the balance
factors are checked and if an imbalance is
recognized, then the tree is restructured.
 For insertion there are four cases to be concerned
with.
AVL Insertion
 Let the node that needs rebalancing be .

 4 cases:
Outside Cases (require single rotation) :
1. Insertion into left subtree of left child of .
2. Insertion into right subtree of right child of .
Inside Cases (require double rotation) :
3. Insertion into left subtree of right child of .
4. Insertion into right subtree of left child of .
Case 1: Insertion into left subtree of left child of 

RIGHT ROTATION
AVL tree may become unbalanced if a node is inserted in the
left subtree of left subtree. The tree then needs a right
rotation.
Case 2: Insertion into right subtree of right child of 

LEFT ROTATION
If a tree become unbalanced, when a node is inserted into the
right subtree of right subtree, then we perform single left
rotation.
Case 3:Insertion into left subtree of right child of 

LEFT-RIGHT ROTATION (Double Rotation)

A node has been inserted into right


subtree of left subtree. This makes C an
unbalanced node.

First perform left rotation on left subtree


of C. This makes A, left subtree of B.
Node C is still unbalanced but now, it is
because of left-subtree of left-subtree.

We shall now right-rotate the tree


making B new root node of this
subtree. C now becomes right subtree of its
own left subtree.

The tree is now balanced.


Case 4:Insertion into right subtree of left child of 

A node has been inserted into left subtree


of right subtree. This makes A an
unbalanced node, with balance factor 2.

First, we perform right rotation


along C node, making Cthe right subtree of
its own left subtree B. Now, Bbecomes
right subtree of A.
Node A is still unbalanced because of
right subtree of its right subtree and
requires a left rotation.

A left rotation is performed by


making B the new root node of the
subtree. A becomes left subtree of its
right subtree B.

The tree is now balanced.


https://www.cs.usfca.edu/~galles/visualization/AVLtree.html
RED BLACK TREES
Red Black Tree Rules(Properties)

1. Every node is colored either red or black


2. The root is black
3. If a node is red its children must be black. (the
red rule)
4. Every path from a node to a null link must
contain the same number of black nodes (the
path rule/depth property)
Example

https://www.cs.usfca.edu/~galles/visualization/RedBlack.html
Comparison with AVL Tree
• The AVL trees are more balanced compared to Red
Black Trees, but they may cause more rotations
during insertion and deletion.
• So if your application involves many frequent
insertions and deletions, then Red Black trees should
be preferred.
• If the insertions and deletions are less frequent and
search is more frequent operation, then AVL tree
should be preferred over Red Black Tree.
MULTIWAY SEARCH TREES
• A multiway search tree is one with nodes that
have two or more children.
• Within each node is stored a given key, which
is associated to an item we wish to access
through the structure.
2-4 Trees
a. Nodes may contain 1, 2 or 3 items.
b. A node with k items has k + 1 children
c. All leaves are on same level.
Example

10 45

3 8 25 38 70 90 100
Insertion

• Find the appropriate leaf. If there is only one


or two items, just add to leaf.
• If no room, move middle item to parent and
split remaining two items among two children.
Insertion

insert 80
10 45

3 8 25 38 70 80 90 100

Overflow!
Insertion
Split & move middle element to parent

10 45 80

3 8 25 38 70 90 100

https://www.cs.usfca.edu/~galles/visualization/BTree.html
B-Trees

10 20 30 40 50 60 70

1 2 3 4 8 6 7 8 9 11 22 33 44 55 66 77

• We can easily find any value.


• Very good and often used for Search Engines!
Thank You !

You might also like