ICS121 - Data Structures I - Binary Tree
ICS121 - Data Structures I - Binary Tree
Dr. E.Silambarasan
Assistant Professor
Department of Cyber Security
Indian Institute of Information technology, Kottayam
Binary Trees – Insertion and Deletion of nodes, Tree Traversals,
Polish Notations, Red Black Trees, B-Trees, Heaps, Priority Queues.
Optimal binary search tree, Application problems on Optimal binary
search Tree
Tree:
• A tree is a non-linear data structure in which data is organized in branches.
• The data elements in tree are arranged in a sorted order.
• It imposes a hierarchical structure on the data elements.
• A Tree is a collection of nodes.
• The collection can be empty also.
• There is a specially designated node called Root Node.
• The Remaining nodes are partitioned in sub-trees like T1, T2,…Tn.
• The tree will have unique path from root node to its children or leaf node.
• The tree does not have cycle.
• Construct tree:
• The root of the tree is the node 60 at the top.
• Node 29 and 44 are the successors of the node 60.
• The nodes 6, 4, 12 and 67 are the terminal nodes.
Types:
Full Binary Tree/ Proper Binary Tree:
• A full Binary tree is a special type of binary tree in which every parent node/internal node has
either two or no children.
deleteTree(struct BTnode*root){
if(root == NULL)
return;
/* Delete Left sub-tree */
deleteTree(root->left);
struct BSTNode {
{ BSTNode *temp;
}; temp->right = NULL;
return temp;
}
Binary Search Tree: else if (key < prev->key)
Insertion Operation: {
insert(BSTNode* &root, int key) prev->left = toinsert;
{ }
BSTNode* toinsert = newNode(key); else
BSTNode* curr = root; {
BSTNode* prev = NULL; prev->right = toinsert;
while (curr != NULL) }
{ }
prev = curr;
if (key < curr->key)
curr = curr->left;
else
curr = curr->right;
}
if (prev == NULL)
{
prev = toinsert;
root = prev;
}
Binary Search Tree:
Search Operation:
SEARCH(x, root)
if(root != null)
if(root->data == x)
write “search element found”
else if(root->data > x)
return SEARCH(x, root->left)
else if(root->data < x)
return SEARCH(x, root->right)
else
write” Search element is not found”
Binary Search Tree:
Finding Maximum/Minimum element Operation:
//deletion at root
else if(root->data==val)
{
//no child
if(root->left == NULL && root->right == NULL)
{
free(root);
}
Binary Search Tree:
Deletion Operation:
//one child
else if (root->left == NULL)
{
BSTNode* temp = root;
root=root->right;
free(temp);
}
else if (root->right == NULL)
{
BSTNode* temp = root;
root=root->left;
free(temp)
}
Binary Search Tree:
Deletion Operation:
//two children
else
{
BSTNode* temp = getMin(root->right);
root->data=temp->data;
root->right=deleteNode(root->right,temp->data); //removing duplicate
}
}
return root;
}
Height Balanced Binary Search Tree or AVL Tree:
• Adelson, Velskei and Landis
• Balancing Factor : -1, 0, 1
• Formula for Balancing Factor = Left subtree height – right subtree height
• Skewed Binary Search Tree - Search Complexity is O(h) or O(n)
• For balanced BST - Search Complexity is O(log n)
• AVL Tree performs 4 kinds of rotation for balancing the BST
• Single rotation
• Left rotation
• Right rotation
• Double Rotation
• Left-Right rotation
• Right- Left rotation
AVL Rotations: Single Rotation
Left Rotation
Right Rotation
AVL Rotations: Double Rotation
Left-Right Rotation
// Perform rotation
y->left = x;
x->right = T2;
// Update heights
x->height = max(height(x->left), height(x->right)) + 1;
y->height = max(height(y->left), height(y->right)) + 1;
// Perform rotation
x->right = y;
y->left = T2;
// Update heights
y->height = max(height(y->left), height(y->right)) + 1;
x->height = max(height(x->left), height(x->right)) + 1;
// Left Rotation
if (balance < -1 && val > node->right->data)
return leftRotate(node);
• Basic red-black tree with the sentinel nodes added. Implementations of the red-black tree algorithms
will usually include the sentinel nodes as a convenient means of flagging that you have reached a leaf
node.
Insertion:
1. Recolor
2. Rotation
3. Rotation followed by Recolor
Steps:
1. Check whether tree is Empty.
2. If tree is Empty then insert the newNode as Root node with color Black and exit
from the operation.
3. If tree is not Empty then insert the newNode as leaf node with color Red.
4. If the parent of newNode is Black then exit from the operation.
5. If the parent of newNode is Red then check the color of parent node's sibling of
newNode.
1. If it is colored Black or NULL then make suitable Rotation and Recolor it.
2. If it is colored Red then perform Recolor and also check if grand parent of newNode is not root
node then recolor it and recheck.
Example: 8, 18, 5, 15, 17, 25, 40, 80
10,18, 7, 15, 16, 30, 25, 40, 60, 2, 1, 70
21, 17, 80, 5, 20, 25,98,2, 40
21,11,35,51,60
Red Black Tree – Deletion: o Reapply cases.
• Perform BST deletion • Case 5: DB’s S is black, S’s child who is far from DB is
black but near child to DB is red.
• Case 1: If the node to be deleted is red, just delete it
o Swap the colour of DB’s S and S’s child who is near
• Case 2: If root is Double Black (DB), just remove DB. to DB
o Rotate S in opposite direction to DB
• Case 3: If DB’s Sibling(S) is black and both the children
are black o Apply case 6