You are on page 1of 22

HOME WORK – 3

DATA STRUCTURE
CAP-205

Homework Title / No. : ___ 3_____________Course Code: __CAP-(205) _

Course Instructor: _Mr. SANJAY SOOD Course Tutor (if applicable): ___________ _

Student’s Roll No. A08 Section No. : D3002

.

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

_______________________________________________________________

DATA STRUCTURE Page 1

PART – A

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

Draw the diagram of the tree.

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

DATA STRUCTURE Page 2

STEP 3: -

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

DATA STRUCTURE Page 3

STEP 5: -
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 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

DATA STRUCTURE Page 4

R

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

DATA STRUCTURE Page 5

QUESTION 2: - For a binary tree T, the Pre-order and In-order traversal sequence are as
follows.

 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

3. What is its Post-Order traversal sequence?

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.

DATA STRUCTURE Page 6

INTERNAL NODE: -

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

15 K 2 0

16 L 0 0

17 13

18 D 0 0

A

B C

D E F

H K L

P Q R

PART - B

ALGORITHM: -

A binary tree T in memory. The algorithm does a pre-order traversal of T, applying an

operation PROCESS to each of its nodes. An array STACK is used to temporarily hold the

3. Apply PROCESS to INFO[PTR],

4. [RIGHT child?]

a. If RIGHT[PTR]!=NULL, then

i. [PUSH on STACK.]

ii. Set TOP:=TOP+1,and STACK[TOP]:=RIGHT[PTR],

b. [End of if structure.]

5. [LEFT child?]

a. If LEFT[PTR]!=NULL, then

i. Set PTR:=LEFT[PTR],

i. Set PTR:=STACK[TOP] and TOP:=TOP-1,

c. [End of if structure.]

7. Exit.

PROGRAM: -

/*Binary Tree Travesals*/

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

typedef struct bst

{
int data;
struct bst *left,*right;
}node;

void insert(node *, node *);

void preorder(node *);

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:

DATA STRUCTURE Page 10

if(root==NULL)
printf("\nNo element In Tree\n");
else
{
printf("\n~~~PREORDER
TRAVERSALS~~~\nThe Tree is:\n");
preorder(root);
}
break;
default:
printf("\n~~~Exit~~~\n");
getch();
exit(0);
break;
}
}
}

/*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)

DATA STRUCTURE Page 11

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

OUTPUT:

DATA STRUCTURE Page 12

QUESTION 5: - Write a program to perform a delete operation for a binary search tree.

PROGRAM: -

Bhb

// Implementation of BINARY TREE OPERATION (insertion & Deletion)

#include<conio.h>
#include<stdio.h>
#include<malloc.h>
#include<process.h>

struct node
{
int data;
};

void main()
{
int s,d;
struct node* finsert();
struct node* delenode(struct node*,int);
void insert(struct node *);
void inorder(struct node *);
clrscr();
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
{
}
else

DATA STRUCTURE Page 13

break;

case 2://Deletion
printf("\nBinary Tree Empty.......\n");
else
{
printf("\nEnter Data to delete : ");
scanf("%d",&d);
>data==d)
{
free(t);
}
else
}
break;

case 3://to display

printf("\n\nIN-ORDER:\n\n");
printf("\n\nBinary Tree Empty....\n\n");
else
break;

case 4://exit
exit(0);
}
}while(s<5 ||s>0);
getch();
}

struct node* finsert()

{
struct node * head;
printf("Enter Data:");
}

DATA STRUCTURE Page 14

{
struct node *t,*n;
n=(struct node *)malloc(sizeof(struct node));
printf("Enter Data:");
scanf("%d",&n->data);
{
if(n->data < t->data)
if(n->data>=t->data)
if((t->llink==NULL) && (n->data < t->data) && (n->data <
break;
if((t->rlink==NULL) && (n->data >= t->data) && (n->data >
break;
}
if((n->data < t->data) && (t->llink==NULL))
if((n->data > t->data) && (t->rlink==NULL))
}

{
{
}
}

struct node * delenode(struct node *head,int d)

{
int f=0,f1=0;
struct node *p,*t,*t1,*x;

while(t!=NULL)
{
if(t->data==d)

DATA STRUCTURE Page 15

{
f=1;
x=t;
break;
}
if(t->data > d)
{
p=t;
}
else if(t->data <= d)
{
p=t;
}
}
if(f==0)
{
}

{
else
free(x);
}

//deleted node has 2 children

{
p=x;
{
p=t1; f1=1;
}
{
x->data=t1->data;
if(f1==1)
if(f1==0)

DATA STRUCTURE Page 16

free(t1);
}
{
x->data=t1->data;
if(f1==1)
if(f1==0)
free(t1);
}
}

{
else
free(x);
}

{
else
free(x);
}
{
free(p);
}

{
free(p);
}
}

OUTPUT: -

DATA STRUCTURE Page 18

11 9 7 15 14 8 10 13

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: -

DATA STRUCTURE Page 20

1
5

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