Dsa Assignment 2
Dsa Assignment 2
Name: Devendhiran.D
Reg no: 21MID0218
Assignment-2
1) Write a program in C to implement a binary tree
#include<stdio.h>
#include<stdlib.h>
struct node
{
int value;
struct node *left_child, *right_child;
};
{
struct node *tmp = (struct node *)malloc(sizeof(struct node));
tmp->value = value;
tmp->left_child = tmp->right_child = NULL;
return tmp;
}
int main()
{
printf("Implementation of a Binary Tree in C is:\n\n");
return 0;
}
Code Screen Shot:
Output:
2)Write a program in C to perform tree traversals (in order, pre order and post order) on a
binary tree
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node* left;
struct node* right;
};
return (node);
}
printPostorder(node->left);
printPostorder(node->right);
printf("%d ", node->data);
}
printInorder(node->left);
printInorder(node->right);
}
printPreorder(node->left);
printPreorder(node->right);
}
int main()
{
struct node* root = newNode(2);
root->left = newNode(4);
root->right = newNode(6);
root->left->left = newNode(8);
root->left->right = newNode(10);
getchar();
return 0;
}
#include<stdio.h>
#include<stdlib.h>
struct node
{
struct node *lchild;
int info;
struct node *rchild;
};
int main( )
{
struct node *root=NULL,*ptr;
int choice,k;
while(1)
{
printf("\n");
printf("1.Insert a element\n");
printf("2.Display\n");
printf("3.Find minimum and maximum\n");
printf("4.Quit\n");
printf("\nEnter a your choice : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\nEnter the key to be inserted : ");
scanf("%d",&k);
root = insert(root, k);
break;
case 2:
printf("\n");
display(root,0);
printf("\n");
break;
case 3:
ptr = Min(root);
if(ptr!=NULL)
printf("\nMinimum key is %d\n", ptr->info );
ptr = Max(root);
if(ptr!=NULL)
printf("\nMaximum key is %d\n", ptr->info );
break;
case 4:
exit(1);
default:
printf("\nWrong choice\n");
}
}
return 0;
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
struct node{
int data;
struct node *left;
struct node *right;
};
newNode->data= data;
newNode->left = NULL;
newNode->right = NULL;
return newNode;
}
while(true) {
parent = current;
else {
current = current->right;
if(current == NULL) {
parent->right = newNode;
return;
}
}
}
}
}
struct node* minNode(struct node *root) {
if (root->left != NULL)
return minNode(root->left);
else
return root;
}
else {
if(node->left == NULL && node->right == NULL)
node = NULL;
if(root == NULL){
printf("Tree is empty\n");
return;
}
else {
if(node->left!= NULL)
inorderTraversal(node->left);
printf("%d ", node->data);
if(node->right!= NULL)
inorderTraversal(node->right);
}
}
int main()
{
insert(50);
insert(30);
insert(70);
insert(60);
insert(10);
insert(90);
return 0;
}
Code screenshots:
Output: