0% found this document useful (0 votes)
36 views

Binary Tree Properties

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

Binary Tree Properties

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 17

Binary Tree Properties-

Important properties of binary trees are-

Property-01:

Minimum number of nodes in a binary tree of height H


=H+1

Example-

To construct a binary tree of height = 4, we need at least 4 + 1 = 5 nodes.

Property-02:

Maximum number of nodes in a binary tree of height H


= 2H+1 – 1
Example-

Maximum number of nodes in a binary tree of height 3


= 23+1 – 1
= 16 – 1
= 15 nodes
Thus, in a binary tree of height = 3, maximum number of nodes that can be inserted =
15.

We can not insert more number of nodes in this binary tree.

Property-03:

Total Number of leaf nodes in a Binary Tree


= Total Number of nodes with 2 children + 1

Example-

Consider the following binary tree-


Here,
 Number of leaf nodes = 3
 Number of nodes with 2 children = 2

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:

Maximum number of nodes at any level ‘L’ in a binary tree


= 2L

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.

Binary Tree Representations

A binary tree data structure is represented using two methods. Those methods are as follows...

1. Array Representation

2. Linked List Representation

Consider the following binary tree...


1. Array Representation of Binary Tree

In array representation of a binary tree, we use one-dimensional array (1-D Array) to represent a

binary tree.

Consider the above example of a binary tree and it is represented as follows...

To represent a binary tree of depth 'n' using array representation, we need one dimensional

array with a maximum size of 2n + 1.

2. Linked List Representation of Binary Tree

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

storing right child address.

In this linked list representation, a node has the following structure...


The above example of the binary tree represented using Linked list representation is shown as

follows...

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.

Binary Tree program in C

struct node {
int data;
struct node* left;
struct node* right;
};

struct node* newNode(int data)


{
struct node* node
= (struct node*)malloc(sizeof(struct node));
node->data = data;
node->left = NULL;
node->right = NULL;

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;

// first recur on left subtree


printPostorder(node->left);

// then recur on right subtree


printPostorder(node->right);

// now deal with the node


printf("%d ", node->data);
}

/* Given a binary tree, print its nodes in inorder*/


void printInorder(struct node* node)
{
if (node == NULL)
return;

/* first recur on left child */


printInorder(node->left);

/* then print the data of node */


printf("%d ", node->data);

/* now recur on right child */


printInorder(node->right);
}

/* Given a binary tree, print its nodes in preorder*/


void printPreorder(struct node* node)
{
if (node == NULL)
return;

/* first print data of node */


printf("%d ", node->data);

/* then recur on left subtree */


printPreorder(node->left);

/* now recur on right subtree */


printPreorder(node->right);
}
Binary Tree-

Before you go through this article, make sure that you gone through the previous article
on Binary Trees.

We have discussed-

 Binary tree is a special tree data structure.

 In a binary tree, each node can have at most 2 children.

In this article, we will discuss about Binary Tree Traversal.

Tree Traversal-

Tree Traversal refers to the process of visiting each node in a tree data structure exactly once.

Various tree traversal techniques are-


Inorder-> L R Rt a+b

Depth First Traversal-

Following three traversal techniques fall under Depth First Traversal-

1. Preorder Traversal

2. Inorder Traversal

3. Postorder Traversal

1. Preorder Traversal-

Algorithm-

1. Visit the root

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)

Root → Left → Right

Example-
Consider the following example-

Preorder Traversal Shortcut

Traverse the entire tree starting from the root node keeping yourself to the left.
Applications-

 Preorder traversal is used to get prefix expression of an expression tree.

 Preorder traversal is used to create a copy of the tree.

2. Inorder Traversal-

Algorithm-

1. Traverse the left sub tree i.e. call Inorder (left sub tree)

2. Visit the root

3. Traverse the right sub tree i.e. call Inorder (right sub tree)

Left → Root → Right

Example-

Consider the following example-


Inorder Traversal Shortcut

Keep a plane mirror horizontally at the bottom of the tree and take the projection of all the nodes.
Application-

 Inorder traversal is used to get infix expression of an expression tree.

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)

3. Visit the root

Left → Right → Root

Example-

Consider the following example-


Postorder Traversal Shortcut

Pluck all the leftmost leaf nodes one by one.

Applications-
 Postorder traversal is used to get postfix expression of an expression tree.

 Postorder traversal is used to delete the tree.

 This is because it deletes the children first and then it deletes the parent.

Breadth First Traversal-

 Breadth First Traversal of a tree prints all the nodes of a tree level by level.

 Breadth First Traversal is also called as Level Order Traversal.

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.

You might also like