BINARY TREE

If you have any queries please leave a message here
Your Message
×


In computer science, a binary tree is a tree data structure in which each node has at most two children, which are referred to as the left child and the right child.


Binary Tree Construction Process

Constract a Binary Tree for the following numbers - 60 , 28 , 82 , 46 , 58 , 75 , 12 , 15 , 95 , 87 , 99 , 8


Figure 1 : First, we create the tree having the root as the only node with node-value 60


Figure 2 : Since 28 < 60, therefore 28 is attached as left child of the root.


Figure 3 : Next value 82 is greater than 60, so 82 is attached as right child of the root.


Figure 4 : Next value 46 is less than 60 but greater than 28, therefore 46 is attached as right child of 28.


Figure 5 : Next value 58 is less than 60 but greater than from both 28 and 46, therefore 58 is attached as right child of 46.


Figure 6 : Next value 75 is greater than 60 but less than 82, therefore 75 is attached as left child of 82.


Figure 7 : Next value 12 is less than 60 but greater than 28, therefore 12 is attached as left child of 28.


Figure 8 : Next value 15 is less than from both 60 and 28 but greater than 12, therefore 15 is attached as right child of 12.


Figure 9 : Next value 95 is greater than from both 60 and 82, therefore 95 is attached as right child of 82.


Figure 10 : Next value 87 is greater than from both 60 and 82 but less than 95, therefore 87 is attached as left child of 95.


Figure 11 : Next value 99 is greater than from 60, 82 and 95, therefore 99 is attached as right child of 95.


Figure 12 : Next value 8 is less than from 60, 28 and 12, therefore 8 is attached as left child of 12.




Tree Programming in C
//Header-File declaration
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>

//Structure declaration
struct data
{
int number;
struct data *leftchild;
struct data *rightchild;
};

//Global variable declaration
struct data *root, *node, *child;
//Global function declaration
void insertNode(int n);
void preorder();
void inorder();
void postorder();

void main()
{
int choice, num;
root = NULL;
//Infinite loop
while(1)
{
clrscr();
printf("\n Press 1 for insert operation");
printf("\n Press 2 for preorder Presentation");
printf("\n Press 3 for inorder Presentation");
printf("\n Press 4 for postorder Presentation");
printf("\n Press 5 for exit window");
printf("\n Enter your choice...");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n Enter the Number...");
scanf("%d",&num);
insertNode(num);
break;
case 2:
preorder();
getch();
break;
case 3:
inorder();
getch();
break;
case 4:
postorder();
getch();
break;
case 5:
exit(0); //Exit from infinite loop
default:
printf("\n\n\n WRONG INPUT");
}
}
}

//Insert operation
void insertNode(int n)
{
if(root == NULL)
{
root = (struct data *) malloc(sizeof(struct data));
root->number = n;
root->leftchild = NULL;
root->rightchild = NULL;
}
else
{
child = (struct data *) malloc(sizeof(struct data));
child->number = n;
child->leftchild = NULL;
child->rightchild = NULL;
node = root;
while(node != NULL)
{
//If number <= root value then goto the left side
if(n <= node->number)
{
if(node->leftchild == NULL)
{
node->leftchild = child;
break;
}
else
node = node->leftchild;
}
//If number > root value then goto the right side
if(n > node->number)
{
if(node->rightchild == NULL)
{
node->rightchild = child;
break;
}
else
node = node->rightchild;
}
}
}
}

//Preorder traversal using recursion
void preorder()
{
node = root;
if(node != NULL)
{
printf("\t%d",node->number);
preorder(node->leftchild);
preorder(node->rightchild);
}
}

//Inorder traversal using recursion
void inorder()
{
node = root;
if(node != NULL)
{
inorder(node->leftchild);
printf("\t%d",node->number);
inorder(node->rightchild);
}
}

//Postorder traversal using recursion
void postorder()
{
node = root;
if(node != NULL)
{
postorder(node->leftchild);
postorder(node->rightchild);
printf("\t%d",node->number);
}
}


ABOUT US

QuestionSolves.com is an educational website that helps worldwide students in solving computer education related queries.

Also, different software like Visual Studio, SQL Server, Oracle etc. are available to download in different versions.

Moreover, QuestionSolves.com provides solutions to your questions and assignments also.


MORE TOPIC


Windows Command

UNIX Command

IGNOU Assignment Solution

IGNOU Question Paper Solution

Solutions of Different Questions


WHAT WE DO


Website Devlopment

Training

Home Learning

Provide BCA, MCA Projects

Provide Assignment & Question Paper Solution


CONTACT US


Follow Us