0% found this document useful (0 votes)
4 views

DS answers UT

The document defines various types of binary trees, including Binary Tree, Height of Tree, Skewed Binary Tree, Strictly Binary Tree, and Complete Binary Tree, along with examples. It explains tree traversal methods: Inorder, Preorder, and Postorder, and provides algorithms for constructing binary trees from traversals. Additionally, it covers operations on Circular Linked Lists, applications of singly linked lists in polynomial addition, and algorithms for stack and queue implementations using singly linked lists.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DS answers UT

The document defines various types of binary trees, including Binary Tree, Height of Tree, Skewed Binary Tree, Strictly Binary Tree, and Complete Binary Tree, along with examples. It explains tree traversal methods: Inorder, Preorder, and Postorder, and provides algorithms for constructing binary trees from traversals. Additionally, it covers operations on Circular Linked Lists, applications of singly linked lists in polynomial addition, and algorithms for stack and queue implementations using singly linked lists.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 20

Define with example- Binary Tree, Height of Tree, Skewed Binary Tree, Strictly Binary Tree, Complete

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.

Example: Node 1 is the root of the binary tree.

Node 2 has two children (4 and 5).

Node 3 has no children (i.e., it is a leaf node).

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.

The longest path is from 1 → 2 → 4 or 1 → 2 → 5, both with 2 edges.

Height of this tree = 2

kewed Binary Tree

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).

Strictly Binary Tree

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).

Complete Binary Tree

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.

• All levels except the last are completely filled.

• 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.

In Inorder traversal, the nodes are visited in the following order:

• First, visit the left subtree.

• Then, visit the root node.

• 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.

• First, visit the root node.


• Then, visit the left subtree.
• Finally, visit the right subtree. The Preorder Traversal would be: 12453

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.

• First, visit the left subtree.


• Then, visit the right subtree.
• Finally, visit the root node. The Postorder Traversal would be: 45231

Construct Binary tree from following traversal

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.

Find this root in the inorder traversal.

Split the inorder list into two parts:

• Left of the root for the left subtree.

• Right of the root for the right subtree.

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.

Find this root in the inorder list.

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.

Recursively construct the left and right subtrees.

L
Write a C Function to performs following operations on Circular Linked List

1. Insertion of node at the beginning

// Function to insert a node at the beginning

void insertAtBeginning(struct Node** head, int data) {

struct Node* newNode = createNode(data);

struct Node* temp = *head;

if (*head == NULL) {

// If the list is empty, the new node is both head and tail

*head = newNode;

newNode->next = newNode;

} else {

// Traverse the list to find the last node

while (temp->next != *head)

temp = temp->next;

newNode->next = *head;

temp->next = newNode;

*head = newNode;

2. Insertion of node at the end

// Function to insert a node at the end

void insertAtEnd(struct Node** head, int data) {

struct Node* newNode = createNode(data);

struct Node* temp = *head;

if (*head == NULL) {

// If the list is empty, the new node is both head and tail

*head = newNode;

newNode->next = newNode;

} else {

// Traverse the list to find the last node

while (temp->next != *head)

temp = temp->next;
temp->next = newNode;

newNode->next = *head;

3. Searching of Node

// Function to search for a node in the list

int searchNode(struct Node* head, int key) {

struct Node* temp = head;

if (head == NULL)

return 0;

do {

if (temp->data == key)

return 1;

temp = temp->next;

} while (temp != head);

return 0;

4. Counting the no of nodes in List

// Function to count the number of nodes

int countNodes(struct Node* head) {

struct Node* temp = head;

int count = 0;

if (head == NULL)

return 0;

do {

count++;

temp = temp->next;

} while (temp != head);


return count;

5. Display the List

// Function to display the list

void displayList(struct Node* head) {

struct Node* temp = head;

if (head == NULL) {

printf("List is empty.\n");

return;

do {

printf("%d -> ", temp->data);

temp = temp->next;

} while (temp != head);

printf("(head)\n");

Explain application of SLL polynomial addition


1. Symbolic Mathematics:

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

Algorithm for Push Operation

Objective: Add an element to the top of the stack.

Steps:

1. Create a new node.

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.

4. Update the top pointer to the new node.

Algorithm :-

PUSH(stack, data):

1. Create a new node, newNode.

2. Set newNode.data = data.

3. Set newNode.next = stack.top.

4. Set stack.top = newNode.

5. End.

2. Algorithm for Pop Operation

Objective: Remove and return the top element from the stack.

Steps:

1. If the stack is empty, return an error or indicate underflow.

2. Save the top element's data.

3. Move the top pointer to the next node in the list (the node below the current top).

4. Free the memory for the old top node.

5. Return the saved data.

Algorithm :-

POP(stack):

1. If stack.top is NULL, then:

a. Display "Stack Underflow" and return.

2. Otherwise:

a. Set temp = stack.top.

b. Set data = temp.data.

c. Set stack.top = stack.top.next.

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:

1. If the stack is empty, return an error or indicate underflow.

2. Otherwise, return the data of the top node.

Algorithm:

PEEK(stack):

1. If stack.top is NULL, then:

a. Display "Stack is empty" and return.

2. Otherwise:

a. Return stack.top.data.

3. End.

4. Algorithm for isEmpty Operation

Objective: Check if the stack is empty.

Steps:

1. If the top pointer is NULL, return true (stack is empty).

2. Otherwise, return false (stack is not empty).

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:

1. Enqueue: Add an element to the rear of the queue.

2. Dequeue: Remove and return the front element of the queue.

3. Peek (Front): View the front element without

Algorithm for Enqueue Operation

Objective: Add an element to the rear of the queue.

Steps:

1. Create a new node.

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

o Set both front and rear to point to the new node.

4. Otherwise:

o Set the next pointer of the current rear node to the new node.

o Update the rear pointer to the new node.

ENQUEUE(queue, data):

1. Create a new node, newNode.

2. Set newNode.data = data.

3. Set newNode.next = NULL.

4. If queue.rear is NULL:

a. Set queue.front = queue.rear = newNode.

5. Else:

a. Set queue.rear.next = newNode.

b. Set queue.rear = newNode.

6. End.

Algorithm for Dequeue Operation

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

1. If queue.front is NULL, then:

a. Display "Queue Underflow" and return.

2. Otherwise:

a. Set temp = queue.front.

b. Set data = temp.data.

c. Set queue.front = queue.front.next.

d. If queue.front is NULL, set queue.rear = NULL.

e. Free temp.

f. Return data.

3. End.

Algorithm for Peek (Front) Operation

Objective: Return the front element without removing it from the queue.

Steps:

1. If the queue is empty, return an error or indicate underflow.

2. Otherwise, return the data of the front node.

Algorithm:

PEEK(queue):

1. If queue.front is NULL, then:

a. Display "Queue is empty" and return.

2. Otherwise:

a. Return queue.front.data.

3. End.

PEEK(queue):

1. If queue.front is NULL, then:

a. Display "Queue is empty" and return.


2. Otherwise:

a. Return queue.front.data.

3. End.

Algorithm for isEmpty Operation

Objective: Check if the queue is empty.

Steps:

1. If both front and rear pointers are NULL, return true (queue is empty).

2. Otherwise, return false (queue is not empty).

ISEMPTY(queue):

1. If queue.front is NULL, then:

a. Return TRUE.

2. Else:

a. Return FALSE.

3. End.
Write functions in C to perform the following operations on the Doubly Linked List:

1. Delete a node after a given node.


Delete a Node After a Given Node

This function deletes the node that comes after a given node in the doubly linked list

// Function to delete a node after a given node

void deleteAfter(struct Node* prev_node) {

// Check if the given node or next node is NULL

if (prev_node == NULL || prev_node->next == NULL) {

printf("The given node is NULL or it has no next node to delete.\n");

return;

// Store the node to be deleted

struct Node* temp = prev_node->next;

// If the node to be deleted is not the last node

if (temp->next != NULL) {

temp->next->prev = prev_node;

// Change the next pointer of the previous node

prev_node->next = temp->next;

// Free the memory of the node to be deleted

free(temp);

printf("Node deleted after the given node.\n");

}
2. Find the node with the smallest data value.

// Function to find the node with the smallest data value

struct Node* findSmallestNode(struct Node* head) {

// If the list is empty, return NULL

if (head == NULL) {

printf("The list is empty.\n");

return NULL;

struct Node* current = head;

struct Node* smallest = head;

// Traverse the list to find the smallest node

while (current != NULL) {

if (current->data < smallest->data) {

smallest = current;

current = current->next;

printf("Smallest node found with value: %d\n", smallest->data);

return smallest;

3. Display the List.

// Function to display the doubly linked list

void displayList(struct Node* head) {

if (head == NULL) {

printf("The list is empty.\n");

return;

struct Node* current = head;

printf("Doubly Linked List: ");


// Traverse the list and print each node's data

while (current != NULL) {

printf("%d ", current->data);

current = current->next;

printf("\n");

4. Insert a node at the end of the List.

// Function to insert a new node at the end of the list

void insertAtEnd(struct Node** head_ref, int data) {

// Allocate memory for the new node

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

struct Node* last = *head_ref; // Used to traverse to the last node

// Assign data to the new node and set its pointers

new_node->data = data;

new_node->next = NULL;

// If the list is empty, make the new node the head

if (*head_ref == NULL) {

new_node->prev = NULL;

*head_ref = new_node;

printf("Node with value %d inserted at the end.\n", data);

return;

// Traverse the list to find the last node

while (last->next != NULL) {

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;

printf("Node with value %d inserted at the end.\n", data);

}
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.

3. Traverse the list until current becomes NULL.

o For each node, increment the counter count by 1.

o Move current to the next node by setting current = current->next.

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

2. Set current = head

3. While current is not NULL:

a. Increment count by 1

b. Move current to the next node (current = current->next)

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.

1. Insertion of node at the beginning

// Structure for a node in singly linked list

struct Node {

int data;

struct Node* next;

};

// Function to insert a new node at the beginning

void insertAtBeginning(struct Node** head_ref, int data) {

// Allocate memory for the new node

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

// Set the data for the new node

new_node->data = data;

// Point the new node's next to the current head

new_node->next = *head_ref;

// Update the head to point to the new node

*head_ref = new_node;

printf("Node with value %d inserted at the beginning.\n", data);

2. Insertion of node at the end

// Function to insert a new node at the end of the list

void insertAtEnd(struct Node** head_ref, int data) {

// Allocate memory for the new node

struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));

// Set the data for the new node

new_node->data = data;
new_node->next = NULL;

// If the list is empty, set the new node as the head

if (*head_ref == NULL) {

*head_ref = new_node;

printf("Node with value %d inserted at the end.\n", data);

return;

// Traverse to the last node

struct Node* last = *head_ref;

while (last->next != NULL) {

last = last->next;

// Link the new node to the end of the list

last->next = new_node;

printf("Node with value %d inserted at the end.\n", data);

3. Searching of Node

// Function to search for a node in the list

int searchNode(struct Node* head, int key) {

struct Node* current = head;

// Traverse through the list to find the node

while (current != NULL) {

if (current->data == key) {

printf("Node with value %d found in the list.\n", key);

return 1; // Node found

current = current->next;

}
printf("Node with value %d not found in the list.\n", key);

return 0; // Node not found

4. Counting the no of nodes in List

// Function to count the number of nodes in the list

int countNodes(struct Node* head) {

int count = 0;

struct Node* current = head;

// Traverse the list and count each node

while (current != NULL) {

count++;

current = current->next;

printf("Number of nodes in the list: %d\n", count);

return count;

5. Display the List

// Function to display the list

void displayList(struct Node* head) {

struct Node* current = head;

// If the list is empty

if (current == NULL) {

printf("The list is empty.\n");

return;

// Traverse the list and print each node's data


printf("Singly Linked List: ");

while (current != NULL) {

printf("%d ", current->data);

current = current->next;

printf("\n");

You might also like