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

Tree

Uploaded by

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

Tree

Uploaded by

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

What is Binary Tree Data

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

Complete Binary Tree


A binary tree is called a complete binary tree if all levels except possibly the last is completely filled and
all the nodes in the last level are as left as possible.
Examples of complete binary trees:
Properties of complete binary tree:
Maximum number of nodes of complete binary tree of height “h” is 2h+1 – 1
Minimum number of nodes of complete binary tree of height “h” - 2h
Minimum height of complete binary tree of with maximum number of nodes as
“n” ⌈ log(n+1) ⌉ - 1
Maximum height of complete binary tree of with minimum number of nodes as “n”
For any binary tree of height “h”, minimum number of nodes = 2 h
=>log(n) = log2h
=> log(n) = (h)log2
=>h = log(n)
=>Height of the binary with minimum “n” nodes => h = log(n)

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)

Perfect Binary Tree


A binary tree is called perfect if all the internal nodes have two children (exactly two
children) and all the leaf nodes are at same level. A perfect binary tree can also be a full
binary tree or a complete binary tree but not vice versa.
Degenerate Binary Tree
All the internal nodes have only one child except leaf nodes.
If every node in the tree has only one left child, it is left skewed
If every node in the tree has only one right child, it is right skewed
Balanced Binary Tree
A balanced binary tree is a binary tree structure in which the left and right sub-trees of
every node differ in height by no more than 1. Balanced Binary Search trees like AVL Trees,
Red-Black Trees are some of the examples
Tree Traversals (Inorder, Preorder and
Postorder)
Inorder Traversal
In this article, we will discuss the inorder traversal in data structure.
If we want to traverse the nodes in ascending order, then we use the inorder
traversal. Following are the steps required for the inorder traversal:
Visit all the nodes in the left subtree
Visit the root node
Visit all the nodes in the right subtree
Linear data structures such as stack, array, queue, etc., only have one way to
traverse the data. But in hierarchical data structures such as tree, there are
multiple ways to traverse the data. Here we will discuss another way to traverse the
tree data structure, i.e., inorder traversal.
There are two approaches used for the inorder traversal:
Inorder traversal using Recursion
Inorder traversal using an Iterative method
An inorder traversal technique follows the Left Root Right policy. Here, Left Root Right means
that the left subtree of the root node is traversed first, then the root node, and then the right
subtree of the root node is traversed. Here, inorder name itself suggests that the root node
comes in between the left and the right subtrees.

Inorder traversal using recursion


Step 1: Recursively traverse the left subtree
Step 2: Now, visit the root
Step 3: Traverse the right subtree recursively
Example of inorder traversal
Now, let's see an example of inorder traversal. It will be easier to understand the
procedure of inorder traversal using an example.

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.

Now, print 35 and move to the root node of 30.


Now, print root node 40 and move to its right subtree.
Now recursively traverse the right subtree of 40 that is 50.
50 have subtree so first traverse the left subtree of 50 that is 45. 45 has no children, so print 45 and move to
its root node.
Now print 50 and move to the right subtree of 50 that is 60.
Now recursively traverse the right subtree of 50 that is 60. 60 have subtree so first traverse the left subtree of 60 that is
55. 55 has no children, so print 55 and move to its root node.
Now print 60 and move to the right subtree of 60 that is 70.
Now print 70.
After the completion of inorder traversal, the final output is -
{15, 25, 28, 30, 35, 40, 45, 50, 55, 60, 70}

Complexity of Inorder traversal


The time complexity of Inorder traversal is O(n), where 'n' is the size of binary tree.
Whereas, the space complexity of inorder traversal is O(1), if we do not consider the stack size for function calls.
Otherwise, the space complexity of inorder traversal is O(h), where 'h' is the height of tree.
Implementation of Inorder traversal
Program: Write a program to implement inorder traversal in C language.

#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 -

First, visit the root node.


Then, visit the left subtree.
At last, visit the right subtree.
Algorithm
Now, let's see the algorithm of preorder traversal.
Step 1: Repeat Steps 2 to 4 while TREE != NULL
Step 2: Write TREE -> DATA
Step 3: PREORDER(TREE -> LEFT)
Step 4: PREORDER(TREE -> RIGHT)
[END OF LOOP]
Step 5: END

Example of preorder traversal


The nodes with yellow color are not visited yet. Now, we will traverse the nodes of the above tree using preorder
traversal.
Start with the root node 40. First, print 40 and then recursively traverse the left subtree.
Now, move to the left subtree. For left subtree, the root node is 30. Print 30, and move towards the left subtree of 30.
In left subtree of 30, there is an element 25, so print 25, and traverse the left subtree of 25.
In left subtree of 25, there is an element 15, and 15 has no subtree. So, print 15, and move to the right
subtree of 25.
In right subtree of 25, there is 28, and 28 has no subtree. So, print 28, and move to the right subtree of 30.
In right subtree of 30, there is 35 that has no subtree. So print 35, and traverse the right subtree of 40.
In the right subtree of 40, there is 50. Print 50, and traverse the left subtree of 50.
In the left subtree of 50, there is 45 that do not have any child. So, print 45, and traverse the right subtree
of 50.
In right subtree of 50, there is 60. Print 60 and traverse the left subtree of 60.
In the left subtree of 60, there is 55 that does not have any child. So, print 55 and move to the right subtree of 60.
In the right subtree of 60, there is 70 that do not have any child. So, print 70 and stop the process.

After the completion of preorder traversal, the final output is -


40, 30, 25, 15, 28, 35, 50, 45, 60, 55, 70
Complexity of Preorder traversal
The time complexity of preorder traversal is O(n), where 'n' is the size of binary tree.
Whereas, the space complexity of preorder traversal is O(1), if we do not consider
the stack size for function calls. Otherwise, the space complexity of preorder
traversal is O(h), where 'h' is the height of the tree.
Preorder Traversal

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

You might also like