DATA STRUCTURES
CSO102
BINARY TREE:
● A hierarchical data structure in which each node has at most two
children, referred to as the left child and the right child.
● The node at the top of the hierarchy of a tree is called the root
node.
● The nodes that hold other sub-nodes are the parent nodes.
BINARY TREE: LINKED LIST IMPLEMENTATION
BINARY TREE:
// Structure of each node of the tree
struct node {
int data;
struct node* left;
struct node* right;
};
BINARY TREE:
5
10 15
20 25 30 35
45
The depth of a node M in the tree is the length of the path from the root of the
tree to M. The height of a tree is one more than the depth of the deepest node in
the tree.
Depth of node 25 = 2
Height of node 25 = 1
● The number of edges in the path from root node to the node 25 is 2.
Therefore, depth of the node 25 is 2.
● The number of edges in the longest path connecting the node 25 to any leaf
node is 1. Therefore, height of the node 25 is 1.
BASIC OPERATIONS ON BINARY TREE:
● Inserting an element
● Removing an element
● Searching for an element
● Traversing an element
BINARY TREE TRAVERSAL:
● Preorder Traversal (current-left-right): Visit the current
node before visiting any nodes inside the left or right
subtrees. Here, the traversal is root – left child – right
child. It means that the root node is traversed first then
its left child and finally the right child.
Algorithm Preorder(tree)
1. Visit the root.
2. Traverse the left subtree, i.e., call Preorder(left->subtree)
3. Traverse the right subtree, i.e., call Preorder(right->subtree)
BINARY TREE TRAVERSAL:
● Inorder Traversal (left-current-right): Visit the current
node after visiting all nodes inside the left subtree but
before visiting any node within the right subtree. Here,
the traversal is left child – root – right child. It means
that the left child is traversed first then its root node
and finally the right child.
Algorithm Inorder(tree)
1. Traverse the left subtree, i.e., call Inorder(left->subtree)
2. Visit the root.
3. Traverse the right subtree, i.e., call Inorder(right->subtree)
BINARY TREE TRAVERSAL:
● Postorder Traversal (left-right-current): Visit the current
node after visiting all the nodes of the left and right
subtrees. Here, the traversal is left child – right child
– root. It means that the left child has traversed first
then the right child and finally its root node.
Algorithm Postorder(tree)
1. Traverse the left subtree, i.e., call Postorder(left->subtree)
2. Traverse the right subtree, i.e., call Postorder(right->subtree)
3. Visit the root
BINARY TREE TRAVERSAL:
HEIGHT OF A BINARY TREE:
5
10 15
int maxDepth(node* node) 20 25 30 35
{
if (node == NULL) 45
return 0;
else {
/* compute the depth of each subtree */
int lDepth = maxDepth(node->left);
int rDepth = maxDepth(node->right);
/* use the larger one */
if (lDepth > rDepth)
return (lDepth + 1);
else
return (rDepth + 1);
}
}
BINARY TREE TRAVERSAL:
● Preorder Traversal
void preorder (struct node *root)
{
if (root != NULL)
{
printf ("%d ", root->data);
preorder (root->left);
preorder (root->right);
}
}
BINARY TREE TRAVERSAL:
● Inorder Traversal
void inorder (struct node *root)
{
if (root != NULL)
{
inorder (root->left);
printf ("%d ", root->data);
inorder (root->right);
}
}
BINARY TREE TRAVERSAL:
● Postorder Traversal
void postorder (struct node *root)
{
if (root != NULL)
{
postorder (root->left);
postorder (root->right);
printf ("%d ", root->data);
}
}
BINARY TREE TRAVERSAL:
● To print Postorder Traversal sequence while
Preorder and Inorder Traversal sequences are
given
● To print Preorder Traversal sequence while
Postorder and Inorder Traversal sequences are
given
BINARY SEARCH TREE:
A binary tree with following properties:
● 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.
BINARY SEARCH TREE:
BINARY SEARCH TREE: Node Structure
struct Node {
int key;
struct Node *left_child;
struct Node *right_child;
};
BINARY SEARCH TREE: Operations
1. Searching
2. Insertion
3. Deletion
4. Traversal
BINARY SEARCH TREE: Searching
if (root == NULL)
return NULL;
if (num == root->key)
return root->key;
if (num < root->key)
return Binary_search(root->left)
if (num > root->key)
return Binary_search(root->right)
BINARY SEARCH TREE: Insertion
if (Node == NULL)
return create_node(key)
if (key < Node->key)
Node->left_child = insert(Node->left_child, key);
else if (key > Node->key)
Node->right_child = insert(Node->right_child, key);
return Node;
BINARY SEARCH TREE: Deletion
Case 1: Deletion of a leaf node
Case 2: Deletion of an internal node having one child
Case 3: Deletion of an internal node having two child nodes
BINARY SEARCH TREE: Sorting
1. Construct BST
2. Print inorder traversal sequence
Time Complexity: ?
BINARY SEARCH TREE TRAVERSAL:
● To print Postorder Traversal sequence while
Preorder Traversal sequence are given
● To print Preorder Traversal sequence while
Postorder Traversal sequence are given
BINARY HEAP
- A Binary Heap is a Complete Binary Tree/ Almost
Complete Binary Tree which is used to store data
efficiently to get the max or min element based on its
structure.
Types:
● Min Heap
● Max Heap
- In a Min Binary Heap, the key at the root must be minimum
among all keys present in Binary Heap. The same property
must be recursively true for all nodes in Binary Tree.
- Max Binary Heap is similar to Min Heap.