0% found this document useful (0 votes)
14 views25 pages

3., Binary Tree

Uploaded by

Vijaya Bhavani
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)
14 views25 pages

3., Binary Tree

Uploaded by

Vijaya Bhavani
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/ 25

Binary Tree

■ It is a special type of tree data structure in which no node can

have more than two children


■ It doesn’t have any condition for the values it hold
Binary Tree: Types

There are different types of binary trees like,

■ Full or Strict Binary Tree

■ Perfect Binary Tree


■ Complete Binary Tree

■ Degenerate or Pathological Tree

■ Skewed Binary Tree


1. Full (Or) Strict Binary Tree

■ It is a binary tree where all internal nodes has 0

or 2 children
■ It can be used to represent mathematical

expression
2. Perfect Binary Tree

■ It is a binary tree where all its internal nodes has

exactly 2 children

■ All leaf nodes are on the same level or depth


3. Complete Binary Tree

■ It is a binary tree where all levels are completely filled

except possibly the last level and the last level has all keys
as left as possible
■ All leaf nodes are on the same level or depth
4. Degenerate or Pathological Tree

■ It is a tree where parent node has only one child

either left or right


5. Skewed Binary Tree

■ It is a binary tree which is dominated solely by left

child nodes or right child nodes

Types:
✔ left skewed binary tree

✔ right skewed binary tree


Binary Tree – Representation

There are two ways for representing binary tree, they are

■ Linear Representation

■ Linked Representation
Binary Tree – Linear Representation

The elements are represented using arrays.

It stores elements based on the levels into an array


Binary Tree – Linked Representation
■ The elements are stored using doubly linked list

■ Each node has : left pointer which has left child

address, right pointer which has right child address and


data of its own
Binary Tree – Linked Representation
ADDRESS LEFT DATA RIGHT

1 -1 D -1
2 -1 E -1
3
4 6 A 7
5
6 1 B 2
7 -1 C 9
8
9 -1 F -1
Binary Tree – Implementation

struct tree {
int data;
struct node* left;
struct node* right;
}
Binary Tree – Tree Traversal
✔ Tree traversal is the process of visiting each node in a
tree

4 types:
1. In-order traversal
2. Pre-Order Traversal
3. Post-Order Traversal
4. Level Order Traversal
In-order Traversal

✔ It performs the following steps starting from the root:


Steps:
1. First left node
2. Second root node
3. Third right node

In-order Traversal: D->B->E->A->C


Guess In-order Traversal for the given tree

In-Order Traversal:
5->15->18->20->25->30->35->40-
>45->50->60
In-order Traversal

void inorder(node *t) //address of root node is passed in t


{
if(t!=NULL)
{
inorder(t->left); //inorder traversal on left subtree
printf("\n%d",t->data); //visit the root
inorder(t->right); //inorder traversal on right subtree
}
}
Pre-order Traversal

✔ It performs the following steps starting from the root:


Steps:
1. First root node
2. Second left node
3. Third right node

Pre order Traversal: A->B->D->E->C


Guess Pre order Traversal for the given tree

Pre-Order Traversal:
30->20->15->5->18->25->40->35-
>50->45->60
Pre-order Traversal

void preorder(node *t) //address of root node is


passed in t
{
if(t!=NULL)
{
printf("\n%d",t->data); //visit the root
preorder(t->left); //preorder traversal on left
subtree
preorder(t->right); //preorder traversal on right
subtree

}
}
Post-order Traversal

✔ It performs the following steps starting from the root:


Steps:
1. First left node
2. Second right node
3. Third root node

Post order Traversal: D->E->B->C->A


Guess Post order Traversal for the given tree

Post-Order Traversal:
5->18->15->25->20->35->45->60-
>50->40->30
Post-order Traversal

void postorder(node *t) //address of root node is


passed in t
{
if(t!=NULL)
{
postorder(t->left); //postorder traversal on left
subtree
postorder(t->right); //postorder traversal on right
subtree
printf("\n%d",t->data); //visit the root
}
}
Breadth First Traversal (or)
Level-order 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.

Level order Traversal: A->B->C->D-


>CE
Find In-order, Pre order & Post order
traversal for the given tree

In-order: 4->10->12->15->18-
>22->24->25->31->35->44->50-
>66->70->90
Pre order: 25->15->10->4->12-
>22->18->24->50->35->31->44-
>70->66->90
Post order: 4->12->10->18->24-
>22->15->31->44->35->66->90-
>70->50->25
Thank you

You might also like