0% found this document useful (0 votes)
18 views18 pages

44 - DS Assignment 6

Uploaded by

adityatemgire123
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)
18 views18 pages

44 - DS Assignment 6

Uploaded by

adityatemgire123
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/ 18

| anay More Ds T W TF S

Illoj24 M

Page No.:
YOUVA

As>ighnent -6 Date

Pon blem htment:


Inmglemend ofbinay
baton binay
pevtom klaiy ogeahoy
te ard tyesal crsive and
hon- xtwsi Ve.

Objeckve:
1: Vnder standy data
2: To stcs.
wdersand lgo vifoyn deve logmen
a
yiTo shvdy diffeAnt
opesakons: on binarp seasch.
Bina Trce gasics
AA 8ind teesa data steh
at mOt
her esch node has
nhildtn, ed to as left or rght child.
J+ is idely sed in seayehing, sohn g and hienachica
darr rftsen tabon.
2)| ) ) Binat e' A inay in hich eveot ndk hàs Oor2
thild
level
Rregt fossiby the last is kly kNel
) Seed Bingy Tee Evegy nodk ha ony one child
y Binart Sedh Tee: AL elemens jn left subbee ax less than
ot hile in nght svee ae mot,
3(aatng a binay tee typically invohes dekaing 4 voot nede
tbee by linting nodes.
And tken -lon shetng ne
all he nedes
ot atee in sgeeihe oYer Dhex axt thee main ype:
it and then
MT WTFS S
Page No.
YOUVA
|Date:

ight.
S))Re wosive tavesaljs a naval hy, vhex he orefoy
sbbee.
Non twysive haÝesal cqnes ng stact to stimote
He hchon ca)l .stact se din

Jmgle mentaton:

Test conitonsi
)Fv node as eithex D os 2 (hilden
2) interna. nodes have '2(hildcn drd dk leaves
level.
2 Dme te has sthe wt
yandor stde wth a mix of nade
having Or Os too chillen.
Psvedo ode
) Gcate:
Algos h ceat.xSbwcd beenode 0) {

ice
Accept (hoip hetrey data ss added to left of
ch: Alocat dnd arceqtth
Set left ond rg+ of wx pade to Null

3
M W T F

Page No.:
YOU
Date:

Al oate 4
to Nolli
yeateY
3

Algon hm inodery (Te node *tmp)


{inoylexy temg left))
inoher (tegterng nght)

35

Plgorhy qesto Yaer y(bee nodo^ tmp)


it (tomp! Mv)
qostoder (trig ght)
MT W T F S
|Page No.:
Date: YOUVA

logy
ce node Copy (be Node

Allocate memoy fr tomp


emp lett = copy boot >lef4);

left ar0
Sray left and
miyy-Yoot lef)

miOY Y (2ot ight);

Trme lomplerity
Date:

oneluyion'
husinle mented Binay be and etosm ogeskons
Pina e
FAQ
)A dath stche whee each node has at most
childoem_,called left and ig h+ child
prcess ef visitng 4l nodes in o ee na spente
sslaying bee data.
be oher' Vist oot, let4 tee ight sbbee
ost oor isit left subtbe, nghtsub bee eed
Inedey Visit left subbce,20t, ight btee
leyel o Visit hodes Jevelt level.

Aeusi ve ) ses hnchon call bo Ayeyse he deta.


" Von - wrsive: Vses date shvcs ike stacks o
4ueves to simu) att te bayegl
itnovt t s ì on
CODE:

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;

void create_r(struct treenode *root) {


char a, b;
struct treenode *temp = root;

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

void inorder_r(struct treenode *temp) {


if (temp != NULL) {
inorder_r(temp->left);
printf("%d \t", temp->data);
inorder_r(temp->right);
}
}

void preorder_r(struct treenode *temp) {


if (temp != NULL) {
printf("%d \t", temp->data);
preorder_r(temp->left);
preorder_r(temp->right);
}
}

void postorder_r(struct treenode *temp) {


if (temp != NULL) {
postorder_r(temp->left);
postorder_r(temp->right);
printf("%d \t", temp->data);
}
}

int main() {
int s;
struct treenode *root;
root = (struct treenode *)malloc(sizeof(struct treenode));

printf("Enter the data for the root:\n");


scanf("%d", &root->data);
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_r(root);
break;
case 2: printf("Inorder Traversal:\n");
inorder_r(root);
break;

case 3 :printf("\nPreorder Traversal:\n");


preorder_r(root);
break;

case 4: printf("\nPostorder Traversal:\n");


postorder_r(root);
break;
default : printf("Enter the valid input");
break;

}
while (s!=5);
return 0;
}
OUTPUT:

Enter the data for the root:

1. Create

2. Display Inorder

3. Display Preorder

4. Display Postorder
1

Do you want to add it to the left side of 1 (y/n)?

Enter the data to the left of 1:

Do you want to add it to the left side of 1 (y/n)?

Enter the data to the left of 1:

Do you want to add it to the left side of 2 (y/n)?

Do you want to add it to the right side of 2 (y/n)?

Enter the data to the right of 2:

Do you want to add it to the left side of 3 (y/n)?

Do you want to add it to the right side of 3 (y/n)?

Do you want to add it to the right side of 1 (y/n)?

Enter the data to the right of 1:

Do you want to add it to the left side of 4 (y/n)?

Enter the data to the left of 4:

Do you want to add it to the left side of 6 (y/n)?

Do you want to add it to the right side of 6 (y/n)?

n
Do you want to add it to the right side of 4 (y/n)?

Do you want to add it to the right side of 1 (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 top = -1;


int size = 10;

struct node *stack[10];

int isEmpty()
{
if (top == -1)
{
return 1;
}
else
return 0;
}

int isfull()
{
if (top == size - 1)
{
return 1;
}
else
return 0;
}

void push(struct node *ch)


{
if (!isfull())
{
top = top + 1;
stack[top] = ch;
}
}
struct node *pop()
{
if (!isEmpty())
{
struct node *temp = stack[top];
top = top - 1;
return (temp);
}
}

void create_nr(struct node *root)


{
int flag = 0;
char choice = 'y';
struct node *temp;

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

printf("Do you want to continue adding nodes? (y/n)\n");


scanf(" %c", &choice);
} while (choice == 'y');
}

void inorder_nr(struct node *root)


{
struct node *temp;
temp = root;

while (1)
{
while (temp != NULL)
{
push(temp);
temp = temp->left;
}
if (isEmpty())
{
break;
}
temp = pop();
printf("%d \t", temp->data);
temp = temp->right;
}
}

void preorder_nr(struct node *root)


{
struct node *temp;
temp = root;
while (1)
{
while (temp != NULL)
{
printf("%d \t", temp->data);
push(temp);
temp = temp->left;
}
if (isEmpty())
{
break;
}
temp = pop();
temp = temp->right;
}
}

void postorder_nr(struct node *root)


{
struct node *temp;
temp = root;
while (1)
{
while (temp != NULL)
{
push(temp);
temp = temp->left;
}
if (stack[top]->right == NULL)
{
temp = pop();
printf("%d \t", temp->data);
}
while (!isEmpty() && stack[top]->right == temp)
{
temp = pop();
printf("%d \t", temp->data);
}
if (isEmpty())
{
break;
}
temp = stack[top]->right;
}
}
int main()
{
int s;
struct node *root;
root = (struct node *)malloc(sizeof(struct node));

printf("Enter the data for the root:\n");


scanf("%d", &root->data);

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:

Enter the data for the root:

1. Create
2. Display Inorder

3. Display Preorder

4. Display Postorder

Enter data:

Do you want to add a node to what position of 1?

1. Left

2. Right

Do you want to continue adding nodes? (y/n)

Enter data:

Do you want to add a node to what position of 1?

1. Left

2. Right

Do you want to add a node to what position of 2?

1. Left

2. Right

Do you want to continue adding nodes? (y/n)

Enter data:

Do you want to add a node to what position of 1?

1. Left

2. Right

2
Do you want to continue adding nodes? (y/n)

Enter data:

Do you want to add a node to what position of 1?

1. Left

2. Right

Do you want to add a node to what position of 2?

1. Left

2. Right

Do you want to add a node to what position of 2?

1. Left

2. Right

Do you want to continue adding nodes? (y/n)

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

You might also like