Professional Documents
Culture Documents
DATA STRUCTURE
CAP-205
Course Instructor: _Mr. SANJAY SOOD Course Tutor (if applicable): ___________ _
Declaration:
I declare that this assignment is my individual work. I have not copied from any
other student’s work or from any other source except where due
acknowledgment is made explicitly in the text, nor has any part been written for
me by another person.
Student’s Signature
RUMANI BHANDARI
Evaluator’s comments:
_______________________________________________________________
QUESTION 1: - suppose the following sequence list of a binary tree T in pre-order and in-
order respectively.
Pre-order: - G, B, Q, A, C, K, F, P, D, E, R, H
In-order: - Q, B, K, C, F, A, G, P, E, D, H, R
ANSWER: -
STEP 1: -
Pre-order: - G, B, Q, A, C, K, F, P, D, E, R, H
In-order: - Q, B, K, C, F, A, G, P, E, D, H, R
Q, B, K, C, F, A P, E, D, H, R
STEP 2: -
Pre-order: - G, B, Q, A, C, K, F, P, D, E, R, H
G
B
P, E, D, H, R
Q
K, C, F, A
Pre-order: - G, B, Q, A, C, K, F, P, D, E, R, H
G
B
P, E, D, H, R
Q
K, C, F, A
STEP 4: -
v
Pre-order: - G, B, Q, A, C, K, F, P, D, E, R, H
G
B
P, E, D, H, R
Q A
K, C, F
B
P, E, D, H, R
Q A
K F
STEP 6: -
v
Pre-order: - G, B, Q, A, C, K, F, P, D, E, R, H
G
B P
Q
A E, D, H, R
K F
STEP 7: -
v
Pre-order: - G, B, Q, A, C, K, F, P, D, E, R, H
G
B P
Q A D
A E
H, R
K F
STEP 8: -
v
Pre-order: - G, B, Q, A, C, K, F, P, D, E, R, H
G
B P
Q A D
A E
K F H
Pre-Order: A B E H Q R C D K L M
In-Order: B Q R H E A D L K M C
ANSWER: -
TREE IS AS FOLLOW: -
A
B C
E D
H K
Q L M
HEIGHT (DEPTH): - It is the maximum level of any node in a given tree. In the above tree,
the root node A has the maximum level. That is the number of levels one can descend the tree
from its root to the terminal nodes (leaves). The term height is also used to denote the depth.
A,B,C,E,D,H,K,Q,L,M,R
POST-ORDER SEQUENCE: -
R, Q, H, E, B, L, M, K, D, C, A
QUESTION 3: -
ROOT
14
1 H 4 11
2 R 0 0
3 17
4 P 0 0
5 B 18 7
6 3
7 E 1 0
8 6
9 C 0 10
10 F 15 16
11 Q 0 12
12 S 0 0
13 0
14 A 5 9
16 L 0 0
17 13
18 D 0 0
ANSWER: -
B C
D E F
H K L
P Q R
ANSWER: -
ALGORITHM: -
4. [RIGHT child?]
a. If RIGHT[PTR]!=NULL, then
i. [PUSH on STACK.]
b. [End of if structure.]
5. [LEFT child?]
a. If LEFT[PTR]!=NULL, then
i. Set PTR:=LEFT[PTR],
c. [End of if structure.]
7. Exit.
#include<stdio.h>
#include<conio.h>
#include<malloc.h>
node *get_node();
/*Main Function*/
void main()
{
int ch,ans=5;
node *New,*root;
root=NULL;
clrscr();
while(1)
{
printf("\nEnter:\n1-Create\n2-Preorder\n3-Exit\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nEnter Elements 1 by 1: (0 to stop
entering)\n");
do
{
New=get_node();
scanf("%d",&New->data);
ans=New->data;
if(ans!=0)
if(root==NULL)
root=New;
else
insert(root,New);
}while(ans!=0);
break;
case 2:
/*Get node*/
node *get_node()
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->left=NULL;
temp->right=NULL;
return temp;
}
/*Insert Function*/
void insert(node *root,node *New)
{
if(New->data < root->data)
{
if(root->left==NULL)
root->left=New;
else
insert(root->left,New);
}
if(New->data > root->data)
{
if(root->right==NULL)
root->right=New;
else
insert(root->right,New);
}
}
/*preorder Traversals*/
void preorder(node *temp)
OUTPUT:
ANSWER: -
PROGRAM: -
Bhb
#include<conio.h>
#include<stdio.h>
#include<malloc.h>
#include<process.h>
struct node
{
struct node *llink;
struct node *rlink;
int data;
};
void main()
{
struct node *head,*t;
int s,d;
struct node* finsert();
struct node* delenode(struct node*,int);
void insert(struct node *);
void inorder(struct node *);
clrscr();
head=NULL;
do
{
printf("\n\n1-Insertion\n");
printf("2-Deletion\n");
printf("3-Inorder\n");
printf("4-Exit\n");
printf("Enter Choice:\n");
scanf("%d",&s);
switch(s)
{
case 1://insertion
if(head==NULL)
{
head=finsert();
}
else
case 2://Deletion
if(head==NULL)
printf("\nBinary Tree Empty.......\n");
else
{
printf("\nEnter Data to delete : ");
scanf("%d",&d);
if(head->llink==NULL && head->rlink==NULL && head-
>data==d)
{
t=head;
head=NULL;
free(t);
}
else
head = delenode(head,d);
}
break;
case 4://exit
exit(0);
}
}while(s<5 ||s>0);
getch();
}
OUTPUT: -
ANSWER: -
MAX HEAP: -
STEP 1: -
1 1 1
1 1 1
9 9 7
STEP 2: -
1
1
9 7
1
5
1
1
1
7
5
1
5
DATA STRUCTURE Page 19
1
1
7
STEP 3: -
1
5
1
7
1
9 1
4
1
5
1
7
4
1
9
1
STEP 4: -
1
7
4
9 1 8
1
1
5
1
8
4
9 1 7
1
STEP 5: -
1
5
1
8
4
1 1
9 7
1 0
1
5
DATA STRUCTURE Page 21
1 1
4 0
9 1 7 8
1
STEP 6: -
1
5
1 1
4 0
9 1 7 8
1
1
3
1
5
1 1
4 0
1 1 7 8
3 1