0% found this document useful (0 votes)
29 views66 pages

Unit-Iii Tree

The document provides an overview of AVL Trees and B-Trees, explaining their properties, balance factors, and rotation methods for maintaining balance during insertions and deletions. It also details the structure and applications of B-Trees, particularly in disk access and indexing large datasets. Additionally, it describes the characteristics and insertion processes of 2-3 Trees, a specific type of B-Tree.

Uploaded by

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

Unit-Iii Tree

The document provides an overview of AVL Trees and B-Trees, explaining their properties, balance factors, and rotation methods for maintaining balance during insertions and deletions. It also details the structure and applications of B-Trees, particularly in disk access and indexing large datasets. Additionally, it describes the characteristics and insertion processes of 2-3 Trees, a specific type of B-Tree.

Uploaded by

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

Unit - III

Satish Bansal
AVL Tree (Adelson, Velski, Landis)
 AVL tree is a special kind binary tree in which the
balance factor of each can not be more than 0, 1 or
-1.
 It also called as height balanced tree.
 Or we can say that height of two child sub tree of
any node differ at most by one where left and right
sub trees again AVL Tree.
Balance Factor (BF) = Height of the left sub tree -
Height of the Right Sub tree
Or
BF = HL – HR
E.g.
C BF = 0 - 0 =
BF = 0 - 3 = -
A 0
3 BF = 0 - 1 = -
1 A BF = 0 - 1 = -
BF = 0 -2 = - D
B 1
2 BF = 0 - 0 =
BF = 0 - 0 =
BF = 0 - 1 = - B E 0
C 0
1
D BF = 0 - 0 =
0
(a) Unbalance BST (b) Balance BST

Note:
BF = -1 i.e. right sub tree is higher than left sub tree or it is
called right heavy tree
BF = 1 i.e. left sub tree is higher than right sub tree or it is
called as left heavy tree.
BF = 0 i.e. Both sub tree are on same height.
Representation of AVL Tree
AVL Tree can also be represented or implemented as other tree in memory
with an additional field to keep track of balance factor.
The balance factor of node represents the difference of height between the
left sub tree and right sub tree of the Struct Node
node. {
Structure of AVL Tree node Node * Left; Int Info; Node *
Left Info Right BF Right; Int BF;
};
Node * root;

Building AVL Tree or Height Balance


Tree
AVL Tree is constructed by using the same process as applied to BST.
The only thing we need to remember that it follow the property of
height balance tree (BF).
The balance factor of each node can be 0, 1 or -1.
While the insertion or deletion is preformed, the tree become unbalanced and
violate the property of AVL tree that time we have to apply different rotation on
the AVL Tree
AVL Tree Single Rotation
1. Left to Left Rotation
(LL)
A BF = 2 - 0 =
2 BF = 1 - 1 = 0
LL
BF = 1 - 0 = B
B
1
BF = 0 - 0 = C A BF = 0 - 0 =
C BF = 0 - 0 = 0 0
0
2. Right to Right Rotation
(RR)

A BF = 0 -2 = -
2 BF = 1 - 1 = 0
RR B
B BF = 0 - 1 = -
1
BF = 0 - 0 = A C BF = 0 - 0 =
C BF = 0 - 0 = 0
0
0
AVL Tree Double Rotation
3. Left to Right Rotation (LR) = RR on subtree + LL full tree

A BF = 2 - 0 =
2 BF = 1 - 1 = 0
LR
BF = 0 - 1 = - C
B
1
BF = 0 - 0 = B A BF = 0 - 0 =
C BF = 0 - 0 = 0 0
0
4. Right to Left Rotation (RL) = LL on subtree + RR full tree

A BF = 0 - 2 = -
2 BF = 1 - 1 = 0
RL C
B BF = 1 - 0 =
1
BF = 0 - 0 = A B BF = 0 - 0 =
C BF = 0 - 0 = 0 0
0
e.g.
23, 12, 82, 15, 16, 57, 50

Sr. I/P Node Tree Rotation


No.
1 23 23 BF = 0 - 0 = 0 Tree is balanced

2 12 1 Tree is balanced
23

0 12

3 0 Tree is balanced
82
23

12 0 82 0
e.g. 23, 12, 82, 15, 16, 57, 50
Sr. I/P Node Tree Rotation
No.
4 1 Tree is balanced
23
15

-1 12 82 0
0
15

5 2 23 1
-2 23
16 0
12 -1 82 R 0 0
R
15 82
15 0
0 0
16 12 16

Now Tree is balanced


e.g. 23, 12, 82, 15, 16, 57, 50
Sr. I/P Node Tree Rotation
No.
6 0 23 Tree is balanced
0 1
57
0 15 0 0 82

12 16 57

7 -1 23 0
50 23
0 LL
2 0 0
0 15 0 82 0 15 57 0
1 0 0
12 16 57 12 16 50 82
0
50 Now Tree is balanced
B (Balanced) Tree
• A B tree of order m contains all the properties of an
multi (M) way search tree. In addition, it contains
the following properties.
• Every node in a B-Tree contains at most m-1 keys
and m children.
• Every node in a B-Tree except the root node and the
leaf node contain at least m/2 children.
• The root nodes must have at least 2 sub trees unless
it is the only node in the tree.
• All leaf nodes must be at the same level.
An example B-Tree
26 A B-tree of order 5
containing 26 items
6 12

42 51 62
1 2 4 7 8 13 15 18 25

27 29 45 46 48 53 55 60 64 70 90

Note that all the leaves are at the same level


Application of B Tree
B Tree is a specialized m-way tree that can be widely
used for disk access. One of the main reason of using
B tree is its capability to store large number of keys
in a single node and large key values by keeping the
height of the tree relatively small.
B tree is used to index the data and provides fast
access to the actual data stored on the disks since,
the access to value stored in a large database that is
stored on a disk is a very time consuming process.
Multi way 2-3 search tree
• If we take m = 3, we get a 2-3 tree, in which
non-leaf nodes have two or three children
(i.e., one or two keys)
– B-Trees are always balanced (since the leaves are
all at the same level), so 2-3 trees make a good
type of height balanced tree.
• The time complexity of search/insert/delete is
O(log N) .
• A 2-3 tree is a B-tree of order 3.
Properties of 2-3 tree
• Nodes with two children are called 2-nodes. The 2-
nodes have one data value and two children
• Nodes with three children are called 3-nodes. The
3-nodes have two data values and three children.
• Data is stored in sorted order.
• It is a balanced tree.
• All the leaf nodes are at same level.
• Each node can either be leaf, 2 node, or 3 node.
• Always insertion is done at leaf.
Example
Search Algorithm:
• To search a key K in given 2-3 tree T, we follow the following
procedure:
• Base cases:
• If T is empty, return False (key cannot be found in the tree).
• If current node contains data value which is equal to K, return
True.
• Searching start with root node then reach the leaf-node and it
doesn’t contain the required key value K, return False.
• Recursive Calls:
• If K < currentNode.leftVal, we explore the left subtree of the
current node.
• Else if currentNode.leftVal < K < currentNode.rightVal, we
explore the middle subtree of the current node.
• Else if K > currentNode.rightVal, we explore the right subtree of
the current node.
Example
Insertion Process

Splitting the Root 2-3 Tree


Start with this tree
New 50
Root
Root M
SM 70
S L 30
L 90
a c d
b a d
c
b
40 6 100
10 20
0 80

2-3 Trees 13 2- 14
3
Trees

Insert 39 Insert 38
Locate leaf to insert Locate lea to insert
39 f 38
50
50
3 70
0 90
30 70 90
10 20 39 60 80 100
40
Leaf to insert only has 1 data 10 20 39 40 60 80 100
item
 Add 39 to the leaf
15 2-3 Trees 16

2-3
Tree
Insert 38
Insert 38
Conceptualize inserting 38 into this Determine
leaf  Smallest =
 Do not actually add the item because 38
the node can only hold 2 data items  Middle = 39
 Largest = 40
50
3 70
0 90
8 10
10 20 38 39 40 60 0
0
2-3 Trees 17 2-3 Trees 18

Insert 38 Insert
Move middle value up to parent p 38
Separate small and large values into
5
two separate nodes that will be 0
children of p
30 70
39 90
50
3 70 10 20 38 40 60 80 100
0 90
8 10
10 20 38 39 40 60 0
0
19 2-3 Trees 20

2-3 Trees
Insert 37 Insert 36
Locate leaf to insert 37 Locate leaf to insert 36
Leaf contains 1 data value, just Conceptualize inserting 36 into this
insert value leaf
 Determine small (36), middle (37), and
50
large (38)
50
30 70 30 39 70 90
39 90
10 20 36 37 38 40 60 80 10
10 20 37 38 40 60 80 100
0
2-3 Trees 21 2-3 Trees 22

Insert 36 Insert 36
Conceptualize moving middle value Conceptualize attaching as children to p
up to parent p the smallest and largest values
 Do not actually move, node can’t have  Do not actually attach because a node can’t
3 data values have 4 children
50
50
30 70 30 37 70
39 90 39 90
8 10
10 20 36 37 38 40 60 80 100 10 20 36 38 40
0 0
60
23 2-3 Trees 24

2-3
Tree
Insert 36 Insert 36
Parent p now has 3 data values and 4 Split parent p
children  Divide to small (30), middle (37), and large (39)
Split - similar to leaf situation where leaf has  Move middle value to nodes parent
3 data values  Small and large become new children, s and l
 You can generalize both situations into one

50
50
30 37 70 s l
30 37 39 70 90
39 90
10
10 20 36 38 40 60 80
0 10 20 36 38 40 60 80
2-3 Trees 25
100

2-3 Trees

26

Insert 36
Divide 4 children
 Two leftmost become children
Insert 35
of s Insert 35
 Two rightmost become children  Inserts into leaf
of l
37 50
37 50
3 3 70
0 9 90 30 39 70 90
10 4 60 80 100
36 38
20 0 10 20 35 36 38 40 60 80 10
2-3 Trees 27 0

2-3 Trees 28
Insert 34 Insert 34
Insert 34 Insert 34
 Causes a split  Causes a split

37 50 37 50

30 39 70 90 30 70 90

39
10 20 34 35 36 38 40 60 80 100
40 60 80 100
10 20 34 35
36 38
2-3 Trees 29

2-3 Trees 30

Insert 34
Insert 33
Insert 34
 Causes a split
Insert 33
37 50
 Inserts into leaf
37 50

30 35 39 70 90
30 35 39 70 90
38 40 60 80 100
10 20
40 60 80 100
10 20 33 34 36 38
34
2-3 Trees 32
36
Algorithm for
Inserting a node in a 2-3 tree
• To insert X in a 2-3 tree.
• If the tree is empty → Add X as root.
• Search for the right position of X and add it as a
leaf node.
• If there is a leaf node with one data part, add X
with and leaf node becomes 2 - Node.
• If the leaf node has two data parts, add X. Split
temporary 3-nodes and move data to parent
nodes according to the sorting order.
2-3 Tree Insertion Example
Here we show the letter-by-letter insertion of the letters A L G O R I T H M S into an initially empty 2-3 tree.
We use the terminal node optimizations to simplify the sequence.

in

sert A in

sert A L in

sert new

root G in

sert G
A L G G A L O A L O
A L

kick up insert insert


G → → →
in

sert
O G O I G O T G O
R A A L R A I L R A I L R T
O

L R
kick up new root
→ → I
in

sert G O I
H A R T I G O
I G O A H L R T
H L A H L R T

kick up
in sert I in sert I → I
→ → S
M G O S G O G O S
A H L M R T A H L M A H L M R T
S

R T
Deletion Process

Remove - Deleting the Root


Merge
L -
New
Empty root
S L root
S P
S A B
S P
- A B b d a b c
a c e
a b c

a b
c

d
2-3 Trees 50
e 49

2-3 Trees

Remove 65
65 is an internal node - swap
with inorder successor
2-3 Tree  Inorder successor will always be
in a leaf
Start with this tree
50 50

3 65
0 90
30 65 90
10 40 6 70 80
20 0 100
10 20 40 60 70 8 100 51 2-3 Trees 52

2-3 0
Tree
Remove 65
Remove 65
65 is now in an invalid location but Since there are 2 data values in the
that is okay because we will leaf, just remove data value
remove it
5 50
0

3 70 3 70
0 90 0 90

10 4 60 65 80 10 4 60 80 100
20 0 20 0
2-3 Trees 100 53 2-3 Trees 54

Delete 70 Delete 70
70 is an internal node - swap 70 is now in an invalid location but
with inorder successor that is okay - we will be removing
 Inorder successor will always be that node
in a leaf 5
50 0

3 70 3 80
0 90 0 90

10 4 6 8 10 10 4 6 7 10
20 0 0 0 0 20 0 0 0 0
55 2-3 Trees 56

2-3
Tree
Delete 70 Delete 70
Removing leaf leaves us with an Merge nodes to fix
invalid 2-3 tree tree
50
50
9
30 3
0
0
80 90
10 4 60 10
20 0 80 0
10 20 40
2-3 Trees 58
60 -
100

2-3 Trees

57
Delete 100
Sibling has data item to
spare, redistribute
Delete 100 50

100 is already leaf, just remove 9


leaf 3
0
0
50 -
10 4 60
20 0 80

30 2-3 Trees 60

90
Delete 100 Delete 80
Sibling has data item to Swap 80 with inorder
spare, redistribute successor
50 5
0

8 8
3 3
0 0
0 0

10 4 6 9 10 4 6 9
20 0 0 0 20 0 0 0

2-3 Trees 61 2-3 Trees 62

Delete 80 Delete 80
Can’t redistribute so merge Can’t redistribute so merge nodes
nodes Invalid 2-3 tree, continue
5 recursively up the tree
0
50
9
3
0 -
0 3
6 - 0
10 4
20 0 0
10 4 60
20 0 90
63 2-3 Trees 64

2-3
Tree
Delete 80
Delete 80
Can’t redistribute so merge Can’t redistribute so merge
nodes nodes
5 -
0

- 30
3
50
0

10 4 60 10 20 40 60 90
20 0 90

2-3 Trees 65 2-3 Trees 66

Delete 80
Root is now empty, set new
root pointer

Roo
t
30
50

10 20 40 60 90

67 2-3 Trees 68

2-3
Tree
Algorithm for
Deletion of a node in a 2-3 tree

• To delete X in a 2-3 tree.


• If the tree is empty return false.
• Search for the position of X and delete it, then
adjust the tree.
• If X is part of 3 node delete X and adjust left
value and middle value. Also adjust node’s
ancestor’s left and middle value if necessary.
• If X is part of 2 node then adjust and split the
tree in a recursive manner arranging nodes in
sorted order.
2-3 Tree Deletion Example
Here we show the letter-by-letter deletion of the letters A L G O R I T H M S from the final 2-3 tree of the
insertion example:
I de lete
→ I ca se 1
→ I
G O S A G O S O S
A H L M R T H L M R T G H L M R T

ca

se 2 O de

lete O de

l et e O
I S L I S G I S
G H L M R T G H M R T H M R T
replace case 1 case 1
de l et e
→ M → M →
→ by pred
O I S I S S M S
H M R T H R T H I R T H I R T
case 3(b)
rem ove M S de l et e M S → M de l et e M
→ → →
root hole H I R T R H I T H I S T I H S T

de

l et e M de

l et e M ca

se 1 rem ove M S de

l et e S de

lete

H S H T S M S root hole M S

At the point where case 3(b) was applied, it is also possible to apply case 4(a) instead. This leads to the following
alternative deletion sequence:
case 4(a) case 3(a)
→ →
delete replace → → S
M S I S I S by pred H S
H I H M T H M T M T H M T
T
de

l et e S ca

se 2 M de

l et e M ca

se 1 rem ove M S de

l et e S de

lete

H M
T H S H S M S root hole M S
There are some other choices in the above sequences. In each of the spots where a deleted value in a non-terminal
node was replaced by its in-order predecessor, it could have been replaced by its in-order successor.
Red-Black Tree
• A Red-Black Tree is a self-balancing binary
search tree where each node has an additional
attribute: a color, which can be
either red or black. The primary objective of
these trees is to maintain balance during
insertions and deletions, ensuring efficient data
retrieval and manipulation.
Properties of Red-Black Trees
• A Red Black Tree is a BST with the following properties:
• Node Color: Each node is either red or black.
• Root Property: The root of the tree is always black.
• Red Property: Red nodes cannot have red children (no
two consecutive red nodes on any path).
• Black Property: Every path from a root node to its
descendant null nodes (leaves) has the same number
of black nodes.
• Leaf Property: All leaves (NIL nodes) are black.
• Every new node must be inserted with RED color.
The Correct Red-Black Tree in above image ensures that every path
from the root to a leaf node has the same number of black nodes.
The Incorrect Red Black Tree does not follow the red-black properties
as two red nodes are adjacent to each other. Another problem is that one
of the paths to a leaf node has zero black nodes, whereas the other two
contain a black node.
Example
Why Red-Black Trees?
• Most of the BST operations (e.g., search, max,
min, insert, delete.. etc) take O(h) time where h is
the height of the BST. The cost of these operations
may become O(n) for a skewed Binary Tree. If
we make sure that the height of the tree
remains O(log n) after every insertion and
deletion, then we can guarantee an upper bound
of O(log n) for all these operations. The height of
a Red-Black tree is always O(log n) where n is
the number of nodes in the tree.
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 frequent insertions
and deletions, then Red-Black trees should be
preferred. And if the insertions and deletions
are less frequent and search is a more frequent
operation, then AVL tree should be preferred
over the Red-Black Tree.
How does a Red-Black Tree ensure
balance?
• A simple example to understand balancing is,
that a chain of 3 nodes is not possible in the
Red-Black tree. We can try any combination of
colors and see if all of them violate the Red-
Black tree property.
Basic Operations on Red-Black Tree:
• The basic operations on a Red-Black Tree
include:
• Insertion
• Search
• Deletion
• Rotation
Insertion
• Inserting a new node in a Red-Black Tree involves a
two-step process: performing a standard binary
search tree (BST) insertion, followed by fixing any
violations of Red-Black properties.
• Insertion Steps
• BST Insert: Insert the new node like in a standard BST.
• Inserting the new node as a red node
• Fix Violations:
– If the parent (root) of the new node is black, no properties
are violated.
– If the parent is red, the tree might violate the Red
Property, requiring fixes.
Insertion
• Fixing Violations During Insertion
• After inserting the new node as a red node, we might
encounter several cases depending on the colors of the
node’s parent and uncle (the sibling of the parent):
• Case 1: Uncle is Red: Recolor the parent and uncle to black,
and the grandparent to red. Then move up the tree to check
for further violations.
• Case 2: Uncle is Black:
– Sub-case 2.1: Node is a right child: Perform a left rotation on the
parent.
– Sub-case 2.2: Node is a left child: Perform a right rotation on the
grandparent and recolor appropriately.
Example
Example
Example
Example
Example
Example
Searching
• Searching for a node in a Red-Black Tree is similar to searching
in a standard Binary Search Tree (BST). The search operation
follows a straightforward path from the root to a leaf,
comparing the target value with the current node’s value and
moving left or right accordingly.
• Search Steps
• Start at the Root: Begin the search at the root node.
• Traverse the Tree:
– If the target value is equal to the current node’s value, the node is
found.
– If the target value is less than the current node’s value, move to the
left child.
– If the target value is greater than the current node’s value, move to
the right child.
• Repeat: Continue this process until the target value is found or
a NIL node is reached (indicating the value is not present in the
Deletion
• Deleting a node from a Red-Black Tree also involves
a two-step process: performing the BST deletion,
followed by fixing any violations that arise.
• Deletion Steps
• BST Deletion: Remove the node using standard BST
rules.
• Fix Double Black:
– If a black node is deleted, a “double black” condition
might arise, which requires specific fixes.
Deletion
• Fixing Violations During Deletion
• When a black node is deleted, we handle the double black issue
based on the sibling’s color and the colors of its children:
• Case 1: Sibling is Red: Rotate the parent and recolor the sibling
and parent.
• Case 2: Sibling is Black:
– Sub-case 2.1: Sibling’s children are black: Recolor the sibling and
propagate the double black upwards.
– Sub-case 2.2: At least one of the sibling’s children is red:
• If the sibling’s far child is red: Perform a rotation on the parent and sibling,
and recolor appropriately.
• If the sibling’s near child is red: Rotate the sibling and its child, then handle as
above.
Example
Example
Example
Example
How About an Example?
6

Remove 9
4 8

2 5 7 9

4 8

2 5 7

55
Example
• What do we know?
– Sibling is black with black children

• What do we do? 6 6
– Recoloring
4 8 4 8

2 5 7 2 5 7

56
Example
• Delete 8
no double black
6 6

4 8 4 7

2 5 7 2 5

57
Example
Delete 7
• Restructuring 6 6

4 7 4

2 5 2 5

2 6

58
Example (Delete 12)

59
Example

60
Rotation
• Rotations are fundamental operations in
maintaining the balanced structure of a Red-
Black Tree (RBT). They help to preserve the
properties of the tree, ensuring that the
longest path from the root to any leaf is no
more than twice the length of the shortest
path. Rotations come in two types: left
rotations and right rotations.
Left Rotation
• A left rotation at node x moves x down to the left and its
right child y up to take x’s place.
• Before Rotation:

• x
• \
• y
Left Rotation Steps:
• /\

Set y to be the right child of x.
a b
Move y’s left subtree to x’s right subtree.
• After Left Rotation: Update the parent of x and y.
Update x’s parent to point to y instead of x.
• y Set y’s left child to x.
• /\ Update x’s parent to y.
• x b
• \
• a
Right Rotation
• A right rotation at node 𝑥 moves 𝑥 down to the right and
its left child y up to take x’s place.
• Befor Right Rotation:

• x
• /
• y Right Rotation Steps:
• /\ Set y to be the left child of x.
• a b Move y’s right subtree to x’s left subtree.
Update the parent of x and y.
• After Right Rotation:
Update x’s parent to point to y instead of x.
• y
Set y’s right child to x.
• /\ Update x’s parent to y.
• a x
• /
• b
Advantages of Red-Black Trees:
• Balanced: Red-Black Trees are self-balancing, meaning they
automatically maintain a balance between the heights of the
left and right subtrees. This ensures that search, insertion, and
deletion operations take O(log n) time in the worst case.
• Efficient search, insertion, and deletion: Due to their balanced
structure, Red-Black Trees offer efficient operations. Search,
insertion, and deletion all take O(log n) time in the worst case.
• Simple to implement: The rules for maintaining the Red-Black
Tree properties are relatively simple and straightforward to
implement.
• Widely used: Red-Black Trees are a popular choice for
implementing various data structures, such as maps, sets, and
priority queues.
Disadvantages of Red-Black Trees:
• More complex than other balanced trees: Compared to
simpler balanced trees like AVL trees, Red-Black Trees
have more complex insertion and deletion rules.
• Constant overhead: Maintaining the Red-Black Tree
properties adds a small overhead to every insertion and
deletion operation.
• Not optimal for all use cases: While efficient for most
operations, Red-Black Trees might not be the best
choice for applications where frequent insertions and
deletions are required, as the constant overhead can
become significant.
Applications of Red-Black Trees:
• Implementing maps and sets: Red-Black Trees are often used
to implement maps and sets, where efficient search,
insertion, and deletion are crucial.
• Priority queues: Red-Black Trees can be used to implement
priority queues, where elements are ordered based on their
priority.
• File systems: Red-Black Trees are used in some file systems to
manage file and directory structures.
• In-memory databases: Red-Black Trees are sometimes used
in in-memory databases to store and retrieve data efficiently.
• Graphics and game development: Red-Black Trees can be
used in graphics and game development for tasks like collision
detection and path finding.

You might also like