0% found this document useful (0 votes)
11 views27 pages

DSA_QB

Uploaded by

smariaanushka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
11 views27 pages

DSA_QB

Uploaded by

smariaanushka
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 27

3.

Write C func ons for the following:


a. Reverse a singly linked list.

NODE reverse (NODE first)


{
NODE cur,temp;
cur=NULL;
while(first!=NULL) {
temp=first->next;
first->next=cur;
cur=first;
first=temp;
}
return cur;
}

b. Concatena on of two singly linked list

NODE concat (NODE first, NODE sec)


{
NODE cur;
if(first==NULL)
return sec;
if(sec==NULL)
return first;
cur=first;
while(cur->next!=NULL)
cur=cur->next;
cur->next=sec;
return first;
}

c. Find the Length of the List

int length (NODE first)


{
NODE cur;
N=int count=0;
if(first==NULL)
return 0;
cur=first;
while(cur!=NULL) {
count++;
cur=cur->next;
}
return count;
}

9. Define a Tree. With suitable example explain the following

A tree is a set of finite set of one or more nodes that shows a parent-child
rela onship such that:
 There is a special node called root node
 The remaining nodes are par oned into disjoint subsets T1, T2… Tn ;
n>=0, where T1, T2… Tn are all children of root node are themselves
trees called subtrees.

a. Binary Tree

A binary tree is a tree which has finite set of nodes that is either empty
or consists of a root and two subtrees called le subtree and right
subtree
 Root: if a tree is empty, the first node in the tree
 Le subtree: connected to the le of the root
 Right subtree: connected to the right of the root
b. Complete Binary Tree

Complete B.T is a B.T in which every level, except possibly the last level is
completely filled. Also, all the nodes should be filled only from le to
right (at the leaf level)

c. Strictly Binary Tree

A binary tree having 2i nodes in any given level i, is called as strictly


binary tree. Here, every node other than the leaf node has two children

d. Skewed Binary Tree


 A tree consis ng of only le subtree or only right subtree is called
skewed tree
 A tree with only le subtree is called le skewed tree, and a tree
with only right subtree is called right skewed tree

13. Define Binary Tree. List and discuss any two Proper es of Binary Tree.
Property 2
6, 14, 16, Define Binary tree. Explain the representa on of a binary tree
with a suitable example.

1. Array representa on – a tree can be represented using a sequen al


array representa on

 The nodes are numbered sequen ally from 0. The node with
posi on 0 is considered the root node.
 Given the posi on of any node i, 2i+1 gives the posi on of the le
child and 2i+2 gives that posi on of the right child
 If i is the posi on of the le child, the posi on of the right child is
given by i+1
 If i is the posi on of the right child, the posi on of the rle child is
given by i-1
 If i is the posi on of a node, the parent node is given by (i-1)/2
2. Linked list representa on – a node in the tree can have 3 fields
Info – contains the informa on
llink – contains address of the le subtree
rlink – contains address of the right subtree

struct node {
int info;
struct node *llink,*rlink;
};
typedef struct node* NODE;

Ap pointer variable root can be used to point the root node always. If
the root points to NULL, it indicates that the tree is empty
The pointer variable root can be declared and ini alized as:
struct node *root=NULL;
(OR)
NODE root=NULL;

14.Write the C rou nes to traverse the given tree using


a. Pre-order Traversal
It can be recursively defined as follows:
i. Process the root node (N)
ii. Traverse the le subtree (L)
iii. Traverse the right subtree (R)
void Preorder(NODE root) {
if (root==NULL)
return;
printf("%d",root->info);
preorder(root->llink);
preorder(root->rlink);
}

b. Inorder Traversal

i. Traverse the le subtree (L)


ii. Process the root node (N)
iii. Traverse the right subtree (R)
void Inorder(NODE root) {
if (root==NULL)
return;
Inorder(root->llink);
printf("%d",root->info);
Inorder(root->rlink);
}
c. Postorder Traversal

i. Traverse the le subtree (L)


ii. Traverse the right subtree (R)
iii. Process the root node (N)
void Postorder(NODE root) {
if (root==NULL)

return;
Postorder(root->llink);
Postorder(root->rlink);
printf("%d",root->info);
}
8. Construct a binary tree from the Post-order and In-order sequence given
below
In-order: GDHBAEICF
Post-order: GHDBIEFCA
(Preorder, just in case)

15.Define Tree. Explain all the terminologies of Tree

 Tree: A tree is a set of finite set of one or more nodes that shows a
parent- child rela onship such that:
o There is a special node called the root node.
o The remaining nodes are par oned into disjoint subsets T1, T2,
…, Tn; n>=0, where T1, T2, …., Tn which are all children of root
node are themselves trees called subtrees
Terminologies:
Consider the following tree,

 Root: The first node at the top of the tree. Ex. 100 is the root node
 Parent: A node having le subtree or right subtree or both. Ex. 50 is
parent to 70
 Child: Node obtained from the parent node. Ex. 70 is child to 50
 Siblings: Two or more nodes having same parent. Ex. 50 & 60 are siblings
 Ancestors: The nodes obtained in the path from the specified node x
while moving upwards towards the root node. Ex. 60 is ancestor to
35,30,80 & 40
 Descendants: The nodes in the path bellow the parent are called
descendants. Ex. 70 is descendant to 50 & 100
 Le Descendant: The descendant node that are all le of the node x.
Ex.70 is le descendant to 50
 Right Descendant: The descendant node that are all right of the node x.
Ex.40 is le descendant to 60 & 100
 Le Subtree: All descendant nodes that are le of node x. Ex. 80, 35 &30
are le subtrees to 60
 Right Subtree: All descendant nodes that are right of node x. Ex. 40 is
right subtree to 60
 Degree: The number of subtrees of a node is called its degree. Ex.
Degree of 100 is 2
 Leaf: A node in a tree that has the degree zero. Ex. 70, 35, 30 & 40
 Internal nodes: The nodes except leaf nodes in a tree. Ex. 70, 35, 30 &
40
 External nodes: The NULL link any node in a tree. Ex. 70, 35, 30 & 40
 Level: The distance of a node from the root. Ex. The node 35 and 30 are
at a distance of 3 nodes from the root node so their levels are 3
 Height: The height of the tree is the maximum level of any leaf in the
tree. Ex. Height of shown tree is 4

5. Write C Func ons for the following


i) Inser ng a node at the beginning of a Doubly linked list
void InsertBeg(){
if(first==NULL) {
create();
first=last=temp;
}
else {
temp -> next=first;
first -> prq=temp;
first=temp;
}
}
ii) Dele ng a node at the end of the Doubly linked list
void deleteEnd() {
struct node *temp, *temp2;
temp=first;
if(first==NULL) {
printf(“List is Empty\n”);
return;
}
if(temp->next==NULL) {
printf(“The deleted item: %d\n”, temp->info);
free(temp);
first=last=NULL;
}
else {
temp2=last->prev;
temp->next=NULL;
printf(“The deleted item: %d\n”, last->info);
free(last);
last=temp2;
}
}
7. Define the Threaded binary tree. Construct Threaded binary for the following elements: A,
B, C, D, E, F, G, H, I

In threaded binary tree are the trees in which NULL links can be replaced by pointers called
threads, to other nodes in the tree. This facilitates the upward movement in the tree

1. Write the C func on to add two polynomials. Show the linked representa on of the
below two polynomials and their addi on using a circular singly linked list
P1: 5x3 + 4x2 +7x + 3
P2: 6x2 + 5
Output: add the above two polynomials and represent them using the Linked list

#include <stdio.h>
#include <stdlib.h>
struct Node {
int coeff;
int exp;
struct Node *next;
};
struct Node* createNode(int coeff, int exp) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));
newNode->coeff = coeff;
newNode->exp = exp;
newNode->next = newNode;
return newNode;
}
void insertNode(struct Node** head, int coeff, int exp) {
struct Node* newNode = createNode(coeff, exp);
if (*head == NULL) {
*head = newNode;
} else {
struct Node* temp = *head;
while (temp->next != *head) {
temp = temp->next;
}
temp->next = newNode;
newNode->next = *head;
}
}
void display(struct Node* head) {
if (head == NULL) return;
struct Node* temp = head;
do {
printf("%dx^%d ", temp->coeff, temp->exp);
if (temp->next != head) printf("+ ");
temp = temp->next;
} while (temp != head);
printf("\n");
}
singly linked lists
struct Node* addPolynomials(struct Node* poly1, struct Node*
poly2) {
struct Node* result = NULL;
struct Node *p1 = poly1, *p2 = poly2;
do {
if (p1->exp > p2->exp) {
insertNode(&result, p1->coeff, p1->exp);
p1 = p1->next;
} else if (p1->exp < p2->exp) {
insertNode(&result, p2->coeff, p2->exp);
p2 = p2->next;
} else {
int sumCoeff = p1->coeff + p2->coeff;
insertNode(&result, sumCoeff, p1->exp);
p1 = p1->next;
p2 = p2->next;
}
} while (p1 != poly1 && p2 != poly2);
while (p1 != poly1) {
insertNode(&result, p1->coeff, p1->exp);
p1 = p1->next;
}
while (p2 != poly2) {
insertNode(&result, p2->coeff, p2->exp);
p2 = p2->next;
}

return result;
}
struct Node* inputPolynomial() {
struct Node* poly = NULL;
int n, coeff, exp;

printf("Enter the number of terms in the polynomial: ");


scanf("%d", &n);

for (int i = 0; i < n; i++) {


printf("Enter coefficient and exponent for term %d: ", i
+ 1);
scanf("%d %d", &coeff, &exp);
insertNode(&poly, coeff, exp);
}
return poly;
}
int main() {
printf("Enter the first polynomial:\n");
struct Node* poly1 = inputPolynomial();
printf("Enter the second polynomial:\n");
struct Node* poly2 = inputPolynomial();
printf("Polynomial 1: ");
display(poly1);
printf("Polynomial 2: ");
display(poly2);
struct Node* result = addPolynomials(poly1, poly2);
printf("Sum of polynomials: ");
display(result);
return 0;
}
Output

2. Define Sparse matrix. For the following given sparse matrix, give the linked list
representa on:
A sparse matrix is a matrix in which most of the elements are zero. It is usually stored in a
memory-efficient way by only storing the non-zero elements, along with their row and
column posi ons. Using a linked list representa on
12. Discuss how to implement Stacks and QUEUES using Linked List.

1. Stack Using Linked List


A stack follows the Last-In-First-Out (LIFO) principle, meaning the most recently added element
is the first one to be removed. In a linked list implementa on of a stack, we typically use the
head (or top) of the list to perform push and pop opera ons efficiently.
Structure of the Node
Each node in the linked list contains:
 Data (the value being stored)
 A pointer to the next node
Stack Opera ons
1. Push: Add a new node at the beginning (top) of the linked list.
2. Pop: Remove the node from the beginning (top) of the linked list.
3. Peek: View the data of the node at the beginning (top) of the linked list without
removing it.
4. isEmpty: Check if the linked list is empty
Code for Stack Using Linked List

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* top = NULL;
void push(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));
newNode->data = value;
newNode->next = top;
top = newNode;
printf("%d pushed to stack\n", value);
}
int pop() {
if (top == NULL) {
printf("Stack underflow\n");
return -1;
}
struct Node* temp = top;
int poppedValue = temp->data;
top = top->next;
free(temp);
return poppedValue;
}
int peek() {
if (top == NULL) {
printf("Stack is empty\n");
return -1;
}
return top->data;
}
int isEmpty() {
return top == NULL;
}
int main() {
push(10);
push(20);
push(30);
printf("Top element is %d\n", peek());
printf("%d popped from stack\n", pop());
printf("Top element is now %d\n", peek());
return 0;
}

2. Queue Using Linked List


A queue follows the First-In-First-Out (FIFO) principle, meaning the first element added is the
first one to be removed. In a linked list implementa on of a queue, we need two pointers, front
and rear, to keep track of the beginning and end of the list.
Structure of the Node
Each node in the linked list contains:
 Data (the value being stored)
 A pointer to the next node
Queue Opera ons
1. Enqueue: Add a new node at the end (rear) of the linked list.
2. Dequeue: Remove the node from the beginning (front) of the linked list.
3. Peek: View the data of the node at the beginning (front) of the linked list without
removing it.
4. isEmpty: Check if the linked list is empty.
Code for Queue Using Linked List

#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
struct Node* front = NULL;
struct Node* rear = NULL;
void enqueue(int value) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));
newNode->data = value;
newNode->next = NULL;
if (rear == NULL) {
front = rear = newNode;
printf("%d enqueued to queue\n", value);
return;
}
rear->next = newNode;
rear = newNode;
printf("%d enqueued to queue\n", value);
}
int dequeue() {
if (front == NULL) {
printf("Queue underflow\n");
return -1;
}
struct Node* temp = front;
int dequeuedValue = temp->data;
front = front->next;
if (front == NULL) rear = NULL;
free(temp);
return dequeuedValue;
}
int peek() {
if (front == NULL) {
printf("Queue is empty\n");
return -1;
}
return front->data;
}
int isEmpty() {
return front == NULL;
}
int main() {
enqueue(10);
enqueue(20);
enqueue(30);
printf("Front element is %d\n", peek());
printf("%d dequeued from queue\n", dequeue());
printf("Front element is now %d\n", peek());
return 0;
}

4. Write recursive C func ons for inorder, preorder and postorder traversals of a binary tree.
Also, find all the traversals for the given tree.
#include <stdio.h>
#include <stdlib.h>
struct Node {
char data;
struct Node* left;
struct Node* right;
};
struct Node* createNode(char data) {
struct Node* newNode = (struct Node*)malloc(sizeof(struct
Node));
newNode->data = data;
newNode->left = newNode->right = NULL;
return newNode;
}
void preorder(struct Node* root) {
if (root == NULL) return;
printf("%c ", root->data);
preorder(root->left);
preorder(root->right);
}
void inorder(struct Node* root) {
if (root == NULL) return;
inorder(root->left);
printf("%c ", root->data);
inorder(root->right);
}
)
void postorder(struct Node* root) {
if (root == NULL) return;
postorder(root->left);
postorder(root->right);
printf("%c ", root->data);
}
int main() {
struct Node* root = createNode('A');
root->left = createNode('B');
root->right = createNode('C');
root->left->left = createNode('D');
root->left->right = createNode('E');
root->left->right->left = createNode('H');
root->left->right->right = createNode('I');
root->right->right = createNode('G');
printf("Preorder traversal: ");
preorder(root);
printf("\n");
printf("Inorder traversal: ");
inorder(root);
printf("\n");
printf("Postorder traversal: ");
postorder(root);
printf("\n");
return 0;
}

1. Preorder Traversal (Root, Le , Right): A B D E H I C G


2. Inorder Traversal (Le , Root, Right): D B H E I A C G
3. Postorder Traversal (Le , Right, Root): D H I E B G C A
11. List of out the Differences between Singly Linked and Doubly Linked List.

You might also like