41 - DS Assignment 6
41 - DS Assignment 6
Rno. 41
fanel B (B2)
ZMELtMENrATIDN
flatioum 69-kit open taarle linu Or its derivation.
1eat cb nditisns
D fve4 nodtu tithtr 0ar 2 children.
AI nMnal nodu hare 2childatn all leaxtt av.
ttmpl ULL
Anrdrrttmp lettl.
hint tem data,
itArderlih gh).
2.
cander
fkcardeI (tem
ErtAdertem tant.
Post order
Alatrithm fattorderr(ttenode tenp) £
f (t rmpl NULL)
fost0rder (tenp left),
fort ordeI_Y (Htmp>igh):
funt (ttm >d ata);
)(opu
+rcenode *copy[TrceNtde *rost)
returnttmpi
4MirOT root) E
Alaozithm ToiTOT-T (Treenode
Swap lett and right
OkEDUCATIONALL USE
DI
Time (omplexity
Create = 0 (n)
Display 3 0(n)
)Delete
Copy
(oncusion: Thu, implemented Binany Tree ke rfom
O4eration on Binay tre
EAQs
A data structurc wwhere each_ node has at mort twe
childaco catled leßk a riqht child
DA roeey st uiuiting_alt mads in ate in a spei~L order.
T is imparting for cearchinmoditying T diplaying
tree data
RECURSIVE:
#include <stdio.h>
#include <stdlib.h>
struct treenode {
int data;
struct treenode *left;
struct treenode *right;
};
#define STACK_SIZE 100
struct treenode *stack[STACK_SIZE];
int top = -1;
printf("Do you want to add it to the left side of %d (y/n)? \n", temp-
>data);
scanf(" %c", &a);
if (a == 'y') {
struct treenode *curr = (struct treenode *)malloc(sizeof(struct
treenode));
printf("Enter the data to the left of %d:\n", temp->data);
scanf("%d", &curr->data);
curr->left = NULL;
curr->right = NULL;
temp->left = curr;
create_r(curr);
}
printf("Do you want to add it to the right side of %d (y/n)? \n", temp-
>data);
scanf(" %c", &b);
if (b == 'y') {
struct treenode *curr = (struct treenode *)malloc(sizeof(struct
treenode));
printf("Enter the data to the right of %d:\n", temp->data);
scanf("%d", &curr->data);
curr->left = NULL;
curr->right = NULL;
temp->right = curr;
create_r(curr);
}
}
int main() {
int s;
struct treenode *root;
root = (struct treenode *)malloc(sizeof(struct treenode));
scanf("%d",&s);
switch (s) {
case 1: create_r(root);
break;
case 2: printf("Inorder Traversal:\n");
inorder_r(root);
break;
}
while (s!=5);
return 0;
}
OUTPUT:
1. Create
2. Display Inorder
3. Display Preorder
4. Display Postorder
1
n
Do you want to add it to the right side of 4 (y/n)?
1. Create
2. Display Inorder
3. Display Preorder
4. Display Postorder
Inorder Traversal:
2 3 1 6 4 1
1. Create
2. Display Inorder
3. Display Preorder
4. Display Postorder
Preorder Traversal:
1 1 2 3 4 6
1. Create
2. Display Inorder
3. Display Preorder
4. Display Postorder
Postorder Traversal:
3 2 6 4 1 1
NON RECURSIVE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *left;
struct node *right;
};
int isEmpty()
{
if (top == -1)
{
return 1;
}
else
return 0;
}
int isfull()
{
if (top == size - 1)
{
return 1;
}
else
return 0;
}
do
{
temp = root;
flag = 0;
struct node *curr;
curr = (struct node *)malloc(sizeof(struct node));
curr->left = NULL;
curr->right = NULL;
printf("Enter data:\n");
scanf("%d", &curr->data);
while (flag == 0)
{
int ch;
printf("Do you want to add a node to what position of %d?\n1.
Left\n2. Right\n", temp->data);
scanf("%d", &ch);
if (ch == 1)
{
if (temp->left == NULL)
{
temp->left = curr;
flag = 1;
}
temp = temp->left;
}
else if (ch == 2)
{
if (temp->right == NULL)
{
temp->right = curr;
flag = 1;
}
temp = temp->right;
}
else
{
printf("Invalid choice!\n");
}
}
while (1)
{
while (temp != NULL)
{
push(temp);
temp = temp->left;
}
if (isEmpty())
{
break;
}
temp = pop();
printf("%d \t", temp->data);
temp = temp->right;
}
}
root->left = NULL;
root->right = NULL;
do
{
printf("\n\n1. Create\n");
printf("2. Display Inorder\n");
printf("3. Display Preorder\n");
printf("4. Display Postorder\n");
scanf("%d", &s);
switch (s)
{
case 1:
create_nr(root);
break;
case 2:
printf("Inorder Traversal:\n");
inorder_nr(root);
break;
case 3:
printf("\nPreorder Traversal:\n");
preorder_nr(root);
break;
case 4:
printf("\nPostorder Traversal:\n");
postorder_nr(root);
break;
default:
printf("Enter the valid input");
break;
}
} while (s != 5);
return 0;
}
OUTPUT:
1. Create
2. Display Inorder
3. Display Preorder
4. Display Postorder
Enter data:
1. Left
2. Right
Enter data:
1. Left
2. Right
1. Left
2. Right
Enter data:
1. Left
2. Right
2
Do you want to continue adding nodes? (y/n)
Enter data:
1. Left
2. Right
1. Left
2. Right
1. Left
2. Right
1. Create
2. Display Inorder
3. Display Preorder
4. Display Postorder
Inorder Traversal:
5 2 2 1 4
1. Create
2. Display Inorder
3. Display Preorder
4. Display Postorder
Preorder Traversal:
1 2 2 5 4
1. Create
2. Display Inorder
3. Display Preorder
4. Display Postorder
Postorder Traversal:
5 2 2 4 1