0% found this document useful (0 votes)
53 views9 pages

Fahima Haque 04210101571 CSE Lab Report:: Implement A Binary Tree and Describe All The Operations

The document describes operations on a binary tree data structure including: - Creating a binary tree by getting nodes and adding left and right children recursively - Traversing the tree using in-order, pre-order and post-order traversal algorithms - Printing the leaf nodes, height of the tree, and performing a level order traversal - Deleting nodes by finding and removing the target node from the tree

Uploaded by

Fahima Haque
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)
53 views9 pages

Fahima Haque 04210101571 CSE Lab Report:: Implement A Binary Tree and Describe All The Operations

The document describes operations on a binary tree data structure including: - Creating a binary tree by getting nodes and adding left and right children recursively - Traversing the tree using in-order, pre-order and post-order traversal algorithms - Printing the leaf nodes, height of the tree, and performing a level order traversal - Deleting nodes by finding and removing the target node from the tree

Uploaded by

Fahima Haque
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/ 9

Fahima Haque

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);
}

• Complete Binary Tree: A Binary Tree is complete Binary Tree if all


levels are completely filled except possibly the last level and the last
level has all keys as left as possible 18
/ \
15 30
/ \ / \
40 50 100 40
/\ /
8 7 9
• In-order traversal first goes over the left subtree of the root node,
then accesses the current node, followed by the right subtree of the
current node.
void in-order (struct node* root) {

// Base case

if (root == null) {

return;

// Travel the left sub-tree first.

In-order(root.left);

// Print the current node value

printf("%d ", root.data);

// Travel the right sub-tree next.

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;

} // Print the current node value

printf("%d ", root.data);

// Travel the left sub-tree first.

Pre-order(root.left);

// Travel the right sub-tree next.

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.

void post-order(struct node* root) {


if (root == null) {
return;
}
// Travel the left sub-tree first.
Post-order(root.left);
// Travel the right sub-tree next.
Post-order(root.right);
// Print the current node value
printf("%d ", root.data);
}

• Level order traversal of a tree is breadth 1 st traversal for the tree


1
/ \
2 3
/ \
4 5

• 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.

void deleteNode(struct node* root, int data){

if (root == NULL) root=tempnode;

if (data < root->key)

root->left = deleteNode(root->left, key);


else if (key > root->key)

root->right = deleteNode(root->right, key);

else

if (root->left == NULL)

struct node *temp = root->right;

free(root);

return temp;

else if (root->right == NULL)

struct node *temp = root->left;

free(root);

return temp;

struct node* temp = minValueNode(root->right);

root->key = temp->key;

root->right = deleteNode(root->right, temp->key);

return root;

OUTPUT:

You might also like