Data Structure Updated Corections (2) (1)
Data Structure Updated Corections (2) (1)
An abstract data type (ADT) is a model for data types where only
behavior is defined, not implementation.
b) Define Tree.
Bubble sort, Insertion sort, Selection sort, Merge sort, Quick sort, Heap
sort.
A binary tree where every level, except possibly the last, is completely
filled and all nodes are as far left as possible.
Each node contains two parts: data and a pointer to the next node. It
only allows traversal in one direction.
Each node contains three parts: data, a pointer to the next node, and a
pointer to the previous node. It allows traversal in both forward and
backward directions.
In this type, the last node points back to the first node, forming a circle.
It can be singly or doubly circular.
Similar to a doubly linked list, but the last node points back to the first
and vice versa, forming a bidirectional circular list.
3
10
/
5
/
2
Becomes:
5
/\
2 10
10
/
5
\
4
Becomes:
7
/\
5 10
10
\
15
\
20
Becomes:
15
/ \
10 20
10
\
15
/
5
13
Becomes:
13
/ \
10 15
10 5
/ --> /\
5 2 10
/
2
10 10 7
/ --> / --> /\
5 7 5 10
\ /
7 5
6
A binary tree where for every node, height difference of left and right
subtrees is at most one.
Insertion sort is a simple sorting algorithm that builds the final sorted
array one item at a time.
Example:
Input: [5, 2, 4, 6, 1, 3]
Sorted: [1, 2, 3, 4, 5, 6]
Algorithm:
1. Iterate from the second element to the end.
2. Compare current element to its predecessor.
3. If smaller, move the predecessor ahead.
4. Insert the current element at the correct position.
Operations:
1. Enqueue: Add an element to the rear.
2. Dequeue: Remove an element from the front.
3. Front: Get the front element.
4. Rear: Get the last element.
5. isEmpty: Check if queue is empty.
Adjacency List:
An array of lists is used. Size of array is equal to number of vertices.
Each entry list represents a vertex and contains all adjacent vertices.
Example:
Graph:
0→1
0→4
1→2
1→3
Adjacency List:
0: 1 → 4
1: 2 → 3
Example:
Infix: A + B * C
Postfix: A B C * +
newNode->data = data;
if (pos == 1) {
newNode->next = head;
8
return newNode;
}
struct Node* temp = head;
for (int i = 0; i < pos - 2 && temp != NULL; i++)
temp = temp->next;
newNode->next = temp->next;
temp->next = newNode;
return head;
}
Q3)
Answer: A circular singly linked list is a linear data structure where the
last node points back to the first node. Unlike a singly linked list, it does
not end at NULL. This structure is useful when we want to continuously
loop through data.
Operations:
1. Creation:
Set the next pointer of each node to point to the next node.
Set the next pointer of the last node to point to the first node (head).
2. Display:
Algorithm:
Function createCircularList(n):
For i = 1 to n:
Create newNode
If head is NULL:
head = newNode
temp = newNode
Else:
temp.next = newNode
temp = newNode
End For
Return head
Function displayCircularList(head):
Do:
Print temp.data
temp = temp.next
If visited[adjacent_vertex] == false:
Steps:
2. Mark it as visited.
Steps:
2. If inserting at position 1:
3. Else:
Algorithm:
If pos == 1:
newNode.next = head
head = newNode
Else:
temp = head
For i = 1 to pos-2:
temp = temp.next
newNode.next = temp.next
temp.next = newNode
#include <stdio.h>
12
#include <string.h>
char stack[MAX];
int top = -1;
void push(char c) {
stack[++top] = c;
}
char pop() {
return stack[top--];
}
int main() {
char str[MAX], rev[MAX];
printf("Enter a string: ");
gets(str);
if (strcmp(str, rev) == 0)
printf("Palindrome\n");
else
printf("Not a Palindrome\n");
return 0;
}
13
b) Construct Binary search tree of following data: 15, 30, 20, 5, 10, 2, 7
c) Write a function to search the element from the array using binary
search.
C Program:
#include <stdio.h>
int main() {
int arr[] = {2, 5, 7, 10, 15, 20, 30};
int x = 10;
int result = binarySearch(arr, 0, 6, x);
if (result != -1)
printf("Element found at index %d", result);
else
printf("Element not found");
return 0;
}
e) Write a ‘C’ Program to count the number of nodes from singly linked
list.
C Program:
#include <stdio.h>
#include <stdlib.h>
struct Node {
int data;
struct Node* next;
};
count++;
head = head->next;
}
return count;
}
Q5
i) AB-CD*/
A * B + C / D = 2 * 10 + 6 / 2 = 20 + 3 = 23
ii) ABC*+
AB-CD/* = (2 * 10 - 6 / 2) = (20 - 3) = 17
iii) Pendant vertex: A vertex with only one edge (degree = 1).