Tree
Tree
Structure?
A binary tree is a tree-type non-linear data structure with a
maximum of two children for each parent. Every node in a binary
tree has a left and right reference along with the data element. 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.
A parent node has two child nodes: the left child and right child.
Hashing, routing data for network traffic, data compression, preparing
binary heaps, and binary search trees are some of the applications
that use a binary tree.
Understanding Properties of Binary Tree Or What Is Binary
Tree?
At every level of it, the maximum number allowed for nodes stands
at 2i.
The height of a binary tree stands defined as the longest path
What Is Binary
emanating from aTree– MoretoThan
root node The Binary
the tree’s Tree Definition
leaf node.
Say a binary tree placed at a height equal to 3. In that case, the
highest number of nodes for this height 3 stands equal to 15, that
is, (1+2+4+8) = 15. In basic terms, the maximum node number
possible for this height h is (20 + 21 + 22+….2h) = 2h+1 -1.
Now, for the minimum node number that is possible at this height
h, it comes as equal to h+1.
If there are a minimum number of nodes, then the height of a
binary tree would stand aa maximum. On the other hand, when
there is a number of a node at its maximum, then the binary
tree m height will be minimum. If there exists around ‘n’
number nodes in a binary tree, here is a calculation to clarify
the binary tree definition.
The tree’s minimum height is computed as:
n = 2h+1 -1
n+1 = 2h+1
Taking log for both sides now,
log2(n+1) = log2(2h+1)
log2(n+1) = h+1
h = log2(n+1) – 1
The highest height will be computed as:
n = h+1
Binary Tree Components
There are three binary tree components. Every binary tree node
has these three components associated with it. It becomes an
essential concept for programmers to understand these three binary
tree components:
1.Data element
2.Pointer to left subtree
3.Pointer to right subtree
Show that the maximum
number of nodes in a binary
tree of height h is 2^(h+1) − 1?
Types of Binary Trees
The following are common types of Binary Trees:
Full/Strict Binary Trees:
A full binary tree is called a binary tree in which each node has exactly
Zero or
Two children
In a full binary tree, number of leaf nodes = No. of internal nodes + 1
Properties of full binary tree:
Maximum number of nodes of full binary tree of height “h” is 2h+1 – 1
Minimum number of nodes of full binary tree of height “h” is 2h+1
Minimum height of the full binary tree of with maximum number of nodes as “n” is ⌈ log(n+1) ⌉ - 1
Maximum height of the binary tree of with minimum number of nodes as “n”
For any binary tree of height “h”, minimum number of nodes = 2h + 1
=>n=2h+1
=>Height of the binary with minimum “n” nodes => h = (n-1)/2
Min Nodes
Max Nodes
Binary Tree 2h+1 – 1 h+1
Full Binary Tree 2h+1 – 1 2h+1
Complete Binary Tree 2h+1 – 1 2h
If maximum and minimum nodes are given, height of the tree is
Max Height
Minimum Height
Binary Tree ⌈ log(n+1) ⌉ - 1 n-1
Full Binary Tree ⌈ log(n+1) ⌉ - 1 (n-1)/2
Complete Binary Tree ⌈ log(n+1) ⌉ - 1 log(n)
The nodes with yellow color are not visited yet. Now, we will traverse the nodes of the above tree using
inorder traversal.
Here, 40 is the root node. We move to the left subtree of 40, that is 30, and it also has subtree 25, so we
again move to the left subtree of 25 that is 15. Here, 15 has no subtree, so print 15 and move towards its
parent node, 25.
Now, print 25 and move to the right subtree of 25
Now, print 28 and move to the root node of 25 that is 30
So, left subtree of 30 is visited. Now, print 30 and move to the right child of 30.
#include <stdio.h>
#include <stdlib.h>
struct node {
int element;
struct node* left;
struct node* right;
};
/*To create a new node*/
struct node* createNode(int val)
{
struct node* Node = (struct node*)malloc(sizeof(struct node));
Node->element = val;
Node->left = NULL;
Node->right = NULL;
return (Node);
}
/*function to traverse the nodes of binary tree in Inorder*/
void traverseInorder(struct node* root)
{
if (root == NULL)
return;
traverseInorder(root->left);
printf(" %d ", root->element);
traverseInorder(root->right);
} int main()
{
struct node* root = createNode(40);
root->left = createNode(30);
root->right = createNode(50);
root->left->left = createNode(25);
root->left->right = createNode(35);
root->left->left->left = createNode(15);
root->left->left->right = createNode(28);
root->right->left = createNode(45);
root->right->right = createNode(60);
root->right->right->left = createNode(55);
root->right->right->right = createNode(70);
printf("\n The Inorder traversal of given binary tree is -\n");
traverseInorder(root);
return 0;
}
Preorder Traversal
In preorder traversal, first, root node is visited, then left sub-tree and after that right sub-
tree is visited. The process of preorder traversal can be represented as -
root → left → right
Root node is always traversed first in preorder traversal, while it is the last item of postorder traversal.
Preorder traversal is used to get the prefix expression of a tree.
The steps to perform the preorder traversal are listed as follows -
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)
Uses of Preorder:
Preorder traversal is used to create a copy of the tree. Preorder traversal
is also used to get prefix expressions on an expression tree.
Example: Preorder traversal for the above-given figure is 1 2 4 5 3.
Postorder Traversal :
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