Fahima Haque 04210101571 CSE Lab Report:: Implement A Binary Tree and Describe All The Operations
Fahima Haque 04210101571 CSE Lab Report:: Implement A Binary Tree and Describe All The Operations
04210101571
CSE
Lab Report: IMPLEMENT A BINARY TREE AND DESCRIBE ALL THE OPERATIONS
# include <stdio.h>
# include <stdlib.h>
# include <string.h>
#include<conio.h>
struct tree
{
struct tree* lchild;
char data[10];
struct tree* rchild;
};
typedef struct tree node;
node *Q[50];
int node_ctr;
node* getnode()
{
node *temp ;
temp = (node*) malloc(sizeof(node));
printf("\n Enter Data: ");
fflush(stdin);
scanf("%s",temp->data);
temp->lchild = NULL;
temp->rchild = NULL;
return temp;
}
void create_binarytree(node *root)
{
char option;
node_ctr = 1;
if( root != NULL )
{
printf("\n Node %s has Left SubTree(Y/N)",root->data);
fflush(stdin);
scanf("%c",&option);
if( option=='Y' || option == 'y')
{
root->lchild = getnode();
node_ctr++;
create_binarytree(root->lchild);
}
else
{
root->lchild = NULL;
create_binarytree(root->lchild);
}
printf("\n Node %s has Right SubTree(Y/N) ",root->data);
fflush(stdin);
scanf("%c",&option);
if( option=='Y' || option == 'y')
{
root->rchild = getnode();
node_ctr++;
create_binarytree(root->rchild);
}
else
{
root->rchild = NULL;
create_binarytree(root->rchild);
}
}
}
void make_Queue(node *root,int parent)
{
if(root != NULL)
{
node_ctr++;
Q[parent] = root;
make_Queue(root->lchild,parent*2+1);
make_Queue(root->rchild,parent*2+2);
}
}
void delete_node(node *root, int parent)
{
int index = 0;
if(root == NULL)
printf("\n Empty TREE ");
else
{
node_ctr = 0;
make_Queue(root,0);
index = node_ctr-1;
Q[index] = NULL;
parent = (index-1) /2;
if( 2* parent + 1 == index )
Q[parent]->lchild = NULL;
else
Q[parent]->rchild = NULL;
}
printf("\n Node Deleted ..");
}
void inorder(node *root)
{
if(root != NULL)
{
inorder(root->lchild);
printf("%3s",root->data);
inorder(root->rchild);
}
}
void preorder(node *root)
{
if( root != NULL )
{
printf("%3s",root->data);
preorder(root->lchild);
preorder(root->rchild);
}
}
void postorder(node *root)
{
if( root != NULL )
{
postorder(root->lchild);
postorder(root->rchild);
printf("%3s", root->data);
}
}
void print_leaf(node *root)
{
if(root != NULL)
{
if(root->lchild == NULL && root->rchild == NULL)
printf("%3s ",root->data);
print_leaf(root->lchild);
print_leaf(root->rchild);
}
}
int height(node *root)
{
if(root == NULL)
return -1;
else
return (1 + height(root->lchild),height(root->rchild));
}
void print_tree(node *root, int line)
{
int i;
if(root != NULL)
{
print_tree(root->rchild,line+1);
printf("\n");
for(i=0;i<line;i++)
printf(" ");
printf("%s", root->data);
print_tree(root->lchild,line+1);
}
}
void level_order(node *Q[],int ctr)
{
int i;
for( i = 0; i < ctr ; i++)
{
if( Q[i] != NULL )
printf("%5s",Q[i]->data);
}
}
int menu()
{
int ch;
printf("\n 1. Create Binary Tree ");
printf("\n 2. Inorder Traversal ");
printf("\n 3. Preorder Traversal ");
printf("\n 4. Postorder Traversal ");
printf("\n 5. Level Order Traversal");
printf("\n 6. Leaf Node ");
printf("\n 7. Print Height of Tree ");
printf("\n 8. Print Binary Tree ");
printf("\n 9. Delete a node ");
printf("\n 10. Quit ");
printf("\n Enter Your choice: ");
scanf("%d", &ch);
return ch;
}
int main()
{
int i,ch;
node *root = NULL;
do
{
ch = menu();
switch( ch)
{
Case 1:
if( root == NULL )
{
root = getnode();
create_binarytree(root);
}
else
{
printf("\n Tree is already Created ..");
}
break;
case 2 :
printf("\n Inorder Traversal: ");
inorder(root);
break;
case 3 :
printf("\n Preorder Traversal: ");
preorder(root);
break;
case 4 :
printf("\n Postorder Traversal: ");
postorder(root);
break;
case 5:
printf("\n Level Order Traversal ..");
make_Queue(root,0);
level_order(Q,node_ctr);
break;
case 6 :
printf("\n Leaf Nodes: ");
print_leaf(root);
break;
case 7 :
printf("\n Height of Tree: %d ", height(root));
break;
case 8 :
printf("\n Print Tree \n");
print_tree(root, 0);
break;
case 9 :
delete_node(root,0);
break;
case 10 :
exit(0);
}
getch();
}while(1);
}
// Base case
if (root == null) {
return;
In-order(root.left);
In-order(root.right);
• Pre- order traversal first accesses the current node value, then
traverses the left and right sub-trees respectively.
void pre-oder(struct node* root) {
if (root == null) {
return;
Pre-order(root.left);
Pre-order(root.right);
• Post- order traversal puts the root value at last, and goes over the
left and right sub-trees first. The relative order of the left and right
sub-trees remain the same.
• Prints leaf nodes a Binary tree, print all the leaf nodes of a Binary tree
at a given level L
1
/ \
2 3
/ / \
4 5 8
/ \ / \
6 7 9 10
• Deleting the last node checking the head for empty. If it is not empty,
then check the head next for empty. If the head next is empty, then
release the head, else traverse to the second last node of the list.
Then, link the next of second last node to NULL and delete the last
node.
else
if (root->left == NULL)
free(root);
return temp;
free(root);
return temp;
root->key = temp->key;
return root;
OUTPUT: