Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 7

-------1st

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tnode {
int data;
struct tnode * leftc;
struct tnode * rightc;
};

void insert(struct tnode **, int num);


void inorder(struct tnode *);
void preorder(struct tnode *);
void postorder(struct tnode *);

int main() {
struct tnode * root=NULL;
char ch[5];
int num;
do {
printf("Enter the element to be inserted in the tree\n");
scanf("%d",&num);
insert(&root, num);
printf("Do you want to insert another element?\n");
scanf("%s",ch);
}while(strcmp(ch,"yes")==0);
printf("Inorder Traversal : The elements in the tree are");
inorder(root);
printf("\n");
printf("Preorder Traversal : The elements in the tree are");
preorder(root);
printf("\n");
printf("Postorder Traversal : The elements in the tree are");
postorder(root);
printf("\n");
return 0;
}

void insert(struct tnode ** s, int num) {


/*--------Fill your code here--------*/
if((*s) ==NULL) {
(*s)=(struct tnode *)malloc(sizeof(struct tnode));
(*s)->data=num;
(*s)->leftc=NULL;
(*s)->rightc=NULL;

}
else {
if(num <(*s)->data)
insert(&((*s)->leftc),num);
else
insert(&((*s)->rightc),num);

void inorder(struct tnode * s) {


/*--------Fill your code here--------*/
if(s->leftc!=NULL)
inorder(s->leftc);
printf("%d ",s->data);
if(s->rightc!=NULL)
inorder(s->rightc);

void preorder(struct tnode * s) {


/*--------Fill your code here--------*/
printf("%d ",s->data);
if(s->leftc!=NULL)
preorder(s->leftc);
if(s->rightc!=NULL)
preorder(s->rightc);
}

void postorder(struct tnode * s) {


/*--------Fill your code here--------*/
if(s->leftc!=NULL)
postorder(s->leftc);
if(s->rightc!=NULL)
postorder(s->rightc);
printf("%d ",s->data);

}
-------------------- 2nd
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tnode
{
int data;
struct tnode * leftc;
struct tnode * rightc;
};

void insert(struct tnode **, int num);


void inorder(struct tnode *);
int search(struct tnode * s, int ele);

int main() {
struct tnode * root=NULL;
char ch[5];
int num,ele;
do {
printf("Enter the element to be inserted in the tree\n");
scanf("%d",&num);
insert(&root, num);
printf("Do you want to insert another element?\n");
scanf("%s",ch);
}while(strcmp(ch,"yes")==0);
printf("Inorder Traversal : The elements in the tree are");
inorder(root);
printf("\n");
printf("Enter the element to be searched\n");
scanf("%d",&ele);
if(search(root,ele))
printf("%d found\n",ele);
else
printf("%d not found\n",ele);
return 0;
}

void insert(struct tnode ** s, int num) {


if((*s) == NULL) {
(*s) = (struct tnode *) malloc( sizeof (struct tnode));
(*s)->data = num;
(*s)->leftc = NULL;
(*s)->rightc = NULL;
}
else {
if(num < (*s)->data)
insert(&( (*s)->leftc ), num);
else
insert(&( (*s)->rightc ), num);
}
}

int search(struct tnode * s, int ele) {


/*----------Fill your code here-----------*/
// struct tnode *root;
int flag=0,f;
//s=root;
while(flag==0 && s!=NULL)
{
if(s->data==ele)
{
flag=1;
f=s->data;
break;
}
else if(ele>s->data)
s=s->rightc;
else s=s->leftc;
}
if(flag==1)

return f;

else
return 0;
}

void inorder(struct tnode * s) {


/*----------Fill your code here-----------*/
if(s->leftc!=NULL)
inorder(s->leftc);
printf("%d ",s->data);
if(s->rightc!=NULL)
inorder(s->rightc);

}
---------4
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct tnode
{
int data;
struct tnode * leftc;
struct tnode * rightc;
};

void insert(struct tnode **, int num);


void inorder(struct tnode *);
int findSize(struct tnode *);

int main() {
struct tnode * root=NULL;
char ch[5];
int num;
do {
printf("Enter the element to be inserted in the tree\n");
scanf("%d",&num);
insert(&root, num);
printf("Do you want to insert another element?\n");
scanf("%s",ch);
}while(strcmp(ch,"yes")==0);

printf("The elements in the tree are");


inorder(root);
printf("\n");
printf("The size of the tree is %d\n",findSize(root));
return 0;
}

void insert(struct tnode ** s, int num) {


if((*s) == NULL) {
(*s) = (struct tnode *) malloc( sizeof (struct tnode));
(*s)->data = num;
(*s)->leftc = NULL;
(*s)->rightc = NULL;
}
else {
if(num < (*s)->data)
insert(&( (*s)->leftc ), num);
else
insert(&( (*s)->rightc ), num);
}
}

void inorder(struct tnode * s) {


/*----------Fill your code here-----------*/
if(s->leftc!=NULL)
inorder(s->leftc);
printf("%d ",s->data);
if(s->rightc!=NULL)
inorder(s->rightc);
}

int findSize(struct tnode * s)


{
/*----------Fill your code here-----------*/
if(s==NULL)
return 0;
else
return(findSize(s->leftc)+1+findSize(s->rightc));
}
-----------------5#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tnode {
int data;
struct tnode * leftc;
struct tnode * rightc;
};

void insert(struct tnode **, int num);


void inorder(struct tnode *);
int getLeafCount(struct tnode* node);

int main() {
struct tnode * root=NULL;
char ch[5];
int num;
do {
printf("Enter the element to be inserted in the tree\n");
scanf("%d ",&num);
insert(&root, num);
printf("Do you want to insert another element?\n");
scanf("%s",ch);
}while(strcmp(ch,"yes")==0);

printf("The elements in the tree are");


inorder(root);
printf("\n");
printf("The number of leaf nodes in the tree is %d\n",getLeafCount(root));
return 0;
}

void insert(struct tnode ** s, int num) {


if((*s) == NULL) {
(*s) = (struct tnode *) malloc( sizeof (struct tnode));
(*s)->data = num;
(*s)->leftc = NULL;
(*s)->rightc = NULL;
}
else {
if(num < (*s)->data)
insert(&( (*s)->leftc ), num);
else
insert(&( (*s)->rightc ), num);
}
}

void inorder(struct tnode * s) {


/*----------Fill your code here-----------*/
if(s->leftc!=NULL)
inorder(s->leftc);
printf("%d",s->data);
if(s->rightc!=NULL)
inorder(s->rightc);
}
int getLeafCount(struct tnode* node) {
/*----------Fill your code here-----------*/
// int f;
//f=getLeafCount(node)
if(node==NULL)
return 0;
if(node->leftc==NULL && node->rightc==NULL)
return 1;
else
return getLeafCount(node->leftc)+getLeafCount(node->rightc);
}
------------6
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct tnode {
int data;
struct tnode * leftc;
struct tnode * rightc;
};

void insert(struct tnode **, int num);


void inorder(struct tnode *);
int findMaxDepth(struct tnode * s);

int main() {
struct tnode * root=NULL;
char ch[5];
int num;
do {
printf("Enter the element to be inserted in the tree\n");
scanf("%d",&num);
insert(&root, num);
printf("Do you want to insert another element?\n");
scanf("%s",ch);
} while(strcmp(ch,"yes")==0);

printf("The elements in the tree are");


inorder(root);
printf("\n");
printf("The maximum depth of the tree is %d\n",findMaxDepth(root));
return 0;
}

void insert(struct tnode ** s, int num) {


if((*s) == NULL) {
(*s) = (struct tnode *) malloc( sizeof (struct tnode));
(*s)->data = num;
(*s)->leftc = NULL;
(*s)->rightc = NULL;
}
else {
if(num < (*s)->data)
insert(&( (*s)->leftc ), num);
else
insert(&( (*s)->rightc ), num);
}
}
void inorder(struct tnode * s) {
/*----------Fill your code here-----------*/
if(s->leftc!=NULL)
inorder(s->leftc);
printf(" %d",s->data);
if(s->rightc!=NULL)
inorder(s->rightc);
}

int findMaxDepth(struct tnode * s) {


/*----------Fill your code here-----------*/
int leftcDepth;
int rightcDepth;
if(s==NULL)
return 0;
else
{
leftcDepth=findMaxDepth(s->leftc);
rightcDepth=findMaxDepth(s->leftc);
if(leftcDepth>rightcDepth)
return (leftcDepth+1);
else
return (rightcDepth+1);
}

You might also like