DSA_QB
DSA_QB
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)
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.
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;
b. Inorder Traversal
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)
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
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;
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.
#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;
}
#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;
}