You are on page 1of 44

UNIT 3

TREE
A tree is a collection of nodes. The collection can be empty; otherwise a tree
consists of a specially designed node called root, and one or more non empty
sub trees T1, T2, …, Tk, each of whose roots are connected by a directed edge
from root.

Root

The root is the first and top most nodes in the hierarchical arrangement of
data items. A node which does not have a parent node is called root node. In
the above tree, the root is A.
Node
Each data item present in the tree is called a node. It is the basic data
structures that specifies the data information and have links to other data items.
Example node A, B, …..,,
Leaf
A node which doesn‟t have children is called leaf or Terminal node. In the
above tree B, C, H, I, K, L, M, N, P, Q are leaf node.
Siblings
Children of the same parents are said to be siblings. In the above tree B,
C, D, E, F ,G are siblings, I,J are siblings, K, L, M are siblings, P, Q are
siblings.
Path
A path from node n1 to nk is defined as a sequence of nodes n1, n2, . . .
, nk such that ni is the parent of ni+1 for 1 <= i <= k.
There is exactly only one path from the root to each node. In the above
tree the path from A to P is A, E, J, P. Where A is the parent for E, E is the
parent of J, and J is the parent of P.
Length
The length is defined as the number of edges on the path. The length for
the path A to P is 3.
Degree
i. Degree of a node
The number of sub trees of a node is called its degree. In the above tree,
degree of A is 6, degree of F is 3, and degree of B is 0.
ii. Degree of a tree
The degree of the tree is the maximum degree of any node in the tree. In
the above tree, the degree of the tree is 6.
Level
The level of a node is defined by initially letting the root be at level one.
If a node is at level L then its children are at level L+1.
In the previous tree, the level of A is 1, level of B, C, D, E, F, G is 2,
level of H, I, J, K, L, M, N is 3, and the level of P, Q is 4.
Depth
For any node n, the depth of n is the length of the unique path from root
to n. For example, the depth of root A is 0, depth of c is 1, depth of H is 2, and
the depth of P is 3
Height
For any node n, the heig ht of the node n is the length of the longest path
from n to the leaf. The height of the leaf is zero. For example, the height of
node F is 1, and the height of E is 2.
Note

The height of the tree is equal to the height of the root.

Depth of the tree is equal to the height of the tree.
Terminal node
A node with degree zero is called a terminal node. Leaf is called the
terminal node because it has the degree 0. In the previous tree node B, C, H, I,
K, L, M, N, P, Q are terminal nodes.
Non – Terminal Node
Any node whose degree is non zero is called Non – Terminal node. In
the previous tree node A, D, E, F, G, J are non terminal nodes.
Edge
An edge is a condition line which connects two adjacent node of a tree.
The line drawn from one node to another node is called edge. If a tree has N
nodes, then there are N-1 edges. For example, the previous tree has 13 nodes
and 12 edges.
Interior node
A node is said to be an interior node only if it is between root and leaves.
All non terminal nodes except the root is called interior node. For example, D,
E, F, G, J are interior nodes.
Ancestor node
A node is said to be an ancestor of another node only if it is the parent of
that node or the parent of same ancestor of that node. For example, the ancestor
of node H are A and D, and the ancestor of node F is A.
Descendent node
A node is said to be a descendent of another node if it is the child of that
node or the child of some other descendent of that node. For example, the
descendent of F are K, L, M, and the descendent of E are I, J, P, Q.

BINARY TREE
Binary Tree is a special datastructure used for data storage purposes. A binary tree
has a special condition that each node can have a maximum of two children. A
binary tree has the benefits of both an ordered array and a linked list as search is as
quick as in a sorted array and insertion or deletion operation are as fast as in linked
list.
Important Terms
Following are the important terms with respect to tree.
• Path − Path refers to the sequence of nodes along the edges of a tree.
• Root − The node at the top of the tree is called root. There is only one root per tree and
one path from the root node to any node.
• Parent − Any node except the root node has one edge upward to a node called parent.
• Child − The node below a given node connected by its edge downward is called its child
node.
• Leaf − The node which does not have any child node is called the leaf node.
• Subtree − Subtree represents the descendants of a node.
• Visiting − Visiting refers to checking the value of a node when control is on the node.
• Traversing − Traversing means passing through nodes in a specific order.
• Levels − Level of a node represents the generation of a node. If the root node is at level
0, then its next child node is at level 1, its grandchild is at level 2, and so on.
• keys − Key represents a value of a node based on which a search operation is to be
carried out for a node.

Implementation

A binary tree has at most two children; we can keep direct pointers to
them. The declaration of tree nodes is similar in structure to that for doubly
linked lists, in that a node is a structure consisting of the key information plus
two pointers (left and right) to other nodes.

Binary Tree node declaration


typedef struct tree_node *tree_ptr; struct tree_node
{
element_type element;
tree_ptr left; tree_ptr right; };
typedef tree_ptr TREE;

Types of Binary Tree

Full binary tree or proper binary tree


A binary tree is a full binary tree if all leaves are at the same level and every
non leaf node has exactly two children and it should contain maximum possible
number of nodes in all levels. A full binary tree of height h has 2h+1 – 1 nodes.

Complete binary tree


Every non leaf node has exactly two children but all leaves are not necessary at
the same level. A complete binary tree is one where all levels have the
maximum number of nodes except the last level. The last level elements should
be filled from left to right.
Comparison between General Tree and Binary Tree

Application of trees
1.Manipulate hierarchical data.
2. Make information easy to search (see tree traversal).
3. Manipulate sorted lists of data.
4. As a workflow for compositing digital images for visual effects.
5. Router algorithms
6. Form of a multi-stage decision-making (see business chess).
Other Application
1. Heap is a tree data structure which is implemented using arrays and used to
implement priority queues.
2. B-Tree and B+ Tree : They are used to implement indexing in databases.
3. Syntax Tree: Used in Compilers.
4. K-D Tree: A space partitioning tree used to organize points in K dimensional
space.
5. Trie : Used to implement dictionaries with prefix lookup.
6. Suffix Tree : For quick pattern searching in a fixed text.

Binary Tree Representations

A binary tree data structure is represented using two methods. Those methods are as follows...

1. Array Representation

2. Linked List Representation

Consider the following binary tree...


1. Array Representation of Binary Tree

In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a

binary tree.

Consider the above example of a binary tree and it is represented as follows...

To represent a binary tree of depth 'n' using array representation, we need one dimensional

array with a maximum size of 2n + 1.

2. Linked List Representation of Binary Tree

We use a double linked list to represent a binary tree. In a double linked list, every node consists

of three fields. First field for storing left child address, second for storing actual data and third for

storing right child address.

In this linked list representation, a node has the following structure...


In this linked list representation, a node has the following structure...

The above example of the binary tree represented using Linked list representation is shown as
follows...
Binary search tree ADT

Binary search tree is a binary tree in which every node X in the tree, the values
of all the keys in its left sub tree are smaller than the key value in X, and the
values of all the keys in its right sub tree are larger than the key vale in X.
Comparison between binary tree and binary search tree

Recursive routine to insert an element into a binary search tree


SearchTree Insert( ElementType X, SearchTree T )
{
if( T = = NULL )
{ /* Create and return a one-node tree */
T = malloc ( sizeof (struct TreeNode) );
if( T != NULL )
{
T-Element = X;
T->Left = T->Right = NULL;
}
}
else
{
if( X < T->Element )
T->Left = Insert( X, T->Left ); else
if( X > T->Element )
T->Right = Insert( X, T->Right );
/* else X is in the tree already. We'll do nothing */
return T;
}
Example: To insert 8, 4, 1, 6, 5, 7, 10
i. First insert element 8 which is considered as root.
ii. Next insert element 4, 4 is less than 8, traverse towards left.
iii. Next insert element 1, 1<8 traverse towards left. 1<4 traverse towards
left.
iv. To insert element 6, 6<8 traverse towards left. 6>4, place it as right child
of 4.
v. To insert element 5, 5<8 traverse towards left. 5>4, traverse towards
right. 5<6, place it as left child of 6.
vi. To insert element 7, 7<8 traverse towards left. 7>4, traverse towards right.
7>6, place it as right child of 6.
vii. To insert element 10, 10>8, place it as a right child of 8.

Delete
While deleting a node from a tree, the memory is to be released. Deletion
operation is the complex operation in the binary search tree. To delete a node
from the tree consider the following three possibilities.
Case 1: Node to be deleted is a leaf node
Case 2: Node with one child.
Case 3: Node with two children.
Case 1: Deleting the leaf node

Search the parent of the leaf node.
➢
Make the parent link of the leaf node as NULL.

Release Memory.
Example: Deletion of node 6

Case 2: Deleting the node with only one child.



Search the parent of the node to be deleted.
➢
Assign the parent link to the child node of the node to be deleted.
➢
Release the memory of the deleted node.
If a node has one child, it can be deleted by adjusting its parent pointer to point
to its child node.
Eg: Deletion of a node with one child, before and after

Case 3: Deleting a node with two children.


The general strategy is to replace the data of the node to be deleted with its
smallest data of the right sub tree (or) largest data of the left sub tree and
recursively delete the data or node.
Inorder to delete a node with two children, it can be replaced by a node whose
key is larger than any node in P‟s right sub tree to preserve the Binary Search
Tree property. The possible nodes that could replace node P are
Rule 1: The node with the largest value in P‟s left sub tree.
Rule 2: The node with the smallest value in P‟s right sub tree.
Eg: Deletion of a node 4 with two children, before and after

Deletion routine for binary search tree


SearchTree Delete( ElementType X, SearchTree T )
{
Position TmpCell;
if( T = = NULL )
Error("Element not found"); else
if( X < T->Element ) /* Go left */
T->Left = Delete( X, T->Left );
else
if( X > T->Element ) /* Go right */
T->Right = Delete( X, T->Right );
else /* Found element to be deleted */
if( T->Left && T->Right ) /* Two children */
{ /* Replace with smallest in right subtree */
TmpCell = FindMin( T->Right );
T->Element = TmpCell->Element;
T->Right = Delete( T->Element, T->Right );
}
else /* One or zero child */
{
TmpCell = T;
if( T->Left = = NULL ) /* Only a right child */
T = T->Right;
if( T->Right = = NULL ) /* Only a left child */
T = T->Left;
free(TmpCell );
}
return T;
}

Search Operation
• Search operation is performed with O(log n) time complexity in a binary
search tree.
• This operation starts from the root node. It is used whenever an element is
to be searched.
The following algorithm shows the search operation in binary search
tree:

Step 1: Read the element from the user .

Step 2: Compare this element with the value of root node in a tree.

Step 3: If element and value are matching, display "Node is Found" and
terminate the function.

Step 4: If element and value are not matching, check whether an element is
smaller or larger than a node value.

Step 5: If an element is smaller, continue the search operation in left


subtree.

Step 6: If an element is larger, continue the search operation in right


subtree.

Step 7: Repeat the same process until we found the exact element.

Step 8: If an element with search value is found, display "Element is found"


and terminate the function.

Step 9: If we reach to a leaf node and the search value is not match to a
leaf node, display "Element is not found" and terminate the function.

Traversal is a process to visit all the nodes of a tree and may print their values too.
Because, all nodes are connected via edges (links) we always start from the root
(head) node. That is, we cannot randomly access a node in a tree. There are three
ways which we use to traverse a tree −

• In-order Traversal
• Pre-order Traversal
• Post-order Traversal
Generally, we traverse a tree to search or locate a given item or key in the tree or to
print all the values it contains.

In-order Traversal
In this traversal method, the left subtree is visited first, then the root and later the
right sub-tree. We should always remember that every node may represent a
subtree itself.
If a binary tree is traversed in-order, the output will produce sorted key values in an
ascending order.

We start from A, and following in-order traversal, we move to its left subtree B. B is
also traversed in-order. The process goes on until all the nodes are visited. The
output of inorder traversal of this tree will be −
D→B→E→A→F→C→G
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Visit root node.
Step 3 − Recursively traverse right subtree.

Pre-order Traversal
In this traversal method, the root node is visited first, then the left subtree and finally
the right subtree.

We start from A, and following pre-order traversal, we first visit A itself and then
move to its left subtree B. B is also traversed pre-order. The process goes on until
all the nodes are visited. The output of pre-order traversal of this tree will be −
A→B→D→E→C→F→G
Algorithm
Until all nodes are traversed −
Step 1 − Visit root node.
Step 2 − Recursively traverse left subtree.
Step 3 − Recursively traverse right subtree.

Post-order Traversal
In this traversal method, the root node is visited last, hence the name. First we
traverse the left subtree, then the right subtree and finally the root node.
We start from A, and following Post-order traversal, we first visit the left
subtree B. B is also traversed post-order. The process goes on until all the nodes
are visited. The output of post-order traversal of this tree will be −
D→E→B→F→G→C→A
Algorithm
Until all nodes are traversed −
Step 1 − Recursively traverse left subtree.
Step 2 − Recursively traverse right subtree.
Step 3 − Visit root node

One more example:

#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *rlink;
struct node *llink;
}*tmp=NULL;
typedef struct node NODE;
NODE *create();
void preorder(NODE *);
void inorder(NODE *);
void postorder(NODE *);
void insert(NODE *);
int main()
{
int n,i,ch;
do
{
printf("\n\n1.Create\n\n2.Insert\n\n3.Preorder\n\n4.Postorder\n\n5.I
norder\n\n6.Exit\n\n");
printf("\n\nEnter Your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:
tmp=create();
break;
case 2:
insert(tmp);
break;
case 3:
printf("\n\nDisplay Tree in Preorder Traversal : ");
preorder(tmp);
break;
case 4:
printf("\n\nDisplay Tree in Postorder Traversal : ");
postorder(tmp);
break;
case 5:
printf("\n\nDisplay Tree in Inorder Traversal : ");
inorder(tmp);
break;
case 6:
exit(0);
default:
printf("\n Inavild Choice..");
}
}
while(n!=5);
}
void insert(NODE *root)
{
NODE *newnode;
if(root==NULL)
{
newnode=create();
root=newnode;
}
else
{
newnode=create();
while(1)
{
if(newnode->data<root->data)
{
if(root->llink==NULL)
{
root->llink=newnode;
break;
}
root=root->llink;
}
if(newnode->data>root->data)
{
if(root->rlink==NULL)
{
root->rlink=newnode;
break;
}
root=root->rlink;
}
}
}
}
NODE *create()
{
NODE *newnode;
int n;
newnode=(NODE *)malloc(sizeof(NODE));
printf("\n\nEnter the Data ");
scanf("%d",&n);
newnode->data=n;
newnode->llink=NULL;
newnode->rlink=NULL;
return(newnode);
}
void postorder(NODE *tmp)
{
if(tmp!=NULL)
{
postorder(tmp->llink);
postorder(tmp->rlink);
printf("%d->",tmp->data);
}
}
void inorder(NODE *tmp)
{
if(tmp!=NULL)
{
inorder(tmp->llink);
printf("%d->",tmp->data);
inorder(tmp->rlink);
}
}
void preorder(NODE *tmp)
{
if(tmp!=NULL)
{
printf("%d->",tmp->data);
preorder(tmp->llink);
preorder(tmp->rlink);
}
}

Expression Tree
Expression tree is a binary tree in which each internal node corresponds to operator
and each leaf node corresponds to operand so for example expression tree for 3 +
((5+9)*2) would be:
Inorder traversal of expression tree produces infix version of given postfix expression
(same with preorder traversal it gives prefix expression)
Construction of Expression Tree:
Now For constructing expression tree we use a stack. We loop through input expression and
do following for every character.
1) If character is operand push that into stack
2) If character is operator pop two values from stack make them its child and push current
node again.
At the end only element of stack will be root of expression tree.

AVL Tree Datastructure

AVL tree is a height-balanced binary search tree. That means, an AVL tree is also a binary
search tree but it is a balanced tree. A binary tree is said to be balanced if, the difference

between the heights of left and right subtrees of every node in the tree is either -1, 0 or +1. In

other words, a binary tree is said to be balanced if the height of left and right children of

every node differ by either -1, 0 or +1. In an AVL tree, every node maintains an extra

information known as balance factor. The AVL tree was introduced in the year 1962 by

G.M. Adelson-Velsky and E.M. Landis.

An AVL tree is defined as follows...

An AVL tree is a balanced binary search tree. In an AVL tree, balance factor of every

node is either -1, 0 or +1.


Balance factor of a node is the difference between the heights of the left and right subtrees of
that node. The balance factor of a node is calculated either height of left subtree - height of
right subtree (OR) height of right subtree - height of left subtree. In the following
explanation, we calculate as follows...

Balance factor = heightOfLeftSubtree - heightOfRightSubtree


Example of AVL Tree

The above tree is a binary search tree and every node is satisfying balance factor condition. So
this tree is said to be an AVL tree

Every AVL Tree is a binary search tree but every Binary Search Tree need not be AVL tree.

AVL Tree Rotations


In AVL tree, after performing operations like insertion and deletion we need to check the balance
factor of every node in the tree. If every node satisfies the balance factor condition then we
conclude the operation otherwise we must make it balanced. Whenever the tree becomes
imbalanced due to any operation we use rotation operations to make the tree balanced.

Rotation operations are used to make the tree balanced.


Rotation is the process of moving nodes either to left or to right to make the tree
balanced.

There are four rotations and they are classified into two types.
Single Left Rotation (LL Rotation)
In LL Rotation, every node moves one position to left from the current position. To understand LL
Rotation, let us consider the following insertion operation in AVL Tree...

Single Right Rotation (RR Rotation)


In RR Rotation, every node moves one position to right from the current position. To understand
RR Rotation, let us consider the following insertion operation in AVL Tree...

Left Right Rotation (LR Rotation)


The LR Rotation is a sequence of single left rotation followed by a single right rotation. In LR
Rotation, at first, every node moves one position to the left and one position to right from the
current position. To understand LR Rotation, let us consider the following insertion operation in
AVL Tree...
Right Left Rotation (RL Rotation)
The RL Rotation is sequence of single right rotation followed by single left rotation. In RL
Rotation, at first every node moves one position to right and one position to left from the current
position. To understand RL Rotation, let us consider the following insertion operation in AVL
Tree...

Operations on an AVL Tree


The following operations are performed on AVL tree...

1. Search
2. Insertion
3. Deletion

Search Operation in AVL Tree


In an AVL tree, the search operation is performed with O(log n) time complexity. The search
operation in the AVL tree is similar to the search operation in a Binary search tree. We use the
following steps to search an element in AVL tree...

• Step 1 - Read the search element from the user.


• Step 2 - Compare the search element with the value of root node in the tree.
• Step 3 - If both are matched, then display "Given node is found!!!" and terminate the
function

• Step 4 - If both are not matched, then check whether search element is smaller or larger
than that node value.
• Step 5 - If search element is smaller, then continue the search process in left subtree.
• Step 6 - If search element is larger, then continue the search process in right subtree.
• Step 7 - Repeat the same until we find the exact element or until the search element is
compared with the leaf node.
• Step 8 - If we reach to the node having the value equal to the search value, then display
"Element is found" and terminate the function.
• Step 9 - If we reach to the leaf node and if it is also not matched with the search element,
then display "Element is not found" and terminate the function.

Insertion Operation in AVL Tree


In an AVL tree, the insertion operation is performed with O(log n) time complexity. In AVL Tree,
a new node is always inserted as a leaf node. The insertion operation is performed as follows...

• Step 1 - Insert the new element into the tree using Binary Search Tree insertion logic.
• Step 2 - After insertion, check the Balance Factor of every node.
• Step 3 - If the Balance Factor of every node is 0 or 1 or -1 then go for next operation.
• Step 4 - If the Balance Factor of any node is other than 0 or 1 or -1 then that tree is said
to be imbalanced. In this case, perform suitable Rotation to make it balanced and go for
next operation.

Example: Construct an AVL Tree by inserting


numbers from 1 to 8.
Deletion Operation in AVL Tree
The deletion operation in AVL Tree is similar to deletion operation in BST. But after every
deletion operation, we need to check with the Balance Factor condition. If the tree is balanced
after deletion go for next operation otherwise perform suitable rotation to make the tree
Balanced.

B - Tree Datastructure
In search trees like binary search tree, AVL Tree, Red-Black tree, etc., every
node contains only one value (key) and a maximum of two children. But
there is a special type of search tree called B-Tree in which a node contains
more than one value (key) and more than two children. B-Tree was
developed in the year 1972 by Bayer and McCreight with the
name Height Balanced m-way Search Tree. Later it was named as B-Tree.

B-Tree can be defined as follows...

B-Tree is a self-balanced search tree in which every node contains multiple


keys and has more than two children.
Here, the number of keys in a node and number of children for a node depends on the order of
B-Tree. Every B-Tree has an order.

B-Tree of Order m has the following properties...

• Property #1 - All leaf nodes must be at same level.


• Property #2 - All nodes except root must have at least [m/2]-1 keys and maximum of m-
1 keys.
• Property #3 - All non leaf nodes except root (i.e. all internal nodes) must have at
least m/2 children.
• Property #4 - If the root node is a non leaf node, then it must have atleast 2 children.
• Property #5 - A non leaf node with n-1 keys must have n number of children.
• Property #6 - All the key values in a node must be in Ascending Order.

For example, B-Tree of Order 4 contains a maximum of 3 key values in a node and maximum of
4 children for a node.

Example

Operations on a B-Tree
The following operations are performed on a B-Tree...

1. Search
2. Insertion
3. Deletion

Search Operation in B-Tree


The search operation in B-Tree is similar to the search operation in Binary Search Tree. In a
Binary search tree, the search process starts from the root node and we make a 2-way decision
every time (we go to either left subtree or right subtree). In B-Tree also search process starts
from the root node but here we make an n-way decision every time. Where 'n' is the total number
of children the node has. In a B-Tree, the search operation is performed with O(log n) time
complexity. The search operation is performed as follows...

• Step 1 - Read the search element from the user.


• Step 2 - Compare the search element with first key value of root node in the tree.
• Step 3 - If both are matched, then display "Given node is found!!!" and terminate the
function
• Step 4 - If both are not matched, then check whether search element is smaller or larger
than that key value.
• Step 5 - If search element is smaller, then continue the search process in left subtree.
• Step 6 - If search element is larger, then compare the search element with next key
value in the same node and repeate steps 3, 4, 5 and 6 until we find the exact match or
until the search element is compared with last key value in the leaf node.
• Step 7 - If the last key value in the leaf node is also not matched then display "Element is
not found" and terminate the function.
Insertion Operation in B-Tree
In a B-Tree, a new element must be added only at the leaf node. That means, the new keyValue
is always attached to the leaf node only. The insertion operation is performed as follows...

• Step 1 - Check whether tree is Empty.


• Step 2 - If tree is Empty, then create a new node with new key value and insert it into the
tree as a root node.
• Step 3 - If tree is Not Empty, then find the suitable leaf node to which the new key value
is added using Binary Search Tree logic.
• Step 4 - If that leaf node has empty position, add the new key value to that leaf node in
ascending order of key value within the node.
• Step 5 - If that leaf node is already full, split that leaf node by sending middle value to its
parent node. Repeat the same until the sending value is fixed into a node.
• Step 6 - If the spilting is performed at root node then the middle value becomes new root
node for the tree and the height of the tree is increased by one.

Example
Construct a B-Tree of Order 3 by inserting numbers from 1 to 10.
Red - Black Tree Datastructure

Red - Black Tree is another variant of Binary Search Tree in which every node is colored either

RED or BLACK. We can define a Red Black Tree as follows...

Red Black Tree is a Binary Search Tree in which every node is colored either RED or

BLACK.

Properties of Red Black Tree


• Property #1: Red - Black Tree must be a Binary Search Tree.
• Property #2: The ROOT node must be colored BLACK.
• Property #3: The children of Red colored node must be colored BLACK. (There should
not be two consecutive RED nodes).
• Property #4: In all the paths of the tree, there should be same number of BLACK colored
nodes.
• Property #5: Every new node must be inserted with RED color.
• Property #6: Every leaf (e.i. NULL node) must be colored BLACK.

Example
Following is a Red-Black Tree which is created by inserting numbers from 1 to 9.

The above tree is a Red-Black tree where every node is satisfying all the properties of Red-Black
Tree.

Every Red Black Tree is a binary search tree but every Binary Search Tree need not be
Red Black tree.
Insertion into RED BLACK Tree
In a Red-Black Tree, every new node must be inserted with the color RED. The insertion
operation in Red Black Tree is similar to insertion operation in Binary Search Tree. But it is
inserted with a color property. After every insertion operation, we need to check all the properties
of Red-Black Tree. If all the properties are satisfied then we go to next operation otherwise we
perform the following operation to make it Red Black Tree.

• 1. Recolor
• 2. Rotation
• 3. Rotation followed by Recolor

The insertion operation in Red Black tree is performed using the following steps...

• Step 1 - Check whether tree is Empty.


• Step 2 - If tree is Empty then insert the newNode as Root node with color Black and exit
from the operation.
• Step 3 - If tree is not Empty then insert the newNode as leaf node with color Red.
• Step 4 - If the parent of newNode is Black then exit from the operation.
• Step 5 - If the parent of newNode is Red then check the color of parentnode's sibling of
newNode.
• Step 6 - If it is colored Black or NULL then make suitable Rotation and Recolor it.
• Step 7 - If it is colored Red then perform Recolor. Repeat the same until tree becomes
Red Black Tree.

Example
Deletion Operation in Red Black Tree
The deletion operation in Red-Black Tree is similar to deletion operation in BST. But after every
deletion operation, we need to check with the Red-Black Tree properties. If any of the properties
are violated then make suitable operations like Recolor, Rotation and Rotation followed by
Recolor to make it Red-Black Tree.

Splay Tree Datastructure

Splay tree is another variant of a binary search tree. In a splay tree, recently accessed element is

placed at the root of the tree. A splay tree is defined as follows...

Splay Tree is a self - adjusted Binary Search Tree in which every operation on element

rearranges the tree so that the element is placed at the root position of the tree.
In a splay tree, every operation is performed at the root of the tree. All the operations in splay
tree are involved with a common operation called "Splaying".
Splaying an element, is the process of bringing it to the root position by performing
suitable rotation operations.
In a splay tree, splaying an element rearranges all the elements in the tree so that splayed
element is placed at the root of the tree.

By splaying elements we bring more frequently used elements closer to the root of the tree so
that any operation on those elements is performed quickly. That means the splaying operation
automatically brings more frequently used elements closer to the root of the tree.

Every operation on splay tree performs the splaying operation. For example, the insertion
operation first inserts the new element using the binary search tree insertion process, then the
newly inserted element is splayed so that it is placed at the root of the tree. The search operation
in a splay tree is nothing but searching the element using binary search process and then
splaying that searched element so that it is placed at the root of the tree.
In splay tree, to splay any element we use the following rotation operations...

Rotations in Splay Tree


• 1. Zig Rotation
• 2. Zag Rotation
• 3. Zig - Zig Rotation
• 4. Zag - Zag Rotation
• 5. Zig - Zag Rotation
• 6. Zag - Zig Rotation

Example

Zig Rotation
The Zig Rotation in splay tree is similar to the single right rotation in AVL Tree rotations. In zig
rotation, every node moves one position to the right from its current position. Consider the
following example...
Zig-Zig Rotation
The Zig-Zig Rotation in splay tree is a double zig rotation. In zig-zig rotation, every node moves
two positions to the right from its current position. Consider the following example...

Zag-Zag Rotation
The Zag-Zag Rotation in splay tree is a double zag rotation. In zag-zag rotation, every node
moves two positions to the left from its current position. Consider the following example...
Zag-Zig Rotation
The Zag-Zig Rotation in splay tree is a sequence of zag rotation followed by zig rotation. In zag-
zig rotation, every node moves one position to the left followed by one position to the right from
its current position. Consider the following example...

Every Splay tree must be a binary search tree but it is need not to be balanced tree.

Insertion Operation in Splay Tree


The insertion operation in Splay tree is performed using following steps...

• Step 1 - Check whether tree is Empty.


• Step 2 - If tree is Empty then insert the newNode as Root node and exit from the
operation.
• Step 3 - If tree is not Empty then insert the newNode as leaf node using Binary Search
tree insertion logic.
• Step 4 - After insertion, Splay the newNode

Deletion Operation in Splay Tree


The deletion operation in splay tree is similar to deletion operation in Binary Search Tree. But
before deleting the element, we first need to splay that element and then delete it from the root
position. Finally join the remaining tree using binary search tree logic.

Tries

All the search trees are used to store the collection of numerical values but they are not suitable

for storing the collection of words or strings. Trie is a data structure which is used to store the

collection of strings and makes searching of a pattern in words more easy. The term trie came

from the word retrieval. Trie data structure makes retrieval of a string from the collection of

strings more easily. Trie is also called as Prefix Tree and some times Digital Tree. A trie is

defined as follows...
Trie is a tree like data structure used to store collection of strings.

A trie can also be defined as follows...

Trie is an efficient information storage and retrieval data structure.

The trie data structure provides fast pattern matching for string data values. Using trie, we bring

the search complexity of a string to the optimal limit. A trie searches a string in O(m) time

complexity, where m is the length of the string.

In trie, every node except the root stores a character value. Every node in trie can have one or a

number of children. All the children of a node are alphabetically ordered. If any two strings have a

common prefix then they will have the same ancestors.

Example
2-3 Trees | (Search and Insert)
2-3 tree is a tree data structure in which every internal node (non-leaf node) has
either one data element and two children or two data elements and three children. If
a node contains one data element leftVal, it has two subtrees (children)
namely left and middle. Whereas if a node contains two data
elements leftVal and rightVal, it has three subtrees namely left, middle and right.
The main advantage with 2-3 trees is that it is balanced in nature as opposed to a
binary search tree whose height in the worst case can be O(n). Due to this, the worst
case time-complexity of operations such as search, insertion and deletion is as the
height of a 2-3 tree is .
Search: To search a key K in given 2-3 tree T, we follow the following procedure:
Base cases:
1. If T is empty, return False (key cannot be found in the tree).
2. If current node contains data value which is equal to K, return True.
3. If we reach the leaf-node and it doesn’t contain the required key value K, return
False.
Recursive Calls:
1. If K < currentNode.leftVal, we explore the left subtree of the current node.
2. Else if currentNode.leftVal < K < currentNode.rightVal, we explore the middle
subtree of the current node.
3. Else if K > currentNode.rightVal, we explore the right subtree of the current
node.

Consider the following example:


Insertion: There are 3 possible cases in insertion which have been discussed
below:
Case 1: Insert in a
node with only one
data element

Case 2: Insert in a node with two data elements whose parent contains only one data
element.
Case 3: Insert in a node with two data elements whose parent also contains two
data elements.

You might also like