Tree
Tree
1. Tree is a hierarchical data structure which stores the information naturally in the form
of hierarchy style.
2. Tree is one of the most powerful and advanced data structures.
3. It is a non-linear data structure compared to arrays, linked lists, stack and queue.
4. It represents the nodes connected by edges.
A is a parent of B and C.
B is called a child of A and also parent of D, E, F.
Note:-
-If node has no children, it is called Leaves or External Nodes.
- Nodes which are not leaves, are called Internal Nodes. Internal nodes have at
least one child.
- A tree can be empty with no nodes or a tree consists of one node called the
Root.
Height of a Node
height of a node is a number of edges on the longest path between that node and a leaf.
Each node has height.
In the above figure, A, B, C, D can have height. Leaf cannot have height as there will be
no path starting from a leaf. Node A's height is the number of edges of the path to K not to
D. And its height is 3.
Note:
- Height of a node defines the longest path from the node to a leaf.
- Path can only be downward.
Binary Tree
"A tree in which every node can have maximum of two children is called as Binary
Tree."
Binary Tree Traversal
Binary tree traversing is a process of accessing every node of the tree and exactly once.
A tree is defined in a recursive manner. Binary tree traversal also defined recursively.
1. Preorder Traversal
2. Postorder Traversal
3. Inorder Traversal
Algorithm for preorder traversal
Step 1 : Start from the Root.
Step 2 : A + B + D (E + F) + C (G + H)
Step 3 : A + B + D + E + F + C + G + H
Preorder Traversal : A B C D E F G H
Algorithm for postorder traversal
Step 1 : Start from the Left Subtree (Last Leaf).
Step 2 : (E + F) + D + B + (G + H) + C + A
Step 3 : E + F + D + B + G + H + C + A
Postorder Traversal : E F D B G H C A
Algorithm for inorder traversal
Step 1 : Start from the Left Subtree.
Step 3 : B + E + D + F + A + G + C + H
Inorder Traversal : B E D F A G C H
Types of Binary Tree
There are four types of binary tree:
#include <stdio.h>
#include <stdlib.h>
Node* createTree();
void inorder(Node* root);
Binary Tree
int main() {
printf("Create the binary tree:\n");
Node* root = createTree();
printf("Inorder: ");
inorder(root);
printf("\n");
return 0;
}
Binary Tree
Node* createTree() {
int data;
if (data == -1) {
return NULL;
}
return root;
}
Binary Tree
inorder(root->left);
inorder(root->right);
}
Binary Tree Using Array
#include <stdio.h>
#include <stdlib.h>
int i;
int tree[MAX_SIZE];
for ( i = 0; i < MAX_SIZE; i++) {
tree[i] = EMPTY;
}
insert(tree, 1, 0);
insert(tree, 2, 1);
insert(tree, 3, 2);
insert(tree, 4, 3);
insert(tree, 5, 4);
printf("Inorder Traversal: ");
inorder(tree, 0);
printf("\n");
return 0;
}
2. First, compare the element to be searched with the root element of the tree.
-> If root is matched with the target element, then return the node’s location.
-> If it is not matched, then check whether the item is less than the root
element, if it is smaller than the root element, then move to the left subtree.
->If it is larger than the root element, then move to the right subtree.
3.If the element is not found or not present in the tree, then return NULL.
Binary Search Tree
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* leftChild;
struct Node* rightChild;
};
Binary Search Tree
return root;
}
Binary Search Tree
int main() {
struct Node* root = NULL;
int choice, data;
do {
printf("\n1. Insert\n2. Inorder\n3. Postorder\n4. Preorder\n5. Search\n6.
Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter Value: ");
scanf("%d", &data);
root = insert(root, data);
break;
Binary Search Tree
case 2:
inorder(root);
printf("\n");
break;
case 3:
postorder(root);
printf("\n");
break;
case 4:
preorder(root);
printf("\n");
break;
Binary Search Tree
case 5:
printf("Enter Value to Search: ");
scanf("%d", &data);
struct Node* result = search(root, data);
if (result != NULL) {
printf("Element %d found\n", data);
} else {
printf("Element not found\n");
}
break;
}
} while (choice != 6);
return 0;
}
AVL Tree
A node B has been inserted into the right subtree of A the left
subtree of C, because of which C has become an unbalanced node
having balance factor 2. This case is L R rotation where: Inserted
node is in the right subtree of left subtree of C
As LR rotation = RR + LL rotation, hence RR (anticlockwise) on
subtree rooted at A is performed first. By doing RR rotation, node A,
has become the left subtree of B.
A node B has been inserted into the left subtree of C the right
subtree of A, because of which A has become an unbalanced node
having balance factor - 2. This case is RL rotation where: Inserted
node is in the left subtree of right subtree of A
As RL rotation = LL rotation + RR rotation, hence, LL (clockwise)
on subtree rooted at C is performed first. By doing RR rotation,
node C has become the right subtree of B.
int height(Node* N) {
if (N == NULL)
return 0;
return N->height;
}
int max(int a, int b) {
return (a > b) ? a : b;
}
x->right = y;
y->left = T2;
return x;
}
Node* leftRotate(Node* x) {
Node* y = x->right;
Node* T2 = y->left;
y->left = x;
x->right = T2;
return y;
}
int getBalance(Node* N) {
if (N == NULL)
return 0;
return height(N->left) - height(N->right);
}
Node* insert(Node* node, int data) {
if (node == NULL)
return createNode(data);
do {
printf("\n1. Insert\n2. Inorder\n3. Preorder\n4.
Postorder\n5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
printf("Enter value to insert: ");
scanf("%d", &value);
root = insert(root, value);
break;
case 2:
printf("Inorder traversal:\n");
inorder(root);
printf("\n");
break;
case 3:
printf("Preorder traversal:\n");
preorder(root);
printf("\n");
break;
case 4:
printf("Postorder traversal:\n");
postorder(root);
printf("\n");
break;
}
} while (choice != 5);
return 0;
}
Thank You