0% found this document useful (0 votes)
9 views

Unit-III DS Search Trees

Unit-III DS Search Trees

Uploaded by

Mohammed Afzal
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)
9 views

Unit-III DS Search Trees

Unit-III DS Search Trees

Uploaded by

Mohammed Afzal
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/ 69

Unit-III: Search Trees

Search Trees: Binary Search Trees, Definition, Implementation, Operations- Searching, Insertion and
Deletion, B- Trees, B+ Trees, AVL Trees, Definition, Height of an AVL Tree, Operations – Insertion,
Deletion and Searching, Red –Black, Splay Trees.

Introduction to Tree
o In linear data structures like an array, linked list, stack and queue in which all the elements are arranged in a
sequential manner.
o A tree data structure is a nonlinear hierarchical data structure that consists of nodes connected by edges.
o In linear data structures like an array, linked list, stack and queue in which all the elements are arranged in a
sequential manner. A tree data structure is a nonlinear hierarchical data structure that consists of nodes
connected by edges.
o A tree is a connected graph without any circuits.
o In tree data structure, every individual element is called as Node. Node in a tree data structure store the
o actual data of that particular element and link to next element in hierarchical structure.
o The topmost node of the tree is called the root, and the nodes below it are called the child nodes.
o Each node can have multiple child nodes, and these child nodes can also have their own child nodes,
forming a recursive structure.
o In a tree data structure, if we have N number of nodes then we can have a maximum of N-1 number of
links.
Definition
A tree is a finite set of one or more nodes such that, there is a specially designated node called root. The
remaining nodes are partitioned into n>=0 disjoint sets T1, T2,..Tn, where each of these set is a tree
T1,T2, ...Tn are called the subtrees of the root.

The important properties of tree data structure are-


 There is one and only one path between every pair of vertices in a tree.
 A tree with n vertices has exactly (n-1) edges.
 A graph is a tree if and only if it is minimally connected.
 Any connected graph with n vertices and (n-1) edges is a tree.
Example:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Trees – Basic Terminology:
The following terminology is used in trees concept.
1. Root
o In a tree data structure, the first node is called as Root Node.
o Every tree must have a root node, the root node is the origin of the tree data structure.
o In any tree, there must be one and only one root node. There cannot be multiple root nodes in a
tree.

2. Edge
o In a tree data structure, the connecting link between any two nodes is called as EDGE.
o In a tree with 'N' number of nodes there will be a maximum of 'N-1' number of edges.

3. Parent
o In a tree data structure, the node which is a predecessor of any node is called as PARENT NODE.
o In simple words, the node which has a branch from it to any other node is called a parent node.
o Parent node can also be defined as "The node which has child / children".

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
4. Child
o In a tree data structure, the node which is descendant of any node is called as CHILD Node.
o In simple words, the node which has a link from its parent node is called as child node.
o In a tree, any parent node can have any number of child nodes.
o In a tree, all the nodes except root are child nodes.

5. Siblings
o In a tree data structure, nodes which belong to same Parent are called as SIBLINGS.
o In simple words, the nodes with the same parent are called Sibling nodes.

6. Leaf
o In a tree data structure, the node which does not have a child is called as LEAF Node.
o In simple words, a leaf is a node with no child.
o In a tree data structure, the leaf nodes are also called as External Nodes.
o External node is also a node with no child.
o In a tree, leaf node is also called as 'Terminal' node.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
7. Internal Nodes
o In a tree data structure, the node which has at least one child is called as INTERNAL Node.
o In simple words, an internal node is a node with at least one child.
o In a tree data structure, nodes other than leaf nodes are called as Internal Nodes.
o The root node is also said to be Internal Node if the tree has more than one node.
o Internal nodes are also called as 'Non-Terminal' nodes.

8. Degree
o In a tree data structure, the total number of children of a node is called as DEGREE of that Node.
o In simple words, the Degree of a node is total number of children it has.
o The highest degree of a node among all the nodes in a tree is called as 'Degree of Tree'.

9. Level
o In a tree data structure, the root node is said to be at Level 0 and the children of root node are at
Level 1 and the children of the nodes which are at Level 1 will be at Level 2 and so on...
o In simple words, in a tree each step from top to bottom is called as a Level and the Level count starts
with '0' and incremented by one at each level (Step).

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
10. Height
o In a tree data structure, the total number of edges from leaf node to a particular node in the longest
path is called as HEIGHT of that Node.
o In a tree, height of the root node is said to be height of the tree.
o In a tree, height of all leaf nodes is '0'.

11. Depth
o In a tree data structure, the total number of edges from root node to a particular node is called as
DEPTH of that Node.
o In a tree, the total number of edges from root node to a leaf node in the longest path is said to be
Depth of the tree.
o In simple words, the highest depth of any leaf node in a tree is said to be depth of that tree.
o In a tree, depth of the root node is '0'.

12. Path
o In a tree data structure, the sequence of Nodes and Edges from one node to another node is called
as PATH between that two Nodes.
o Length of a Path is total number of nodes in that path.
o In below example the path A - B - E - J has length 4.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
13. Degree of a Node: The number of children a node has.
14. Sub Tree
o In a tree data structure, each child from a node forms a subtree recursively.
o Every child node will form a subtree on its parent node.

Properties of Tree data structure


 Recursive data structure: The tree is also known as a recursive data structure. A tree can be defined
as recursively because the distinguished node in a tree data structure is known as a root node. The root
node of the tree contains a link to all the roots of its subtrees. The left subtree can be further split into
subtrees. Recursion means reducing something in a self-similar manner. So, this recursive property of
the tree data structure is implemented in various applications.
 Number of edges: If there are n nodes, then there would n-1 edges. Each arrow in the structure
represents the link or path. Each node, except the root node, will have at least one incoming link known
as an edge. There would be one link for the parent-child relationship.
 Depth of node x: The depth of node x can be defined as the length of the path from the root to the node
x. One edge contributes one-unit length in the path. So, the depth of node x can also be defined as the
number of edges between the root node and the node x. The root node has 0 depth.
 Height of node x: The height of node x can be defined as the longest path from the node x to any leaf
node.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Applications of trees
The following are the applications of trees:
 Storing naturally hierarchical data: Trees are used to store the data in the hierarchical structure. For
example, the file system. The file system stored on the disc drive, the file and folder are in the form of
the naturally hierarchical data and stored in the form of trees.
 Organize data: It is used to organize data for efficient insertion, deletion and searching. For example, a
binary tree has a O(logn) time for searching an element.
 Trie: It is a special kind of tree that is used to store the dictionary. It is a fast and efficient way for
dynamic spell checking.
 Heap: It is also a tree data structure implemented using arrays. It is used to implement priority queues.
 B-Tree and B+Tree: B-Tree and B+Tree are the tree data structures used to implement indexing in
databases.
 Routing table: The tree data structure is also used to store the data in routing tables in the routers.

TREE REPRESENTATIONS
o A tree data structure can be represented in two methods.
o Those methods are as follows:
a) List Representation
b) Left Child - Right Sibling Representation
Consider the following tree:

a) List Representation
o In this representation, two types of nodes are used, one for representing the node with data called
'data node' and another for representing only references called 'reference node'.
o We start with a 'data node' from the root node in the tree. Then it is linked to an internal node through
a 'reference node' which is further linked to any other node directly. This process repeats for all the
nodes in the tree.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
The above example tree can be represented using List representation as follows:

b) Left Child - Right Sibling Representation


o In this representation, we use a list with one type of node which consists of three fields namely Data
field,
o Left child reference field and Right sibling reference field.
o Data field stores the actual value of a node, left reference field stores the address of the left child and
right reference field stores the address of the right sibling node.
o Graphical representation of that node is as follows:

o In this representation, every node's data field stores the actual value of that node. If that node has left
a child, then left reference field stores the address of that left child node otherwise stores NULL.
o If that node has the right sibling, then right reference field stores the address of right sibling node
otherwise stores NULL.
The above example tree can be represented using Left Child - Right Sibling representation as follows:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Why Trees?
1. One reason to use trees is, to store information that naturally forms a hierarchy. For example, the file
system on a computer which stores the files and directories in a tree fashion.
2. Trees provide moderate access/search (quicker than Linked List and slower than arrays).
3. Unlike Arrays, Trees don’t have an upper limit on number of nodes as nodes are linked using pointers.
Types of Tree data structure
The following are the types of a tree data structure:
1. General Trees
2. Binary Trees
3. Binary Search Trees
1. General tree:
The general tree is one of the types of tree data structure. In the general tree, a node can have either 0 or
maximum n number of nodes. There is no restriction imposed on the degree of the node (the number of
nodes that a node can contain). The topmost node in a general tree is known as a root node. The children of
the parent node are known as subtrees.

There can be n number of subtrees in a general tree. In the general tree, the subtrees are unordered as the
nodes in the subtree cannot be ordered.
Every non-empty tree has a downward edge, and these edges are connected to the nodes known as child
nodes. The root node is labeled with level 0. The nodes that have the same parent are known as siblings.
2. Binary tree:
Here, binary name itself suggests two numbers, i.e., 0 and 1. In a binary tree, each node in a tree can have
utmost two child nodes. Here, utmost means whether the node has 0 nodes, 1 node or 2 nodes.
Definition
A binary tree is a finite set of nodes that either is empty or consists of a root and two disjoint binary trees
called the left subtree and right subtree.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Types of Binary Trees
 Full Binary Tree: A full binary tree is a binary tree type where every node has either 0 or 2 child
nodes.
 Complete Binary Tree: A complete binary tree is a binary tree in which all the levels are
completely filled except possibly the lowest one, which is filled from the left.
 Perfect Binary Tree: A perfect binary tree is a binary tree type where all the leaf nodes are on the
same level and every node except leaf nodes have 2 children.
 Strictly binary tree: If the binary tree has each node consisting of either two nodes or no nodes at
all, then it is called a strictly binary tree.
 Skewed Binary tree: If the new nodes in the tree are added only to one side of the binary tree then
it is a skewed binary tree.
Full Binary Tree Complete Binary Tree Perfect Binary Tree

Strictly binary Tree Left Skewed Binary Tree Right Skewed Binary Tree

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
3. Binary Search Tree
A binary tree in which each node has a value greater than all values in its left subtree and less than all
values in its right subtree.
i.e., left subtree < root node ≤ right subtree.
Definition
A Binary tree T is a Binary Search Tree (BST), if each node N of T has the following property: The value at
N is greater than every value in the left subtree of N and is less than every value in the right subtree of N.

Balanced Binary Search Trees (BSTs)


 BSTs are an enhancement over standard binary search trees.
 They incorporate mechanisms to maintain balance, ensuring the tree remains efficient for operations
like search, insertion, and deletion.
 The term "balanced" in this context refers to ensuring that the tree's height (the longest path from the
root node to a leaf node) is kept as small as possible.
Types of balanced BSTs
1. AVL Trees (Adelson-Velsky and Landis Trees)
2. Red-Black Trees
3. B Trees
4. B+ Trees
5. Splay Trees

Basic Operation Of Tree Data Structure


1. Create – create a tree in the data structure.
2. Insert − Inserts data in a tree.
3. Search − Searches specific data in a tree to check whether it is present or not.
4. Traversal:
o Preorder Traversal – perform Traveling a tree in a pre-order manner in the data structure.
o In order Traversal – perform Traveling a tree in an in-order manner.
o Post-order Traversal – perform Traveling a tree in a post-order manner.

Binary tree traversals


Binary tree traversals are methods for visiting all the nodes in a binary tree in a specific order. There are several
types of traversals, each serving different purposes.
There are three ways which we use to traverse a tree
1. In-order Traversal
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.
Mob: +91-8179700193, Email: [email protected]
2. Pre-order Traversal
3. Post-order Traversal
Let's go through each of these traversal’s step by step:
1. In-Order Traversal
In an in-order traversal, the nodes are traversed in a left-root-right order. This means that for any given
node, you first visit its left subtree, then the node itself, and finally its right subtree.
In-Order Traversal Recursion and Non-Recursion Algorithm
Steps:
 Traverse the left subtree in an in-order manner.
 Visit the current node (e.g., print the node's data).
 Traverse the right subtree in an in-order manner.
Use Case: In a Binary Search Tree (BST), an in-order traversal retrieves the nodes in sorted order.
Recursion Algorithm
inorder(root):
if root ≠ NULL, then
inorder(root->left)
Print root → info
inorder(root->right)
Non-Recursion Algorithm
inorder(root):
1. If root = NULL, then print "Empty tree" and exit.
2. top = -1 //Initialize stack top]
3. ptr = root. //Initialize ptr with root of the binary tree]
4. Repeat step 5, while stack is not empty or ptr ≠ NULL //Pushes left-most path onto stack]
5. if ptr ≠ NULL, then
a. Push(ptr) //[Push current node address onto stack]
b. ptr = ptr>left //Move ptr to left subtree]
else:
c. ptr = popl //[Pop from stack, stored address and assign it to ptr
d. Print ptr → info //Print node data]
e. ptr = ptr→right //Move ptr to right subtree]
6. Exit

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
2. Pre-Order Traversal
In a pre-order traversal, the nodes are traversed in a root-left-right order. This means that you visit the node
first, then its left subtree, and finally its right subtree.

Pre-Order Traversal Recursion and Non-Recursion Algorithm

Steps:
 Visit the current node (e.g., print the node's data).
 Traverse the left subtree in a pre-order manner.
 Traverse the right subtree in a pre-order manner.
Use Case: Pre-order traversal is useful for creating a copy of the tree or for prefix notation expressions.
Recursion Algorithm
preorder(root):
if root ≠ NULL, then
Print root → info
preorder(root->left)
preorder(root->right)
Non-Recursion Algorithm
preorder(root):
1. If root = NULL, then print "Empty tree" and exit.
2. top = -1 //[Initialize stack top]
3. Push(root) //[Push root address onto stack]
4. ptr = root. //[Initialize ptr with root of the binary tree]
5. Repeat steps 6 and 7, while stack is not empty
6. ptr = pop) //[Get stored address and assign to ptr]
7. If ptr ≠ NULL, then
(a) Print ptr→info //[Print node info]
(b) Push(ptr>right) //[Store address of right subtree]
(c) Push(ptr>left) //[ Store address of left subtree]
8. Exit

3. Post-Order Traversal
In post-order traversal, the nodes are traversed in a left-right-root order. Here, you first visit the left and
right subtrees and then the node itself.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Post-Order Traversal Recursion and Non-Recursion Algorithm
Steps:
 Traverse the left subtree in a post-order manner.
 Traverse the right subtree in a post-order manner.
 Visit the current node (e.g., print the node's data).
Use Case: Post-order traversal is used in deleting the tree or in postfix notation expressions.
Recursion Algorithm
postorder(root)
if root ≠ NULL, then
postorder(root->left)
postorder(root->right)
Print root → info
Non-Recursion Algorithm
postorder(root)
1. If root = NULL, then print "Empty tree" and exit.
2. top = -1 //[Initialize stack topl
3. ptr = root. //[Initialize ptr with root of the binary tree]
4. tmp = NULL //[Initialize pointer variable, tmp to NULL]
5. Repeat steps 6 thru 8, while ptr> left ≠ NULL
6. Push (ptr)
7. ptr = ptr>left
8. Repeat steps a and b, while ptr ≠ NULL
a. If ptr>right = NULL or ptr>right = tmp
i. Print ptr> info
ii. tmp = ptr
iii. ptr = popl
b. Else:
i. Push(ptr)
ii. ptr = ptr>right
iii. Repeat steps (a) and (b), while ptr> left ≠ NULL
(a) Push(ptr)
(b) ptr = ptr->left
9. Exit

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Example tree traversals
Consider a tree with the structure:

 In-Order: D, B, E, A, C, F
 Pre-Order: A, B, D, E, C, F
 Post-Order: D, E, B, F, C, A
Each traversal serves different purposes and offers a unique view of the elements in the binary tree.

Construction of a Binary Tree from its Traversals


It is sometimes necessary to construct (or reconstruct) a binary tree from its traversals. We require a minimum
of two traversals to construct a unique binary tree. We need one of these two combinations:
1. InOrder Traversal and PreOrder Traverasal, or
2. InOrder Traversal and PostOrder Traversal
Note: A unique binary tree cannot be built with a combination of PreOrder and PostOrder traversals.

Reconstructing binary tree from Inorder and Preorder traversal


 Given an Inorder and Preorder traversal of a binary tree, it can be uniquely re-constructed.
 The first element in the Preorder traversal of a tree is the root. Whereas, in an Inorder traversal the root
of the tree lies somewhere in the middle.
 Using this knowledge, let us see how we can re-construct the binary tree.
Algorithm: Tree construction from Inorder and Preorder
Consider the following traversals of the tree.
Inorder = {2, 5, 6, 10, 12, 14, 15}
Preorder = {10, 5, 2, 6, 14, 12, 15}
Steps of construction:
1. First element in Preorder will be the root of the tree, here its 10.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
2. Now search for element 10 in inorder[], say you find it at position i, once you find it, make note of
elements which are left to i (this will construct the leftsubtree) and elements which are right to i ( this
will construct the rightsubtree).
3. See the step-2 above and recursively construct left subtree and link it root.left and recursively
construct right subtree and link it root.right.

Pictorial Representation of above questions


Consider the following traversals of the tree.
Inorder = {2, 5, 6, 10, 12, 14, 15}
Preorder = {10, 5, 2, 6, 14, 12, 15}

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Reconstructing binary tree from Inorder and Postorder traversal
 Given an Inorder and Postorder traversal of a binary tree, it can be uniquely re-constructed.
 The last element in the post order traversal of a tree is the root. Whereas, in an Inorder traversal the root
of the tree lies somewhere in the middle.
 Using this knowledge, let us see how we can re-construct the binary tree.
Algorithm: Tree construction from Inorder and Postorder
Consider the following traversals of the tree.
Inorder = { 4, 2, 5, 1, 6, 3, 7 }
Postorder = { 4, 5, 2, 6, 7, 3, 1 }
Steps of construction:
1. Last element in the postorder[] will be the root of the tree, here it is 1.
2. Now the search element 1 in inorder[], say you find it at position i, once you find it, make note of ele-
ments which are left to i (this will construct the leftsubtree) and elements which are right to i ( this
will construct the rightSubtree).
3. Suppose in previous step, there are X number of elements which are left of ‘i’ (which will construct
the leftsubtree), take first X elements from the postorder[] traversal, this will be the post order
traversal for elements which are left to i. similarly if there are Y number of elements which are right of
‘i’ (which will construct the rightsubtree), take next Y elements, after X elements from the
postorder[] traversal, this will be the post order traversal for elements which are right to i.
4. From previous two steps construct the left and right subtree and link it to root.left and root.right
respectively.

Pictorial Representation of above questions


Consider the following traversals of the tree.
Inorder = { 4, 2, 5, 1, 6, 3, 7 }
Postorder = { 4, 5, 2, 6, 7, 3, 1 }

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Binary Search Tree
 A Binary Search Tree (BST) is a special type of binary tree in which the left child of a node has a value
less than the node’s value and the right child has a value greater than the node’s value. This property is
called the BST property and it makes it possible to efficiently search, insert, and delete elements in the
tree.
 BST are used to maintain a dynamically changing dataset in sorted order, which allows for quick
lookup, addition, and removal of items.
Definition
A Binary tree T is a Binary Search Tree (BST), if each node N of T has the following property: The value at
N is greater than every value in the left subtree of N and is less than every value in the right subtree of N.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
A Binary Search Tree (BST) properties
 Binary Tree: Each node has at most two children, commonly referred to as the left child and the right
child.
 Ordered Structure: For every node N, the values of all the nodes in the left subtree of N are less than
the value of N, and the values of all the nodes in the right subtree of N are greater than the value of N.
This is known as the binary search property.
 No Duplicate Nodes: Typically, BSTs do not contain duplicate values. Each node has a distinct key or
value.
Binary search tree example:

Advantages of Binary search tree


o Searching an element in the Binary search tree is easy as we always have a hint that which subtree has
the desired element.
o As compared to array and linked lists, insertion and deletion operations are faster in BST.
Example of creating a binary search tree
Now, let's see the creation of binary search tree using an example.
Suppose the data elements are - 45, 15, 79, 90, 10, 55, 12, 20, 50
o First, we have to insert 45 into the tree as the root of the tree.
o Then, read the next element; if it is smaller than the root node, insert it as the root of the left subtree,
and move to the next element.
o Otherwise, if the element is larger than the root node, then insert it as the root of the right subtree.
Now, let's see the process of creating the Binary search tree using the given data element. The process of
creating the BST is shown below -
Step – 1: Insert 45.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Step – 2: Insert 15.
As 15 is smaller than 45, so insert it as the root node of the left subtree.

Step – 3: Insert 79.


As 79 is greater than 45, so insert it as the root node of the right subtree.

Step – 4: Insert 90.


90 is greater than 45 and 79, so it will be inserted as the right subtree of 79.

Step – 5: Insert 10.


10 is smaller than 45 and 15, so it will be inserted as a left subtree of 15.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Step – 6: Insert 55.
55 is larger than 45 and smaller than 79, so it will be inserted as the left subtree of 79.

Step – 7: Insert 12.


12 is smaller than 45 and 15 but greater than 10, so it will be inserted as the right subtree of 10.

Step – 8: Insert 20.


20 is smaller than 45 but greater than 15, so it will be inserted as the right subtree of 15.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Step – 9: Insert 50.
50 is greater than 45 but smaller than 79 and 55. So, it will be inserted as a left subtree of 55.

Now, the creation of binary search tree is completed. After that, let's move towards the operations that can be
performed on Binary search tree.
We can perform insert, delete and search operations on the binary search tree.
Let's understand how a search is performed on a binary search tree.
Searching in Binary search tree
Searching means to find or locate a specific element or node in a data structure. In Binary search tree, searching
a node is easy because elements in BST are stored in a specific order.
The steps of searching a node in Binary Search tree are listed as follows -
1. First, compare the element to be searched with the root element of the tree.
2. If root is matched with the target element, then return the node's location.
3. If it is not matched, then check whether the item is less than the root element, if it is smaller than the
root element, then move to the left subtree.
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.
Mob: +91-8179700193, Email: [email protected]
4. If it is larger than the root element, then move to the right subtree.
5. Repeat the above procedure recursively until the match is found.
6. If the element is not found or not present in the tree, then return NULL.
Algorithm to search an element in Binary search tree
Search(root, item):
Step 1 - if (item = root → data) or (root = NULL)
return root
else if (item < root → data)
return Search(root → left, item)
else
return Search(root → right, item)
END if
Step 2 - END

Now, let's understand the searching in binary tree using an example. We are taking the binary search tree
formed above. Suppose we have to find node 20 from the below tree.
Step – 1:

Step – 2:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Step – 3:

Deletion in Binary Search tree


o In a binary search tree, we must delete a node from the tree by keeping in mind that the property of BST is
not violated.
o To delete a node from BST, there are three possible situations occur –
1. The node to be deleted is the leaf node, or,
2. The node to be deleted has only one child, and,
3. The node to be deleted has two children
o Let’s understand the situations listed above in detail.
When the node to be deleted is the leaf node
o It is the simplest case to delete a node in BST. Here, we have to replace the leaf node with NULL and
simply free the allocated space.
o We can see the process to delete a leaf node from BST in the below image.
o In below image, suppose we have to delete node 90, as the node to be deleted is a leaf node, so it will be
replaced with NULL, and the allocated space for the node will be freed.

When the node to be deleted has only one child


o In this case, we have to replace the target node with its child, and then delete the child node. It means
that after replacing the target node with its child node, the child node will now contain the value to be
deleted. So, we simply have to replace the child node with NULL and free up the allocated space.
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.
Mob: +91-8179700193, Email: [email protected]
o We can see the process of deleting a node with one child from BST in the below image.
o In the below image, suppose we have to delete the node 79, as the node to be deleted has only one child,
so it will be replaced with its child 55.
o So, the replaced node 79 will now be a leaf node that can be easily deleted.

When the node to be deleted has two children


This case of deleting a node in BST is a bit complex among other two cases. In such a case, the steps to be
followed are listed as follows –
o First, find the inorder successor of the node to be deleted.
o After that, replace that node with the inorder successor until the target node is placed at the leaf of tree.
o And at last, replace the node with NULL and free up the allocated space.
The inorder successor is required when the right child of the node is not empty. We can obtain the inorder
successor by finding the minimum element in the right child of the node.
We can see the process of deleting a node with two children from BST in the below image. In the below image,
suppose we have to delete node 45 that is the root node, as the node to be deleted has two children, so it will be
replaced with its inorder successor. Now, node 45 will be at the leaf of the tree so that it can be deleted easily.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Insertion in Binary Search tree
o A new key in BST is always inserted at the leaf.
o To insert an element in BST, we have to start searching from the root node; if the node to be inserted is less
than the root node, then search for an empty location in the left subtree. Else, search for the empty location
in the right subtree and insert the data. Insert in BST is similar to searching, as we always have to maintain
the rule that the left subtree is smaller than the root, and right subtree is larger than the root.
o Now, let's see the process of inserting a node into BST using an example.

The complexity of the Binary Search tree


Let's see the time and space complexity of the Binary search tree. We will see the time complexity for insertion,
deletion, and searching operations in best case, average case, and worst case.
1. Time Complexity

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Where 'n' is the number of nodes in the given tree.
2. Space Complexity

o The space complexity of all operations of Binary search tree is O(n).

B-Trees & B+-Trees


B-Trees and B+-Trees are originating from m-way search trees.
m-way search Tree:
 We all know that Binary search trees are useful for searching, where in for searching any key value, we
start from the root and if the key value is less than the root we search for it in left side sub tree. And if
the key value is greater than the root, we search for it in the right-side sub tree.
 Example:

 In a binary search tree:


1. Each node can have only one key.
2. Each node can have at most two children.
 The m-way search trees are multi-way trees which are generalized versions of binary trees where each
node contains multiple elements. In an m-Way tree of order m, each node contains a maximum of m –
1 elements and maximum m children.
 Here, we can say that m-way search tree is an extension of binary search tree or binary search tree is a
type of m-way search tree with degree 2.
 Example m-Way Search Tree where m=5:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
The problem with m-way search trees:
 The creation process of m-way search trees is not under any control, that is there are no rules/guidelines
for the creation process, and the tree can be created as we like.
 That is there is no restriction on how the nodes and their key values, has to be inserted into the m-way
search tree.
 Because of this reason the tree may become like a regular linked list, where the insertion, deletion and
all other operations will be performed in O(n) time.
 Example: Consider a 10-way search tree an try to insert the following keys: 10, 20, 30, 40….n

 There comes the role of B-tree, we can say that B-trees are m-way search trees with guidelines.

B-Trees:
 B-Trees were introduced by Rudolf Bayer and Edward M. McCreight at Boeing Research Labs in
1970 with the name Height Balanced M-way Search Tree. Later it was named as B-Tree.
 A B-tree is a self-balancing tree where all the leaf nodes are at the same level which allows for efficient
searching, insertion and deletion of records. Because of all the leaf nodes being on the same level, the
access time of data is fixed regardless of the size of the data set.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
 In binary search tree, AVL Tree, Red-Black tree, etc., every node contains only one value(key) and a
maximum of two children. B-Tree contains more than one value and more than two children. The
number of values(keys) in a node and number of children for a node is called as order of B-Tree.

Properties of B-Tree of Order m.


 All leaf nodes must be at same level.
 All nodes except root should have at least m/2 - 1 keys.
 All non-leaf nodes except root should have at least m/2 children.
 If the root node is a non-leaf node, then it must have at least 1 key.
 A non-leaf node with m-1 keys must have m number of children.
 All keys of each node of a B tree, should be stored in the ascending order.
Algorithm or Pseudocode for Searching an Element in a B-Tree
Pseudo-code
function search(node, key):
i=1
while i <= node.n and key > node.keys[i]:
i=i+1
if i <= node.n and key == node.keys[i]:
return (node, i) // Key found
else if node.leaf:
return null // Key not found and reached leaf
else:
return search(node.children[i], key) // Go to the appropriate child

Insertion operation in B-Tree


Here's a step-by-step explanation of the insertion process in a B-tree:
 Case 0: Start at the Root
o If tree is Empty, then create a new node with key value and make it as a root node.
o If the tree is not Empty, traverse down the tree from the root to the appropriate leaf node where
the new key should be inserted. The correct node is determined by comparing the key to be
inserted with the keys in each node and following the correct child pointer.
 Case 1: Inserting into a Non-Full Leaf Node
o If the leaf node has less than m-1 keys, Insert the key into the node in its correct sorted position.
 Case 2: Inserting into a Full Leaf Node (If the leaf node is full, it must be split before the new key is
inserted.)
o Create a new node.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
o Distribute keys evenly between the original and new nodes, keeping the median key in the parent
node and make parent node as root.
o Insert the new key into the appropriate node.
o If the parent node is also full, recursively apply Case 2 to the parent until a non-full node is
reached.
Example for Creating a B Tree:
Insert the elements 5, 50, 30, 80, 90, 60, and 65 into B-Tree with an order 3

Delete Operation in B-Tree


When deleting, you have to make sure that the B tree properties are preserved. We consider 3 cases in deletion
of B trees.
Case 1: Key is in a Leaf Node (with Sufficient Keys)
 If the key k is in a leaf node N and N has more than the minimum number of keys, simply remove k
from N.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Case 2: Key is in a Leaf Node (with Minimum Keys would violate the B-tree property)
a. The leaf has a sibling that has more than the required number of keys.
o we can take a key from the parent and move a key from the left or right sibling up to the parent
(borrowing a key from an adjacent sibling).

b. The leaf does not have a sibling that has more than the required number of keys.
o We need to merge the leaf with either its left or right sibling.
o Because two leaves are being merged, we also need to take the key from the parent whose left
and right pointers used to point to these two leaves and add this key to the merged node.

Case 3: Key is in an Internal Node


a. Key in an Internal Node with a Child Having More than the Minimum Keys.
o If the key k is in an internal node N, find the predecessor k 0 of k in the subtree rooted at the left
child of k. Replace k with k0 and then delete k0 from the left child.
o If the left child has only the minimum number of keys, then can be done with the successor in the
right subtree.
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.
Mob: +91-8179700193, Email: [email protected]
b. Key in an Internal Node with Children Having Only Minimum Keys.
o If both immediate children of the internal node N that contains k have the minimum number of
keys, merge these children along with k. Then delete k from the merged node.
Note: If the Root is Left with No Keys, delete the old root and make its only child the new root of the tree. This
decreases the height of the tree.

B+ Tree
 A B+ tree is a self-balancing tree data structure that keeps data sorted and allows for efficient search,
insertion, and deletion operations. It is often used in databases and file systems to organize and access
large amounts of data.
 B+ Trees retain the basic principles of B-Trees but introduce significant modifications that enhance
performance in disk-based storage systems. These modifications include storing all data in leaf nodes
and linking these leaf nodes to facilitate efficient sequential access, which is particularly valuable for
range queries.
Properties or Characteristics of B+ Trees
 In a B+ Tree, all data records are stored at the leaf level. This contrasts with a B-tree, where data can be at
any level.
 Leaf nodes in a B+ Tree are linked together in a linked list fashion, facilitating efficient range queries
and sequential access.
 The internal nodes (non-leaf nodes) of a B+ Tree do not store data. Instead, they act as indexes
containing only keys that guide the search towards the leaf nodes.
 Due to the high degree (number of child pointers per node), B+ Trees are typically shallower than B-trees
with the same number of elements. This results in fewer disk reads, which is crucial for performance in
disk-based storage.
 All leaf nodes are at the same depth, which ensures that every access path from the root to a leaf node is
of the same length, providing consistent query performance.
 By storing values only in leaves and maintaining a high fan-out, B+ Trees use disk space more efficiently
and reduce the number of disk I/O operations needed.
 Like B-trees, B+ Trees are self-balancing. Insertions and deletions may cause redistribution of keys
among nodes or splitting/merging of nodes but maintain the tree's balance.
B+ Tree Insertion in data structure
 Case 1: Inserting into a Non-Full Leaf Node
o If the leaf node has less than m-1 keys, Insert the key into the node in its correct sorted position.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
 Case 2: Inserting into a Full Leaf Node (If the leaf node is full, it must be split before the new key is
inserted.)
o Create a new leaf node.
o Distribute keys evenly between the original and new nodes, keeping the median key in the
original node.
o Insert the new key into the appropriate node.
o Promote the median key to the parent node and make it root.
o Update sibling pointers to maintain linked leaf structure.
o If the parent node is also full, recursively apply Case 2 to the parent until a non-full node is
reached.
Insert the elements 5, 50, 30, 80, 90, 60, and 65 into B+ Tree with an order 3

Deletion in B+ Tree
Step-1: Delete the key and data from the leaves.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Step-2: if the leaf node contains less than minimum number of elements, merge down the node with its sibling
and delete the key in between them.
Step-3: if the index node contains less than minimum number of elements, merge the node with the sibling and
move down the key in between them.
Example: Delete the key 200 from the B+ Tree shown in the following figure.

200 is present in the right sub-tree of 190, after 195. delete it.

Merge the two nodes by using 195, 190, 154 and 129.

Now, element 120 is the single element present in the node which is violating the B+ Tree properties.
Therefore, we need to merge it by using 60, 78, 108 and 120.
Now, the height of B+ tree will be decreased by 1.

Difference between B-Tree and B+ trees


Feature B-Tree B+ Tree

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Node Structure All nodes, including leaf nodes, can Only leaf nodes store data, while
store both keys and data. Internal internal nodes only contain keys and
nodes also contain pointers to their pointers to child nodes.
child nodes.
Linked Leaf Nodes Leaf nodes are not linked to each Leaf nodes are linked together in a
other. doubly linked list, allowing efficient
range searches.
Key Distribution Keys are distributed throughout all Keys are only stored in leaf nodes,
nodes, including internal nodes. allowing for a more balanced
distribution of keys across the tree.
Search Search requires traversing the tree Search only involves traversing the
Performance from the root to the leaf node leaf nodes using the linked list, leading
containing the desired key. to potentially faster search
performance for range searches.
Insertion and Insertion and deletion can occur in Insertion and deletion occur only in
Deletion both internal and leaf nodes, leaf nodes, simplifying the process and
requiring splitting or merging nodes potentially reducing overhead.
to maintain balance.
Space Utilization Internal nodes store both keys and Internal nodes only store keys,
data, leading to lower space allowing for more efficient space
utilization in large trees. utilization, especially in memory-
constrained environments.
Write Performance Frequent insertions and deletions Localized insertions and deletions in
can lead to more frequent node leaf nodes can improve write
splits and merges, potentially performance compared to B-trees.
impacting write performance.
Applications Suitable for applications where data Ideal for applications requiring
access patterns are random and efficient range searches, such as
range searches are not crucial. databases, file systems, and search
engines.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
AVL Tree
 AVL trees, named after their inventors Adelson-Velsky and Landis in the year 1962, are self-
balancing binary search trees.
 In AVL trees, the balance factor and rotations are crucial for maintaining the tree's balanced structure,
which ensures efficient operations.
 In an AVL tree, the heights of the two child subtrees of any node differ by no more than one. If at any
time they differ by more than one, rebalancing is done to restore this property.
Balance Factor in AVL trees
The balance factor of a node in an AVL tree is defined as the difference in height between its left and right
subtrees. Mathematically, it is calculated as:

For an AVL tree to be balanced, the balance factor of every node must be -1, 0, or 1.
Here is what these values signify:
 0: The heights of the left and right subtrees are equal. The subtree is perfectly balanced.
 1: The left subtree is taller than the right subtree by one level.
 -1: The right subtree is taller than the left subtree by one level.
 If the balance factor of any node falls outside this range (-1, 0, 1), it indicates that the tree is unbalanced at
that node, and a rotation (or series of rotations) is required to bring it back into balance.
 An AVL tree is given in the following figure. We can see that, balance factor associated with each node is in
between -1 and +1. therefore, it is an example of AVL tree.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Complexity

Operations on AVL tree


 Due to the fact that, AVL tree is also a binary search tree therefore, all the operations are performed in
the same way as they are performed in a binary search tree.
 Searching and traversing do not lead to the violation in property of AVL tree. However, insertion and
deletion are the operations which can violate this property and therefore, they need to be revisited.

Why AVL Tree?


 AVL tree controls the height of the binary search tree by not letting it to be skewed.
 The time taken for all operations in a binary search tree of height h is O(h). However, it can be extended
to O(n) if the BST becomes skewed (i.e., worst case).
 By limiting this height to log n, AVL tree imposes an upper bound on each operation to be O(log
n) where n is the number of nodes.
Rotations in AVL trees
Rotations are operations that restructure the tree to restore its balance. There are four types of rotations used in
AVL trees:
1. L L rotation: Inserted node is in the left subtree of left subtree of A
2. R R rotation: Inserted node is in the right subtree of right subtree of A
3. L R rotation: Inserted node is in the right subtree of left subtree of A

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
4. R L rotation: Inserted node is in the left subtree of right subtree of A
Where node A is the node whose balance Factor is other than -1, 0, 1.
The first two rotations LL and RR are single rotations and the next two rotations LR and RL are double
rotations. For a tree to be unbalanced, minimum height must be at least 2, Let us understand each rotation.
1. RR Rotation
 When BST becomes unbalanced, due to a node is inserted into the right subtree of the right subtree of A,
then we perform RR rotation, RR rotation is an anticlockwise rotation, which is applied on the edge
below a node having balance factor -2

 In above example, node A has balance factor -2 because a node C is inserted in the right subtree of A
right subtree. We perform the RR rotation on the edge below A.
2. LL Rotation
 When BST becomes unbalanced, due to a node is inserted into the left subtree of the left subtree of C,
then we perform LL rotation, LL rotation is clockwise rotation, which is applied on the edge below a
node having balance factor 2.

 In above example, node C has balance factor 2 because a node A is inserted in the left subtree of C left
subtree. We perform the LL rotation on the edge below A.
3. LR Rotation
 LR rotation = RR rotation + LL rotation, i.e., first RR rotation is performed on subtree and then LL
rotation is performed on full tree, by full tree we mean the first node from the path of inserted node
whose balance factor is other than -1, 0, or 1.
 Let us understand each and every step very clearly:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.
Mob: +91-8179700193, Email: [email protected]
4. RL Rotation
 As already discussed, that double rotations are bit tougher than single rotation which has already
explained above. R L rotation = LL rotation + RR rotation, i.e., first LL rotation is performed on subtree
and then RR rotation is performed on full tree, by full tree we mean the first node from the path of
inserted node whose balance factor is other than -1, 0, or 1.
Let us understand each and every step very clearly:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Example: Construct an AVL tree having the following elements:
H, I, J, B, A, E, C, F, D, G, K, L
1. Insert H, I, J The resultant balance tree is:

 On inserting the above elements, especially in the case of H, the BST becomes unbalanced as the
Balance Factor of H is -2. Since the BST is right-skewed, we will perform RR Rotation on node H.
2. Insert B, A The resultant balance tree is:

 On inserting the above elements, especially in case of A, the BST becomes unbalanced as the Balance
Factor of H and I is 2, we consider the first node from the last inserted node i.e. H. Since the BST from
H is left-skewed, we will perform LL Rotation on node H.
3. Insert E

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
 On inserting E, BST becomes unbalanced as the Balance Factor of I is 2, since if we travel from E to I
we find that it is inserted in the left subtree of right subtree of I, we will perform LR Rotation on node I.
LR = RR + LL rotation.
a) We first perform RR rotation on node B b) We first perform LL rotation on the node I
The resultant tree after RR rotation is: The resultant balanced tree after LL rotation is:

4. Insert C, F, D

 On inserting C, F, D, BST becomes unbalanced as the Balance Factor of B and H is -2, since if we travel
from D to B we find that it is inserted in the right subtree of left subtree of B, we will perform RL
Rotation on node I. RL = LL + RR rotation.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
a) We first perform LL rotation on node E b) We then perform RR rotation on node B
The resultant tree after LL rotation is: The resultant balanced tree after RR rotation is:

5. Insert G

 On inserting G, BST become unbalanced as the Balance Factor of H is 2, since if we travel from G to H,
we find that it is inserted in the left subtree of right subtree of H, we will perform LR Rotation on node I.
LR = RR + LL rotation.
a) We first perform RR rotation on node C b) We then perform LL rotation on node H
The resultant tree after RR rotation is: The resultant balanced tree after LL rotation is:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
6. Insert K
The resultant balanced tree after RR rotation is:

 On inserting K, BST becomes unbalanced as the Balance Factor of I is -2. Since the BST is right-skewed
from I to K, hence we will perform RR Rotation on the node I.
7. Insert L
 On inserting the L tree is still balanced as the Balance Factor of each node is now either, -1, 0, +1.
Hence the tree is a Balanced AVL tree.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Red-black tree
 The red-Black tree is a binary search tree. The prerequisite of the red-black tree is that we should know
about the binary search tree. In a binary search tree, the values of the nodes in the left subtree should be
less than the value of the root node, and the values of the nodes in the right subtree should be greater
than the value of the root node.
 Each node in the Red-black tree contains an extra bit that represents a colour to ensure that the tree is
balanced during any operations performed on the tree like insertion, deletion, etc. In a binary search tree,
the searching, insertion and deletion take O(log2n) time in the average case, O(1) in the best case
and O(n) in the worst case.
Let's understand the different scenarios of a binary search tree.

 In the above tree, if we want to search the 80. We will first compare 80 with the root node. 80 is greater
than the root node, i.e., 10, so searching will be performed on the right subtree. Again, 80 is compared
with 15; 80 is greater than 15, so we move to the right of the 15, i.e., 20.
 Now, we reach the leaf node 20, and 20 is not equal to 80. Therefore, it will show that the element is not
found in the tree. After each operation, the search is divided into half. The above BST will take O(logn)
time to search the element.

 The above tree shows the right-skewed BST. If we want to search the 80 in the tree, we will compare 80
with all the nodes until we find the element or reach the leaf node. So, the above right-skewed BST will
take O(n) time to search the element.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
 In the above BST, the first one is the balanced BST, whereas the second one is the unbalanced BST. We
conclude from the above two binary search trees that a balanced tree takes less time than an unbalanced
tree for performing any operation on the tree.
 Therefore, we need a balanced tree, and the Red-Black tree is a self-balanced binary search tree. Now,
the question arises that why do we require a Red-Black tree if AVL is also a height-balanced tree. The
Red-Black tree is used because the AVL tree requires many rotations when the tree is large, whereas the
Red-Black tree requires a maximum of two rotations to balance the tree.
 The main difference between the AVL tree and the Red-Black tree is that the AVL tree is strictly
balanced, while the Red-Black tree is not completely height-balanced. So, the AVL tree is more
balanced than the Red-Black tree, but the Red-Black tree guarantees O(log2n) time for all operations
like insertion, deletion, and searching.
 Insertion is easier in the AVL tree as the AVL tree is strictly balanced, whereas deletion and searching
are easier in the Red-Black tree as the Red-Black tree requires fewer rotations.
 As the name suggests that the node is either colored in Red or Black color. Sometimes no rotation is
required, and only recoloring is needed to balance the tree.
Properties of Red-Black tree
o It is a self-balancing Binary Search tree. Here, self-balancing means that it balances the tree itself by
either doing the rotations or recoloring the nodes.
o This tree data structure is named as a Red-Black tree as each node is either Red or Black in color. Every
node store one extra information known as a bit that represents the color of the node. For example, 0 bit
denotes the black color while 1 bit denotes the red color of the node. Other information stored by the
node is similar to the binary tree, i.e., data part, left pointer and right pointer.
o In the Red-Black tree, the root node is always black in color.
o In a binary tree, we consider those nodes as the leaf which have no child. In contrast, in the Red-Black
tree, the nodes that have no child are considered the internal nodes and these nodes are connected to the
NIL nodes that are always black in color. The NIL nodes are the leaf nodes in the Red-Black tree.
o If the node is Red, then its children should be in Black color. In other words, we can say that there
should be no red-red parent-child relationship.
o Every path from a node to any of its descendant's NIL node should have same number of black nodes.
Is every AVL tree can be a Red-Black tree?
Yes, every AVL tree can be a Red-Black tree if we color each node either by Red or Black colour. But every
Red-Black tree is not an AVL because the AVL tree is strictly height-balanced while the Red-Black tree is not
completely height-balanced.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Insertion in Red Black tree
The following are some rules used to create the Red-Black tree:
1. If the tree is empty, then we create a new node as a root node with the color black.
2. If the tree is not empty, then we create a new node as a leaf node with a color red.
3. If the parent of a new node is black, then exit.
4. If the parent of a new node is Red, then we must check the color of the parent's sibling of a new node.
4a) If the color is Black, then we perform rotations and recoloring.
4b) If the color is Red then we recolor the node. We will also check whether the parents' parent of a new
node is the root node or not; if it is not a root node, we will recolor and recheck the node.
Let's understand the insertion in the Red-Black tree.
10, 18, 7, 15, 16, 30, 25, 40, 60
Step 1: Initially, the tree is empty, so we create a new node having value 10. This is the first node of the tree, so
it would be the root node of the tree. As we already discussed that root node must be black in color, which is
shown below:

Step 2: The next node is 18. As 18 is greater than 10 so it will come at the right of 10 as shown below.

We know the second rule of the Red Black tree that if the tree is not empty then the newly created node will
have the Red color. Therefore, node 18 has a Red color, as shown in the figure:
Now we verify the third rule of the Red-Black tree, i.e., the parent of the new node is black or not. In the above
figure, the parent of the node is black in color; therefore, it is a Red-Black tree.
Step 3: Now, we create the new node having value 7 with Red color. As 7 is less than 10, so it will come at the
left of 10 as shown below.

Now we verify the third rule of the Red-Black tree, i.e., the parent of the new node is black or not. As we can
observe, the parent of the node 7 is black in color, and it obeys the Red-Black tree's properties.
Step 4: The next element is 15, and 15 is greater than 10, but less than 18, so the new node will be created at the
left of node 18. The node 15 would be Red in color as the tree is not empty.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
The above tree violates the property of the Red-Black tree as it has Red-red parent-child relationship. Now we
have to apply some rule to make a Red-Black tree. The rule 4 says that if the new node's parent is Red, then we
have to check the color of the parent's sibling of a new node. The new node is node 15; the parent of the new
node is node 18 and the sibling of the parent node is node 7. As the color of the parent's sibling is Red in colour,
so we apply the rule 4b. The rule 4b says that we have to recolor both the parent and parent's sibling node. So,
both the nodes, i.e., 7 and 18, would be recolored as shown in the below figure.

We also have to check whether the parent's parent of the new node is the root node or not. As we can observe in
the above figure, the parent's parent of a new node is the root node, so we do not need to recolor it.
Step 5: The next element is 16. As 16 is greater than 10 but less than 18 and greater than 15, so node 16 will
come at the right of node 15. The tree is not empty; node 16 would be Red in color, as shown in the below
figure:

In the above figure, we can observe that it violates the property of the parent-child relationship as it has a red-
red parent-child relationship. We have to apply some rules to make a Red-Black tree. Since the new node's
parent is Red color, and the parent of the new node has no sibling, so rule 4a will be applied. The rule 4a says
that some rotations and recoloring would be performed on the tree.
Since node 16 is right of node 15 and the parent of node 15 is node 18. Node 15 is the left of node 18. Here we
have an LR relationship, so we require to perform two rotations. First, we will perform left, and then we will
perform the right rotation. The left rotation would be performed on nodes 15 and 16, where node 16 will move
upward, and node 15 will move downward. Once the left rotation is performed, the tree looks like as shown in
the below figure:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
In the above figure, we can observe that there is an LL relationship. The above tree has a Red-red conflict, so
we perform the right rotation. When we perform the right rotation, the median element would be the root node.
Once the right rotation is performed, node 16 would become the root node, and nodes 15 and 18 would be the
left child and right child, respectively, as shown in the below figure.

After rotation, node 16 and node 18 would be recolored; the color of node 16 is red, so it will change to black,
and the color of node 18 is black, so it will change to a red color as shown in the below figure:

Step 6: The next element is 30. Node 30 is inserted at the right of node 18. As the tree is not empty, so the color
of node 30 would be red.

The color of the parent and parent's sibling of a new node is Red, so rule 4b is applied. In rule 4b, we have to do
only recoloring, i.e., no rotations are required. The color of both the parent (node 18) and parent's sibling (node
15) would become black, as shown in the below image.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
We also have to check the parent's parent of the new node, whether it is a root node or not. The parent's parent
of the new node, i.e., node 30 is node 16 and node 16 is not a root node, so we will recolor the node 16 and
changes to the Red color. The parent of node 16 is node 10, and it is not in Red color, so there is no Red-red
conflict.

Step 7: The next element is 25, which we have to insert in the tree. Since 25 is greater than 10, 16, 18 but less
than 30; so, it will come at the left of node 30. As the tree is not empty, node 25 would be in Red color. Here
Red-red conflict occurs as the parent of the newly created is Red color.
Since there is no parent's sibling, so rule 4a is applied in which rotation, as well as recoloring, are performed.
First, we will perform rotations. As the newly created node is at the left of its parent and the parent node is at
the right of its parent, so the RL relationship is formed. Firstly, the right rotation is performed in which node 25
goes upwards, whereas node 30 goes downwards, as shown in the below figure.

After the first rotation, there is an RR relationship, so left rotation is performed. After right rotation, the median
element, i.e., 25 would be the root node; node 30 would be at the right of 25 and node 18 would be at the left of
node 25.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Now recoloring would be performed on nodes 25 and 18; node 25 becomes black in color, and node 18
becomes red in color.

Step 8: The next element is 40. Since 40 is greater than 10, 16, 18, 25, and 30, so node 40 will come at the right
of node 30. As the tree is not empty, node 40 would be Red in color. There is a Red-red conflict between nodes
40 and 30, so rule 4b will be applied.

As the color of parent and parent's sibling node of a new node is Red so recoloring would be performed. The
color of both the nodes would become black, as shown in the below image.
After recoloring, we also have to check the parent's parent of a new node, i.e., 25, which is not a root node, so
recoloring would be performed, and the color of node 25 changes to Red.
After recoloring, red-red conflict occurs between nodes 25 and 16. Now node 25 would be considered as the
new node. Since the parent of node 25 is red in color, and the parent's sibling is black in color, rule 4a would be
applied. Since 25 is at the right of the node 16 and 16 is at the right of its parent, so there is an RR relationship.
In the RR relationship, left rotation is performed. After left rotation, the median element 16 would be the root
node, as shown in the below figure.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
After rotation, recoloring is performed on nodes 16 and 10. The color of node 10 and node 16 changes to Red
and Black, respectively as shown in the below figure.

Step 9: The next element is 60. Since 60 is greater than 16, 25, 30, and 40, so node 60 will come at the right of
node 40. As the tree is not empty, the color of node 60 would be Red.
As we can observe in the above tree that there is a Red-red conflict occurs. The parent node is Red in color, and
there is no parent's sibling exists in the tree, so rule 4a would be applied. The first rotation would be performed.
The RR relationship exists between the nodes, so left rotation would be performed.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
When left rotation is performed, node 40 will come upwards, and node 30 will come downwards, as shown in
the below figure:

After rotation, the recoloring is performed on nodes 30 and 40. The color of node 30 would become Red, while
the color of node 40 would become black.

The above tree is a Red-Black tree as it follows all the Red-Black tree properties.
Deletion in Red Back tree
Let's understand how we can delete the particular node from the Red-Black tree. The following are the rules
used to delete the particular node from the tree:
Step 1: First, we perform BST rules for the deletion.
Step 2:
Case 1: if the node is Red, which is to be deleted, we simply delete it.
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.
Mob: +91-8179700193, Email: [email protected]
Let's understand case 1 through an example.
Suppose we want to delete node 30 from the tree, which is given below.

Initially, we are having the address of the root node. First, we will apply BST to search the node. Since 30 is
greater than 10 and 20, which means that 30 is the right child of node 20. Node 30 is a leaf node and Red in
color, so it is simply deleted from the tree.
If we want to delete the internal node that has one child. First, replace the value of the internal node with the
value of the child node and then simply delete the child node.
Let's take another example in which we want to delete the internal node, i.e., node 20.

We cannot delete the internal node; we can only replace the value of that node with another value. Node 20 is at
the right of the root node, and it is having only one child, node 30. So, node 20 is replaced with a value 30, but
the color of the node would remain the same, i.e., Black. In the end, node 20 (leaf node) is deleted from the tree.

If we want to delete the internal node that has two child nodes. In this case, we have to decide from which we
have to replace the value of the internal node (either left subtree or right subtree). We have two ways:
o In-order predecessor: We will replace with the largest value that exists in the left subtree.
o In-order successor: We will replace with the smallest value that exists in the right subtree.
Suppose we want to delete node 30 from the tree, which is shown below:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Node 30 is at the right of the root node. In this case, we will use the in-order successor. The value 38 is the
smallest value in the right subtree, so we will replace the value 30 with 38, but the node would remain the same,
i.e., Red. After replacement, the leaf node, i.e., 30, would be deleted from the tree. Since node 30 is a leaf node
and Red in color, we need to delete it (we do not have to perform any rotations or any recoloring).

Case 2: If the root node is also double black, then simply remove the double black and make it a single black.
Case 3: If the double black's sibling is black and both its children are black.
o Remove the double black node.
o Add the color of the node to the parent (P) node.
1. If the color of P is red then it becomes black.
2. If the color of P is black, then it becomes double black.
o The color of double black's sibling changes to red.
o If still double black situation arises, then we will apply other cases.
Let's understand this case through an example.
Suppose we want to delete node 15 in the below tree.

We cannot simply delete node 15 from the tree as node 15 is Black in color. Node 15 has two children, which
are nil. So, we replace the 15 value with a nil value. As node 15 and nil node are black in color, the node
becomes double black after replacement, as shown in the below figure.
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.
Mob: +91-8179700193, Email: [email protected]
In the above tree, we can observe that the double black's sibling is black in color and its children are nil,
which are also black. As the double black's sibling and its children have black so it cannot give its black color
to neither of these. Now, the double black's parent node is Red so double black's node add its black color to its
parent node. The color of the node 20 changes to black while the color of the nil node changes to a single black
as shown in the below figure.

After adding the color to its parent node, the color of the double black's sibling, i.e., node 30 changes to red as
shown in the below figure.
In the above tree, we can observe that there is no longer double black's problem exists, and it is also a Red-
Black tree.
Case 4: If double black's sibling is Red.
o Swap the color of its parent and its sibling.
o Rotate the parent node in the double black's direction.
o Reapply cases.
Let's understand this case through an example.
Suppose we want to delete node 15.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Initially, the 15 is replaced with a nil value. After replacement, the node becomes double black. Since double
black's sibling is Red so color of the node 20 changes to Red and the color of the node 30 changes to Black.
Once the swapping of the color is completed, the rotation towards the double black would be performed. The
node 30 will move upwards and the node 20 will move downwards as shown in the below figure.

In the above tree, we can observe that double black situation still exists in the tree. It satisfies the case 3 in
which double black's sibling is black as well as both its children are black. First, we remove the double black
from the node and add the black color to its parent node. At the end, the color of the double black's sibling, i.e.,
node 25 changes to Red as shown in the below figure.

In the above tree, we can observe that the double black situation has been resolved. It also satisfies the
properties of the Red Black tree.
Case 5: If double black's sibling is black, sibling's child who is far from the double black is black, but near child
to double black is red.
o Swap the color of double black's sibling and the sibling child which is nearer to the double black node.
o Rotate the sibling in the opposite direction of the double black.
o Apply case 6
Suppose we want to delete the node 1 in the below tree.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
First, we replace the value 1 with the nil value. The node becomes double black as both the nodes, i.e., 1 and nil
are black. It satisfies the case 3 that implies if DB's sibling is black and both its children are black. First, we
remove the double black of the nil node. Since the parent of DB is Black, so when the black color is added to
the parent node then it becomes double black. After adding the color, the double black's sibling color changes to
Red as shown below.

We can observe in the above screenshot that the double black problem still exists in the tree. So, we will reapply
the cases. We will apply case 5 because the sibling of node 5 is node 30, which is black in color, the child of
node 30, which is far from node 5 is black, and the child of the node 30 which is near to node 5 is Red. In this
case, first we will swap the color of node 30 and node 25 so the color of node 30 changes to Red and the color
of node 25 changes to Black as shown below.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Once the swapping of the color between the nodes is completed, we need to rotate the sibling in the opposite
direction of the double black node. In this rotation, the node 30 moves downwards while the node 25 moves
upwards as shown below.

As we can observe in the above tree that double black situation still exists. So, we need to case 6. Let's first see
what is case 6.
Case 6: If double black's sibling is black, far child is Red
o Swap the color of Parent and its sibling node.
o Rotate the parent towards the Double black's direction
o Remove Double black
o Change the Red color to black.
Now we will apply case 6 in the above example to solve the double black's situation.
In the above example, the double black is node 5, and the sibling of node 5 is node 25, which is black in color.
The far child of the double black node is node 30, which is Red in color as shown in the below figure:

First, we will swap the colors of Parent and its sibling. The parent of node 5 is node 10, and the sibling node is
node 25. The colors of both the nodes are black, so there is no swapping would occur.
In the second step, we need to rotate the parent in the double black's direction. After rotation, node 25 will move
upwards, whereas node 10 will move downwards. Once the rotation is performed, the tree would like, as shown
in the below figure:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
In the next step, we will remove double black from node 5 and node 5 will give its black color to the far child,
i.e., node 30. Therefore, the color of node 30 changes to black as shown in the below figure.

Splay Tree
The splay tree was developed by Daniel Dominic Sleator and Robert Endre Tarjan in 1985. A Splay Tree is
a self-adjusting binary search tree data structure that automatically reorganizes itself to optimize access times
for frequently accessed elements by moving them closer to the root. It accomplishes this by performing a splay
operation after every search, insert, or delete operation, bringing the accessed node to the root of the tree.

Key Features of Splay Trees


 Self-Adjusting: After any operation, the accessed node is splayed to the root of the tree, making it the
new root. This helps improve the access time for frequently accessed elements.

 No Height Balance Guarantee: Unlike AVL trees and Red-Black trees, Splay Trees do not guarantee a
balanced height. However, they exhibit a form of local balance, which means nodes that are frequently
accessed tend to move closer to the root.

Splay Tree Rotations:


Splay Trees use rotations to perform the splay operation and bring the accessed node to the root.
Zig Rotation (Right)
 This rotation moves a child node up and its parent node down, making the child the new parent.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
 It is used when the target node is the right child of its parent.

Zag Rotation (Left)


 Similar to the zig rotation, the zag rotation moves a child node up and its parent node down, but the
child becomes the left child of the new parent.
 It is used when the target node is the left child of its parent

Zig – Zig Rotation


 In a Zig-Zig rotation, two consecutive rotations are performed to move a node up two levels closer to the
root.
 The first rotation moves the target node's grandchild up and its child down.
 The second rotation moves the target node up and its previous grandchild down.
 It is used when the target node is the right child of the right child of its parent.

Zag – Zag Rotation


 In a Zag-Zag rotation, two consecutive rotations are performed to move a node up two levels closer to
the root.
 This rotation is similar to the zig-zig rotation but involves two consecutive zag rotations.
 It is used when the target node is the left child of the left child of its parent.

Zig – Zag Rotation


 In a Zig-Zag rotation, two consecutive rotations are performed to move a node up one level closer to the
root.
 This rotation combines a zig rotation followed by a zag rotation.
 It is used when the target node is the right child of the left child of its parent.
Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.
Mob: +91-8179700193, Email: [email protected]
Zag – Zig Rotation
 In a Zag-Zig rotation, two consecutive rotations are performed to move a node up one level closer to the
root.
 This rotation performs a zag rotation followed by a zig rotation.
 It is used when the target node is the left child of the right child of its parent.

Insertion in a Splay Tree:


Pseudocode
procedure splayInsert(root, key):
if root is null:
// Create a new node with the given key and make it the root
root = createNode(key)
else:
// Perform a normal BST insertion
root = bstInsert(root, key)

// Splay the newly inserted node to the root


root = splay(root, key)

function bstInsert(node, key):


// Standard BST insertion procedure
if node is null:
return createNode(key)

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
if key < node.key:
node.left = bstInsert(node.left, key)
else if key > node.key:
node.right = bstInsert(node.right, key)
return node

function splay(root, key):


if root is null or root.key == key:
return root

// Key is in the left subtree


if key < root.key:
// Key is not in the tree, return the unchanged root
if root.left is null:
return root
// Zig-Zig step: Left-left case
if key < root.left.key:
root.left.left = splay(root.left.left, key)
root = rightRotate(root)
// Zig-Zag step: Left-right case
else if key > root.left.key:
root.left.right = splay(root.left.right, key)
if root.left.right is not null:
root.left = leftRotate(root.left)
// Perform a final rotation to bring the key to the root
if root.left is not null:
root = rightRotate(root)
return root

// Key is in the right subtree


else:
// Key is not in the tree, return the unchanged root
if root.right is null:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
return root
// Zig-Zig step: Right-right case
if key > root.right.key:
root.right.right = splay(root.right.right, key)
root = leftRotate(root)
// Zig-Zag step: Right-left case
else if key < root.right.key:
root.right.left = splay(root.right.left, key)
if root.right.left is not null:
root.right = rightRotate(root.right)
// Perform a final rotation to bring the key to the root
if root.right is not null:
root = leftRotate(root)
return root

function rightRotate(node):
// Right rotation operation
newRoot = node.left
node.left = newRoot.right
newRoot.right = node
return newRoot

function leftRotate(node):
// Left rotation operation
newRoot = node.right
node.right = newRoot.left
newRoot.left = node
return newRoot

Deletion in a Splay Tree:


Pseudocode
procedure splayDelete(root, key):
if root is null:

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
// Key not found, return the unchanged root
return root

// Splay the node containing the key to the root


root = splay(root, key)

// After splaying, if the key is not at the root, it's not in the tree
if key != root.key:
return root

// Perform the actual deletion


if root.left is null:
return root.right
else:
// Find the maximum node in the left subtree
maxNode = findMax(root.left)
// Splay the maximum node to the root of the left subtree
root.left = splay(root.left, maxNode.key)
// Attach the right subtree as the right child of the maximum node
maxNode.right = root.right
return maxNode

function findMax(node):
// Find the maximum node in a subtree
while node.right is not null:
node = node.right
return node

Searching in a Splay Tree


Pseudocode
function splaySearch(root, key):
if root is null:
// Key not found, return null or a sentinel value

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
return null

// Start searching from the root


while root is not null:
if key == root.key:
// Key found, splay the node to the root
return splay(root, key)
else if key < root.key:
// Key is in the left subtree
if root.left is null:
// Key not found, splay the last accessed node (root)
return splay(root, key)
// Continue searching in the left subtree
root = root.left
else:
// Key is in the right subtree
if root.right is null:
// Key not found, splay the last accessed node (root)
return splay(root, key)
// Continue searching in the right subtree
root = root.right

// Key not found, return null or a sentinel value


return null

Insertion operation in Splay tree


In the insertion operation, we first insert the element in the tree and then perform the splaying operation on the
inserted element.
15, 10, 17, 7
Step 1: First, we insert node 15 in the tree. After insertion, we need to perform splaying. As 15 is a root node,
so we do not need to perform splaying.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
Step 2: The next element is 10. As 10 is less than 15, so node 10 will be the left child of node 15, as shown
below:
Now, we perform splaying. To make 10 as a root node, we will perform the right rotation, as shown below:

Step 3: The next element is 17. As 17 is greater than 10 and 15 so it will become the right child of node 15.
Now, we will perform splaying. As 17 is having a parent as well as a grandparent so we will perform zig zig
rotations.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
In the above figure, we can observe that 17 becomes the root node of the tree; therefore, the insertion is
completed.
Step 4: The next element is 7. As 7 is less than 17, 15, and 10, so node 7 will be left child of 10.
Now, we have to splay the tree. As 7 is having a parent as well as a grandparent so we will perform two right
rotations as shown below:

Still the node 7 is not a root node, it is a left child of the root node, i.e., 17. So, we need to perform one more
right rotation to make node 7 as a root node as shown below:

Advantages of Splay tree


o In the splay tree, we do not need to store the extra information. In contrast, in AVL trees, we need to store
the balance factor of each node that requires extra space, and Red-Black trees also require to store one
extra bit of information that denotes the colour of the node, either Red or Black.
o It is the fastest type of Binary Search tree for various practical applications. It is used in Windows NT
and GCC compilers.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]
o It provides better performance as the frequently accessed nodes will move nearer to the root node, due to
which the elements can be accessed quickly in splay trees. It is used in the cache implementation as the
recently accessed data is stored in the cache so that we do not need to go to the memory for accessing the
data, and it takes less time.
Drawback of Splay tree
The major drawback of the splay tree would be that trees are not strictly balanced, i.e., they are roughly
balanced. Sometimes the splay trees are linear, so it will take O(n) time complexity.

Mr. Mohammed Afzal, Asst. Professor in CSE (AI&ML) dept.


Mob: +91-8179700193, Email: [email protected]

You might also like