Binary Tree Properties
Binary Tree Properties
Property-01:
Example-
Property-02:
Property-03:
Example-
Clearly, number of leaf nodes is one greater than number of nodes with 2 children.
This verifies the above relation.
NOTE
It is interesting to note that-
Number of leaf nodes in any binary tree depends only on the number of nodes with 2 children.
Property-04:
Example-
Maximum number of nodes at level-2 in a binary tree
= 22
=4
Thus, in a binary tree, maximum number of nodes that can be present at level-2 = 4.
A binary tree data structure is represented using two methods. Those methods are as follows...
1. Array Representation
In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a
binary tree.
To represent a binary tree of depth 'n' using array representation, we need one dimensional
We use a double linked list to represent a binary tree. In a double linked list, every node consists
of three fields. First field for storing left child address, second for storing actual data and third for
follows...
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.
struct node {
int data;
struct node* left;
struct node* right;
};
return (node);
}
int main()
{
struct node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
}
void printPostorder(struct node* node)
{
if (node == NULL)
return;
Before you go through this article, make sure that you gone through the previous article
on Binary Trees.
We have discussed-
Tree Traversal-
Tree Traversal refers to the process of visiting each node in a tree data structure exactly once.
1. Preorder Traversal
2. Inorder Traversal
3. Postorder Traversal
1. Preorder Traversal-
Algorithm-
2. Traverse the left sub tree i.e. call Preorder (left sub tree)
3. Traverse the right sub tree i.e. call Preorder (right sub tree)
Example-
Consider the following example-
Traverse the entire tree starting from the root node keeping yourself to the left.
Applications-
2. Inorder Traversal-
Algorithm-
1. Traverse the left sub tree i.e. call Inorder (left sub tree)
3. Traverse the right sub tree i.e. call Inorder (right sub tree)
Example-
Keep a plane mirror horizontally at the bottom of the tree and take the projection of all the nodes.
Application-
3. Postorder Traversal-
Algorithm-
1. Traverse the left sub tree i.e. call Postorder (left sub tree)
2. Traverse the right sub tree i.e. call Postorder (right sub tree)
Example-
Applications-
Postorder traversal is used to get postfix expression of an expression tree.
This is because it deletes the children first and then it deletes the parent.
Breadth First Traversal of a tree prints all the nodes of a tree level by level.
Example-
Application-
Level order traversal is used to print the data in the same order as stored in the array
representation of a complete binary tree.