DS answers UT
DS answers UT
Binary Tree
Binary Tree
A Binary Tree is a data structure where each node has at most two children, often referred to as the left child and the
right child.
Height of a Tree
The Height of a Tree is the length of the longest path from the root to any leaf node. It is measured by the number
of edges on that path.
A Skewed Binary Tree is a binary tree in which all the nodes have only
one child, either the left or the right. It can be left-skewed (only left
children) or right-skewed (only right children).
A Strictly Binary Tree (also called Full Binary Tree) is a binary tree in which every node
has either 0 or 2 children. No node can have only one child.
In this tree, each node either has exactly 2 children (e.g., A, B) or no children at all (e.g.,
D, E, C).
A Complete Binary Tree is a binary tree in which all the levels are completely filled except possibly the last level, and
the nodes at the last level are filled from left to right.
• The last level is filled from left to right (D, E are filled; no gaps to the right).
In a complete binary tree, there are no gaps between the nodes at any level except for the
last level, which is filled in order.
Explain Inorder, Preorder and Postorder Traversal of Binary tree with example.
Inorder
This technique follows the 'left root right' policy. It means that first left subtree is
visited after that root node is traversed, and finally, the right subtree is traversed. As
the root node is traversed between the left and right subtree, it is named inorder
traversal.
• Finally, visit the right subtree. The Inorder Traversal would be: 425136
Preorder
Preorder Traversal is a depth-first traversal method used in binary trees. In this traversal technique, the nodes are
visited in the following order: Root → Left subtree → Right subtree.
PostOrder
Postorder Traversal is a depth-first traversal method used in binary trees. In this traversal technique, the nodes are
visited in the following order: Left subtree → Right subtree → Root.
inorder traversal- D B H E I A F J C G
Postorder Traversal- D H I E B J F G C A
Start with the postorder traversal, and pick the last element as the root.
Recursively construct the left and right subtrees using the corresponding sections of the inorder and postorder
traversals.
Construct Binary tree from following traversal
Postorder traversal- D E F B G L J K H C A
Inorder Traversal- D B F E A G C L J H K
Start with the last element of the postorder list as the root.
Split the inorder list into left and right parts based on the root's index.
Use the postorder list to determine the corresponding left and right parts.
L
Write a C Function to performs following operations on Circular Linked List
if (*head == NULL) {
// If the list is empty, the new node is both head and tail
*head = newNode;
newNode->next = newNode;
} else {
temp = temp->next;
newNode->next = *head;
temp->next = newNode;
*head = newNode;
if (*head == NULL) {
// If the list is empty, the new node is both head and tail
*head = newNode;
newNode->next = newNode;
} else {
temp = temp->next;
temp->next = newNode;
newNode->next = *head;
3. Searching of Node
if (head == NULL)
return 0;
do {
if (temp->data == key)
return 1;
temp = temp->next;
return 0;
int count = 0;
if (head == NULL)
return 0;
do {
count++;
temp = temp->next;
if (head == NULL) {
printf("List is empty.\n");
return;
do {
temp = temp->next;
printf("(head)\n");
In Computer Algebra Systems (CAS), computers work with mathematical expressions like polynomials. These
expressions can change in size (more or fewer terms), and linked lists help manage these changes easily. When
adding polynomials, the system can efficiently go through each term and add them together.
2. Scientific Computing:
In scientific calculations, you often deal with polynomials, especially when solving complex equations like
differential equations. Sometimes, these polynomials have many terms, but most of them are zero (called sparse
polynomials). Linked lists are helpful because they only store the important non-zero terms and adjust their size
as needed.
3. Computer Graphics:
In graphics and animation, polynomials are used to create smooth curves, like the ones in drawing software or
animation paths. These curves can change, and using linked lists allows the system to handle these changes
smoothly when combining different curves or shapes.
4. Control Systems:
In control systems (like robots or automatic machines), polynomials are used to describe how the system
responds to commands. These polynomials change when adjusting settings. Linked lists make it easier to add or
modify these polynomials, helping control systems function efficiently, such as in tuning a machine's response or
solving equations for system stability.
Write an algorithm for Stack using Singly Linked List
Steps:
2. Set the data of the new node to the element being pushed.
3. Set the next pointer of the new node to point to the current top of the stack.
Algorithm :-
PUSH(stack, data):
5. End.
Objective: Remove and return the top element from the stack.
Steps:
3. Move the top pointer to the next node in the list (the node below the current top).
Algorithm :-
POP(stack):
2. Otherwise:
d. Free temp.
e. Return data.
3. End.
3.Algorithm for Peek Operation
Objective: Return the top element without removing it from the stack.
Steps:
Algorithm:
PEEK(stack):
2. Otherwise:
a. Return stack.top.data.
3. End.
Steps:
Algorithm:
ISEMPTY(stack):
1. If stack.top is NULL:
a. Return TRUE.
2. Else:
a. Return FALSE.
3. End.
Write an algorithm for Queue using Singly Linked List
Queue Operations:
Steps:
2. Set the data of the new node to the element being enqueued.
3. If the queue is empty (i.e., both front and rear are NULL):
4. Otherwise:
o Set the next pointer of the current rear node to the new node.
ENQUEUE(queue, data):
4. If queue.rear is NULL:
5. Else:
6. End.
Objective: Remove and return the front element from the queue.
Steps:
1. If the queue is empty (i.e., front is NULL), return an error or indicate underflow.
2. Otherwise:
o Save the data from the front node.
o Move the front pointer to the next node in the list.
o If the front becomes NULL, set the rear to NULL as well (queue becomes empty).
o Free the memory for the old front node.
o Return the saved data.
DEQUEUE(queue):
2. Otherwise:
e. Free temp.
f. Return data.
3. End.
Objective: Return the front element without removing it from the queue.
Steps:
Algorithm:
PEEK(queue):
2. Otherwise:
a. Return queue.front.data.
3. End.
PEEK(queue):
a. Return queue.front.data.
3. End.
Steps:
1. If both front and rear pointers are NULL, return true (queue is empty).
ISEMPTY(queue):
a. Return TRUE.
2. Else:
a. Return FALSE.
3. End.
Write functions in C to perform the following operations on the Doubly Linked List:
This function deletes the node that comes after a given node in the doubly linked list
return;
if (temp->next != NULL) {
temp->next->prev = prev_node;
prev_node->next = temp->next;
free(temp);
}
2. Find the node with the smallest data value.
if (head == NULL) {
return NULL;
smallest = current;
current = current->next;
return smallest;
if (head == NULL) {
return;
current = current->next;
printf("\n");
new_node->data = data;
new_node->next = NULL;
if (*head_ref == NULL) {
new_node->prev = NULL;
*head_ref = new_node;
return;
last = last->next;
}
// Change the next of the last node and the prev of the new node
last->next = new_node;
new_node->prev = last;
}
Write an algorithm to count the number of nodes in DDL
1. Initialize a counter variable count to 0.
2. Set a pointer current to point to the head of the doubly linked list.
4. Once the traversal is complete, the value of count will hold the number of nodes in the list.
5. Return count.
COUNT_NODES_DLL(head):
1. Initialize count = 0
a. Increment count by 1
4. Return count
The algorithm starts by initializing the count to 0 and sets the current pointer to the head of the list.
It traverses the list one node at a time, counting each node by incrementing count in each step.
When the current pointer reaches NULL (end of the list), the loop ends, and the total number of nodes is returned.
Write a C Function to perform following operations on Singly Linked List.
struct Node {
int data;
};
new_node->data = data;
new_node->next = *head_ref;
*head_ref = new_node;
new_node->data = data;
new_node->next = NULL;
if (*head_ref == NULL) {
*head_ref = new_node;
return;
last = last->next;
last->next = new_node;
3. Searching of Node
if (current->data == key) {
current = current->next;
}
printf("Node with value %d not found in the list.\n", key);
int count = 0;
count++;
current = current->next;
return count;
if (current == NULL) {
return;
current = current->next;
printf("\n");