1) Introduction to Trees.
2) Basic terminologies
3) Binary tree
4) Binary tree types
5) Binary tree representation
6) Binary search tree
7) Creation of a binary tree
8) Operations on binary search tree Trees
1. Dr.M.Usha
Assistant Professor,
Department of CSE
Velammal engineering College
Velammal Engineering College
(An Autonomous Institution, Affiliated to Anna University, Chennai)
(Accredited by NAAC & NBA)
NON LINEAR DATA STRUCTURES
- BST
2. CONTENTS
1) Introduction to Trees.
2) Basic terminologies
3) Binary tree
4) Binary tree types
5) Binary tree representation
6) Binary search tree
7) Creation of a binary tree
8) Operations on binary search tree Trees
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 2
3. Nonlinear Data Structures
The data structure where data items are not organized
sequentially is called non linear data structure.
A data item in a nonlinear data structure could be
attached to several other data elements to reflect a
special relationship among them and all the data items
cannot be traversed in a single run.
Data structures like trees and graphs are some examples
of widely used nonlinear data structures.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 3
4. A tree is a nonlinear hierarchical data structure that consists
of nodes connected by edges .
It stores the information naturally in the form of hierarchical
style.
In this, the data elements can be attached to more than one
element exhibiting the hierarchical relationship which
involves the relationship between the child, parent, and
grandparent.
A tree can be empty with no nodes or a tree is a structure
consisting of one node called the root and zero or one or
more subtrees.
In a general tree, A node can have any number of children
nodes but it can have only a single parent.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 4
TREE
11. 1. Root-
The first node from where the tree originates is called as a root node.
In any tree, there must be only one root node.
We can never have multiple root nodes in a tree data structure.
2. Edge-
The connecting link between any two nodes is called as an edge.
In a tree with n number of nodes, there are exactly (n-1) number of edges.
3. Parent-
The node which has a branch from it to any other node is called as a parent node.
In other words, the node which has one or more children is called as a parent node.
In a tree, a parent node can have any number of child nodes.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 11
13. 4. Child-
The node which is a descendant of some node is called as a child node.
All the nodes except root node are child nodes.
5. Siblings-
Nodes which belong to the same parent are called as siblings.
In other words, nodes with the same parent are sibling nodes.
6. Degree-
Degree of a node is the total number of children of that node.
Degree of a tree is the highest degree of a node among all the nodes in the tree.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 13
15. 7. Internal Node-
The node which has at least one child is called as an internal node.
Internal nodes are also called as non-terminal nodes.
Every non-leaf node is an internal node.
8. Leaf Node-
The node which does not have any child is called as a leaf node.
Leaf nodes are also called as external nodes or terminal nodes.
9. Level-
In a tree, each step from top to bottom is called as level of a tree.
The level count starts with 0 and increments by 1 at each level or step.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 15
17. 10. Height-
Total number of edges that lies on the longest path from any leaf node to a
particular node is called as height of that node.
Height of a tree is the height of root node.
Height of all leaf nodes = 0
11. Depth-
Total number of edges from root node to a particular node is called as depth
of that node.
Depth of a tree is the total number of edges from root node to a leaf node in
the longest path.
Depth of the root node = 0
The terms “level” and “depth” are used interchangeably.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 17
19. 12. Subtree-
In a tree, each child from a node forms a subtree recursively.
Every child node forms a subtree on its parent node.
13. Forest-
A forest is a set of disjoint trees.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 19
20. BINARY TREE
Binary tree is a special tree data structure in which each node can have at most 2
children.
Thus, in a binary tree,each node has either 0 child or 1 child or 2 children.
Binary Tree Representation :
1. Array Representation
2. Linked Representation
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 20
21. Binary Tree using Array Representation
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 21
24. Linked List Representation of Binary Tree
Double linked list used 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...
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 24
struct node {
int data;
struct node *leftChild;
struct node *rightChild;
};
25. Types of Binary Trees-
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 25
FULL BINARY TREE
PERFECT BINARY TREE
COMPLETE BINARY TREE
SKEWED BINARY TREE
26. 1. Full / Strictly Binary Tree-
A binary tree in which every node has either 0 or 2 children is called as a Full
binary tree.
Full binary tree is also called as Strictly binary tree.
2.Perfect Binary Tree-
A Perfect binary tree is a binary tree that satisfies the following 2 properties-
Every internal node has exactly 2 children.
All the leaf nodes are at the same level.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 26
27. 3.Complete Binary Tree-
A complete binary tree is a binary tree that satisfies the following 2 properties-
All the levels are completely filled except possibly the last level.
The last level must be strictly filled from left to right.
4.Skewed Binary Tree-
A skewed binary tree is a binary tree that satisfies the following 2 properties-
All the nodes except one node has one and only one child.
The remaining node has no child.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 27
28. Properties of Binary Tree :
1. A binary tree with n nodes has n+1 null branches.
2. A tree with n nodes has exactly(n-1) edges.
3. The maximum number of nodes at level i in a binary tree is, 2i where n> =0
4. The maximum number of nodes in a perfect binary tree of height h is 2h+1 – 1 Nodes, where
h>=0.
5. The maximum number of nodes in a complete binary of height h, has between 2h to 2h+1 –1,
h>=0.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 28
29. Binary Tree Traversals
Displaying (or) visiting order of nodes in a binary tree is called as Binary Tree
Traversal.
There are three types of binary tree traversals.
In - Order Traversal
Pre - Order Traversal
Post - Order Traversal
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 29
30. 1. Inorder Traversal
Algorithm-
1. Traverse the left sub tree i.e. call Inorder (left sub tree)
2. Visit the root
3. Traverse the right sub tree i.e. call Inorder (right sub tree)
Left → Root → Right
void inorder_traversal(struct node* root) {
if(root != NULL) {
inorder_traversal(root->leftChild);
printf("%d ",root->data);
inorder_traversal(root->rightChild);
}
}
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 30
Root
Root
NULL
31. 2. Preorder Traversal
Algorithm-
1. Visit the root
2. Traverse the left sub tree i.e. call Preorder (left sub tree)
3. Traverse the right sub tree i.e. call Preorder (right sub tree)
Root → Left → Right
void pre_order_traversal(struct node* root) {
if(root != NULL) {
printf("%d ",root->data);
pre_order_traversal(root->leftChild);
pre_order_traversal(root->rightChild);
}
}
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 31
32. 3. Postorder Traversal
Algorithm-
Traverse the left sub tree i.e. call Postorder (left sub tree)
Traverse the right sub tree i.e. call Postorder (right sub tree)
Visit the root
Left → Right → Root
void post_order_traversal(struct node* root) {
if(root != NULL) {
post_order_traversal(root->leftChild);
post_order_traversal(root->rightChild);
printf("%d ", root->data);
}
}
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 32
33. BINARY SEARCH TREE
Binary Search Tree is a special kind of binary tree in which nodes
are arranged in a specific order.
The left subtree of a node contains only nodes with keys lesser
than the node’s key.
The right subtree of a node contains only nodes with keys greater
than the node’s key.
The left and right subtree each must also be a binary search tree.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 33
34. Number of Binary Search Trees
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 34
Example-
Number of distinct binary search trees possible with 3 distinct keys
= 2×3C3 / 3+1
= 6C3 / 4
= 5
35. CONT..
Create the binary search tree using the following data elements.
43, 10, 79, 90, 12, 54, 11, 9, 50
Insert 43 into the tree as the root of the tree.
Read the next element, if it is lesser than the root node element, insert it as the root of
the left sub-tree.
Otherwise, insert it as the root of the right of the right sub-tree.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 35
38. Operations on BST
Insert an element – Delete an element – Search for an element – Find the
minimum/maximum element – Find the successor/predecessor of a node.
Basic operations of a tree
Search − Searches an element in a tree.
FindMin – Find Minimum element in a tree
FindMax – Find Maximum element in a tree
Insert − Inserts an element in a tree.
Delete − deletes an element in a tree.
Pre-order Traversal − Traverses a tree in a pre-order manner.
In-order Traversal − Traverses a tree in an in-order manner.
Post-order Traversal − Traverses a tree in a post-order manner.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 38
39. 1. Search Operation
Search Operation is performed to search a particular element in the Binary Search Tree.
Rules-
For searching a given key in the BST,
Compare the key with the value of root node.
If the key is present at the root node, then return the root node.
If the key is greater than the root node value, then recur for the root node’s right
subtree.
If the key is smaller than the root node value, then recur for the root node’s left subtree.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 39
40. 9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 40
Algorithm:
Search (ROOT, ITEM)
•Step 1: IF ROOT -> DATA = ITEM OR
ROOT = NULL
Return ROOT
ELSE
IF ITEM < ROOT -> DATA
Return search(ROOT -> LEFT, ITEM)
ELSE
Return search(ROOT -> RIGHT,ITEM)
[END OF IF]
[END OF IF]
•Step 2: END
41. Example
Consider key = 45 has to be searched in the given BST-
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 41
•We start our search from the root node 25.
•As 45 > 25, so we search in 25’s right subtree.
•As 45 < 50, so we search in 50’s left subtree.
•As 45 > 35, so we search in 35’s right subtree.
•As 45 > 44, so we search in 44’s right subtree but 44 has
no subtrees.
•So, we conclude that 45 is not present in the above BST.
42. 2. Insertion Operation
Rules-
Insert function is used to add a new element in a binary search tree at appropriate
location. Insert function is to be designed in such a way that, it must node violate the
property of binary search tree at each value.
Allocate the memory for tree.
Set the data part to the value and set the left and right pointer of tree, point to NULL.
If the item to be inserted, will be the first element of the tree, then the left and right of
this node will point to NULL.
Else, check if the item is less than the root element of the tree, if this is true, then
recursively perform this operation with the left of the root.
If this is false, then perform this operation recursively with the right sub-tree of the root.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 42
43. 9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 43
Insert (ROOT, ITEM)
•Step 1: IF ROOT = NULL
Allocate memory for TREE
SET ROOT -> DATA = ITEM
SET ROOT -> LEFT = ROOT -> RIGHT = NULL
ELSE
IF ITEM < ROOT -> DATA
Insert(ROOT -> LEFT, ITEM)
ELSE
Insert(ROOT -> RIGHT, ITEM)
[END OF IF]
[END OF IF]
•Step 2: END
44. struct node* insert(struct node* node, int
data)
{
/* If the tree is empty, return a new node */
if (node == NULL)
return newNode(data);
/* Recur down the tree */
if (data < node->data)
node->left = insert(node->left, data);
else if (data > node->data)
node->right = insert(node->right, data);
return node;
}
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 44
struct node *newNode(int item)
{
struct node *temp = (struct node
*)malloc(sizeof(struct node));
temp->data = item;
temp->left = temp->right = NULL;
return temp;
}
45. Example
Consider the following example where key = 40 is inserted in the given BST-
We start searching for value 40 from the root node 100.
As 40 < 100, so we search in 100’s left subtree.
As 40 > 20, so we search in 20’s right subtree.
As 40 > 30, so we add 40 to 30’s right subtree.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 45
46. FIND MIN
Approch for finding minimum element:
Traverse the node from root to left recursively until left is NULL.
The node whose left is NULL is the node with minimum value.
int minValue(struct node* root)
{
struct node* current = root;
/* loop down to find the leftmost leaf */
while (current->left != NULL)
{
current = current->left;
}
return(current->key);
}
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 46
48. FIND MAX
Approch for finding maximum element:
Traverse the node from root to right recursively until right is NULL.
The node whose right is NULL is the node with maximum value.
int maxValue(struct node* root)
{
struct node* current = root;
/* loop down to find the leftmost leaf */
while (current->right != NULL)
{
current = current->right;
}
return(current->key);
} 9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 48
50. Time Complexity:
O(N) Worst case happens for left skewed trees in finding
the minimum value.
O(N) Worst case happens for right skewed trees in finding
the maximum value.
O(1) Best case happens for left skewed trees in finding the
maximum value.
O(1) Best case happens for right skewed trees in finding the
minimum value.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 50
51. 3. Deletion Operation
Deletion Operation is performed to delete a particular
element from the Binary Search Tree.
• Case-01: Deletion Of A Node Having No Child (Leaf
Node)
• Case-02: Deletion Of A Node Having Only One
Child
• Case-02: Deletion Of A Node Having Two Children
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 51
52. Case-01: Deletion Of A Node Having No
Child (Leaf Node)
It is the simplest case, in this case, replace the leaf node with the NULL and simple free the allocated
space.
In the following image, we are deleting the node -4, since the node is a leaf node, therefore the node
will be replaced with NULL and allocated space will be freed.
Example-Remove -4 from a BST.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 52
53. Case-02: Deletion Of A Node Having Only One
Child
In this case, replace the node with its child and delete the child node, which now
contains the value which is to be deleted. Simply replace it with the NULL and free the
allocated space.
Example :
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 53
54. Case-02: Deletion Of A Node Having Two
Children
A node with two children may be deleted from the BST in the following two ways-
Method-01:
Visit to the right subtree of the deleting node.
Pluck the least value element called as inorder successor.
Replace the deleting element with its inorder successor.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 54
55. struct node* delete_node(struct node*
root, int data)
{
if (root == NULL)
return root;
// If the key to be deleted is smaller than
the root's key,
if (data < root->data)
root->left = delete_node(root->left,
data);
// If the key to be deleted is greater than
the root's key,
else if (data > root->data)
root->right = delete_node(root->right,
data);
else
{
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 55
// node with only one child or no child
if (root->left == NULL)
{
struct node *temp = root->right;
free(root);
return temp;
}
else if (root->right == NULL)
{
struct node *temp = root->left;
free(root);
return temp;
}
// node with two children:
struct node* temp = minValueNode(root->right);
// Copy the inorder successor's content to this node
root->data = temp->data;
// Delete the inorder successor
root->right = delete_node(root->right, temp->data);
}
return root;
}
56. Algorithm
Delete (ROOT, ITEM)
Step 1: IF ROOT = NULL
Write "item not found in the tree"
// If the key to be deleted is smaller than the root's key,
then it lies in left subtree
ELSE IF ITEM < TREE -> DATA
Delete(ROOT->LEFT, ITEM)
// If the key to be deleted is greater than the root's key,
// then it lies in right subtree
ELSE IF ITEM > ROOT -> DATA
Delete(ROOT-> RIGHT, ITEM)
// node with two children: Get the inorder
predecessor(largest in the left subtree)
ELSE IF ROOT -> LEFT AND ROOT -> RIGHT
SET TEMP = findmax(TREE -> LEFT)
SET ROOT -> DATA = TEMP -> DATA
Delete(ROOT -> LEFT, TEMP -> DATA)
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 56
// node with only one child or no child
ELSE
SET TEMP = ROOT
IF ROOT -> LEFT = NULL AND ROOT->
RIGHT = NULL
SET ROOT = NULL
ELSE IF ROOT -> LEFT != NULL
SET ROOT = ROOT -> LEFT
ELSE
SET ROOT = ROOT -> RIGHT
[END OF IF]
FREE TEMP
[END OF IF]
Step 2: END
59. Example
Consider the following example where node with value = 15 is deleted from
the BST-
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 59
60. Method-02:
Visit to the left subtree of the deleting node.
Pluck the greatest value element called as inorder predecessor.
Replace the deleting element with its inorder predecessor.
Consider the following example where node with value = 15 is deleted from the BST-
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 60
61. Advantages of using binary search tree
Searching become very efficient in a binary search tree since, we
get a hint at each step, about which sub-tree contains the desired
element.
The binary search tree is considered as efficient data structure in
compare to arrays and linked lists. In searching process, it
removes half sub-tree at every step. Searching for an element in a
binary search tree takes o(log2n) time. In worst case, the time it
takes to search an element is 0(n).
It also speed up the insertion and deletion operations as compare
to that in array and linked list.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 61
62. TIME COMPLEXITY
Time complexity of all BST Operations = O(h).
Here, h = Height of binary search tree
Worst Case-
In worst case,
The binary search tree is a skewed binary search tree.
Height of the binary search tree becomes n.
So, Time complexity of BST Operations = O(n).
In this case, binary search tree is as good as unordered list with no benefits.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 62
63. Best Case-
In best case,
The binary search tree is a balanced binary search tree.
Height of the binary search tree becomes log(n).
So, Time complexity of BST Operations = O(logn).
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 63
64. APPLICATIONS -BST
1) Used to express arithmetic expressions
2) Used to evaluate expression trees.
3) Used for indexing IP addresses.
4) It is used to implement dictionary.
5) It is used to implement multilevel indexing in DATABASE.
7) To implement Huffman Coding Algorithm.
8) It is used to implement searching Algorithm.
9) Implementing routing table in router.
9/8/2021
VELAMMAL ENGINEERING COLLEGE, Dept. of CSE 64