Unit 4
Unit 4
Tree
Tree Definition
• Inorder: 2 3 6 7 8 10 11
12 14 15 20 22 27 30 6 10 22 30
12
• Postorder: 3 7 6 2 10 14
12 11 8 22 30 27 20 15 3 7 14
Algorithm for Traversal
• Preorder traversal Algorithm:
Until all nodes of the tree are no
t visited
• Step 1 - Visit the root node
• Step 2 -
Traverse the left subtree rec
ursively.
• Step 3 -
Traverse the right subtree re
cursively.
Postorder traversal
• Step 1 -
Traverse the left subtree
recursively.
• Step 2 -
Traverse the right subtre
e recursively.
• Step 3 -
Visit the root node.
Inorder traversal
• Step 1 -
Traverse the left subtree
recursively.
• Step 2 -
Visit the root node.
• Step 3 -
Traverse the right subtre
e recursively.
Write a program to implement tree
traversal techniques in C.
• #include <stdio.h>
• #include <stdlib.h>
•
• struct node {
• int element;
• struct node* left;
• struct node* right;
• };
Contd..
• /*To create a new node*/
• struct node* createNode(int val)
• {
• struct node* Node = (struct node*)malloc(si
zeof(struct node));
• Node->element = val;
• Node->left = NULL;
• Node->right = NULL;
•
• return (Node);
• }
/*function to traverse the nodes of binary tree in preorder*/
void traversePreorder(struct node* root)
{
if (root == NULL)
return;
printf(" %d ", root->element);
traversePreorder(root->left);
traversePreorder(root->right);
}
return 0;
}
What is a Binary Search tree?
• A binary search tree follows
some order to arrange the
elements. In a Binary search
tree, the value of left node
must be smaller than the
parent node, and the value of
right node must be greater
than the parent node. This
rule is applied recursively to
the left and right subtrees of
the root.
• Let's understand the concept
of Binary search tree with an
example.
In the above figure, we can observe that the root node is 40, and all the nodes of the
left subtree are smaller than the root node, and all the nodes of the right subtree are
greater than the root node.
Example of creating a binary search
tree
• Suppose the data elements are - 45, 15, 79,
90, 10, 55, 12, 20, 50
• First, we have to insert 45 into the tree as the
root of the tree.
• 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.
• Otherwise, if the element is larger than the root
node, then insert it as the root of the right
subtree.
Contd…
• Step 1 - Insert 45. • Step 3 - Insert 79.
• As 79 is greater than 45,
so insert it as the root
node of the right subtree.
• 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
Contd..
• 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.
Contd..
• LR Rotation
• Double rotations are bit tougher than single rotation
which has already explained above. 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:
Contd..
Contd..
• 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:
Construct an AVL tree having the following elements
• H, I, J, B, A, E, C, F, D, G, K,
L