44 - DS Assignment 6
44 - DS Assignment 6
Illoj24 M
Page No.:
YOUVA
As>ighnent -6 Date
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
35
logy
ce node Copy (be Node
left ar0
Sray left and
miyy-Yoot lef)
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.
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