Trees
Trees
Trees
*Binary Tree
The Binary tree means that the node can have maximum two children. Here, binary
name itself suggests that 'two'; therefore, each node can have either 0, 1 or 2
children.
As we know that,
n = 2h+1 -1
n+1 = 2h+1
log2(n+1) = log2(2h+1)
log2(n+1) = h+1
h = log2(n+1) - 1
As we know that,
n = h+1
h= n-1
In the above tree, we can observe that each node is either containing zero or two
children; therefore, it is a Full Binary tree.
o The number of leaf nodes is equal to the number of internal nodes plus 1. In the
above example, the number of internal nodes is 5; therefore, the number of leaf
nodes is equal to 6.
o The maximum number of nodes is the same as the number of nodes in the binary
tree, i.e., 2h+1 -1.
o The minimum number of nodes in the full binary tree is 2*h-1.
o The minimum height of the full binary tree is log2(n+1) - 1.
o The maximum height of the full binary tree can be computed as:
n= 2*h - 1
n+1 = 2*h
h = n+1/2
Complete Binary Tree
The complete binary tree is a tree in which all the nodes are completely filled except
the last level. In the last level, all the nodes must be as left as possible. In a complete
binary tree, the nodes should be added from the left.
The above tree is a complete binary tree because all the nodes are completely filled,
and all the nodes in the last level are added at the left first.
A tree is a perfect binary tree if all the internal nodes have 2 children, and all the leaf
nodes are at the same level.
Let's look at a simple example of a perfect binary tree.
The below tree is not a perfect binary tree because all the leaf nodes are not at the
same level.
Note: All the perfect binary trees are the complete binary trees as well as the full
binary tree, but vice versa is not true, i.e., all complete binary trees and full binary
trees are the perfect binary trees.
The above tree is a degenerate binary tree because all the nodes have only one child.
It is also known as a right-skewed tree as all the nodes have a right child only.
The above tree is also a degenerate binary tree because all the nodes have only one
child. It is also known as a left-skewed tree as all the nodes have a left child only.
The balanced binary tree is a tree in which both the left and right trees differ by
atmost 1. For example, AVL and Red-Black trees are balanced binary tree.
The above tree is not a balanced binary tree because the difference between the left
subtree and the right subtree is greater than 1.
Binary Tree Implementation
A Binary tree is implemented with the help of pointers. The first node in the tree is
represented by the root pointer. Each node in the tree consists of three parts, i.e.,
data, left pointer and right pointer. To create a binary tree, we first need to create the
node. We will create the node of user-defined as shown below:
1. struct node
2. {
3. int data,
4. struct node *left, *right;
5. }
In the above structure, data is the value, left pointer contains the address of the left
node, and right pointer contains the address of the right node.
1. #include<stdio.h>
2. struct node
3. {
4. int data;
5. struct node *left, *right;
6. }
7. void main()
8. {
9. struct node *root;
10. root = create();
11. }
12. struct node *create()
13. {
14. struct node *temp;
15. int data;
16. temp = (struct node *)malloc(sizeof(struct node));
17. printf("Press 0 to exit");
18. printf("\nPress 1 for new node");
19. printf("Enter your choice : ");
20. scanf("%d", &choice);
21. if(choice==0)
22. {
23. return 0;
24. }
25. else
26. {
27. printf("Enter the data:");
28. scanf("%d", &data);
29. temp->data = data;
30. printf("Enter the left child of %d", data);
31. temp->left = create();
32. printf("Enter the right child of %d", data);
33. temp->right = create();
34. return temp;
35. }
36. }
The above code is calling the create() function recursively and creating new node on
each recursive call. When all the nodes are created, then it forms a binary tree
structure. The process of visiting the nodes is known as tree traversal. There are three
types traversals used to visit a node:
o Inorder traversal
o Preorder traversal
o Postorder traversal
#include <stdio.h>
#include <stdlib.h>
struct node {
};
return temp;
// searching operation
{
if (root == NULL || root -> data == x) //if root->data is x then the element is found
return root;
else if (x > root -> data) // x is greater, so we will search the right subtree
else //x is smaller than the data, so we will search the left subtree
// insertion
if (root == NULL)
return new_node(x);
else if (x > root -> data) // x is greater. Should be inserted to the right
return root;
return NULL;
else if (root -> left_child != NULL) // node with minimum value will have no left child
return root;
// deletion
if (root == NULL)
return NULL;
else {
free(root);
return NULL;
}
//One Child node
else
free(root);
return temp;
//Two Children
else {
return root;
// Inorder Traversal
int main() {
root = new_node(20);
insert(root, 5);
insert(root, 1);
insert(root, 15);
insert(root, 9);
insert(root, 7);
insert(root, 12);
insert(root, 30);
insert(root, 25);
insert(root, 40);
insert(root, 45);
insert(root, 42);
inorder(root);
printf("\n");
inorder(root);
printf("\n");
return 0;