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): ___________ _

Date of Allotment: _____28-March-2011 _____ Date of submission: __ 11-April-2011______

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

Evaluator’s comments:
_______________________________________________________________

Marks obtained: ___________ out of ______________________

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.

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

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

1. What is the height of the tree?

2. What are the internal nodes?

3. What is its Post-Order traversal sequence?

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.

So Height of the tree is 6.

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

Suppose a binary tree is in memory:-

ROOT

14

INFO LEFT RIGHT

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

DATA STRUCTURE Page 7


15 K 2 0

16 L 0 0

17 13

18 D 0 0

Draw the Binary Tree.

ANSWER: -

BINARY TREE IS AS FOLLOW: -


A

B C

D E F

H K L

P Q R

DATA STRUCTURE Page 8


PART - B

QUESTION 4: - Write a program to traverse a binary using pre-order Traversal Technique.

ANSWER: -

ALGORITHM: -

PREORD (INFO, LEFT, RIGHT, ROOT)

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
address of nodes.

1. [Initially push NULL onto STACK, and initialize PTR,]

a. Set TOP :=1, STACK[i]:= NULL and PTR= ROOT,

2. Repeat steps 3 to 5 while PTR!=NULL,

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],

b. Else: [POP from STACK.]

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

c. [End of if structure.]

6. [End of step 2 while loop.]

7. Exit.

DATA STRUCTURE Page 9


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.

ANSWER: -

PROGRAM: -

Bhb

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

#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

DATA STRUCTURE Page 13


insert(head);
break;

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 3://to display


printf("\n\nIN-ORDER:\n\n");
if(head==NULL)
printf("\n\nBinary Tree Empty....\n\n");
else
inorder(head);
break;

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

struct node* finsert()


{
struct node * head;
head=(struct node*)malloc(sizeof(struct node));
printf("Enter Data:");
scanf("%d",&head->data);
head->llink=NULL;
head->rlink=NULL;
return head;
}

void insert(struct node *head)

DATA STRUCTURE Page 14


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

void inorder(struct node * head)


{
if(head!=NULL)
{
inorder(head->llink);
printf("%d ",head->data);
inorder(head->rlink);
}
}

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


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

//to search found or not


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

DATA STRUCTURE Page 15


{
f=1;
x=t;
break;
}
if(t->data > d)
{
p=t;
t=t->llink;
}
else if(t->data <= d)
{
p=t;
t=t->rlink;
}
}
if(f==0)
{
printf("\n\nGiven element not found.......\n\n");
return head;
}

//Deleted node has no child


if(x->llink==NULL && x->rlink==NULL)
{
if(p->rlink==x)
p->rlink=NULL;
else
p->llink=NULL;
free(x);
return head;
}

//deleted node has 2 children


if(x->llink!=NULL && x->rlink!=NULL)
{
p=x;
t1=x->rlink;
while(t1->llink!=NULL)
{
p=t1; f1=1;
t1=t1->llink;
}
if(t1->llink==NULL && t1->rlink==NULL)
{
x->data=t1->data;
if(f1==1)
p->llink=t1->llink;
if(f1==0)

DATA STRUCTURE Page 16


x->rlink=t1->rlink;
free(t1);
return head;
}
if(t1->rlink!=NULL)
{
x->data=t1->data;
if(f1==1)
p->llink=t1->rlink;
if(f1==0)
p->rlink=t1->rlink;
free(t1);
return head;
}
}

//Deleted node has oniy right child


if(x->llink==NULL && x->rlink!=NULL && x->data!=head->data)
{
if(p->llink==x)
p->llink=x->rlink;
else
p->rlink=x->rlink;
free(x);
return head;
}

//Deleted node has oniy left child


if(x->llink!=NULL && x->rlink==NULL && x->data!=head->data)
{
if(p->llink==x)
p->llink=x->llink;
else
p->rlink=x->llink;
free(x);
return head;
}
if(x->llink!=NULL && x->rlink==NULL && x->data==head->data)
{
head=x->llink;
free(p);
return head;
}

DATA STRUCTURE Page 17


if(x->llink==NULL && x->rlink!=NULL && x->data==head->data)
{
head=x->rlink;
free(p);
return head;
}
}

OUTPUT: -

QUESTION 6: - Create a heap for following series:

DATA STRUCTURE Page 18


11 9 7 15 14 8 10 13

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

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

DATA STRUCTURE Page 22