Daa Lab Fast Learner Nemesis
Daa Lab Fast Learner Nemesis
Aim:
Given the pointer to the head node of a linked list, change the next pointers of
the nodes so that their order is reversed. The head pointer given may be null
meaning that the initial list is empty.
Code:
#include <bits/stdc++.h>
using namespace std;
struct SinglyLinkedListNode {
int data;
SinglyLinkedListNode* next;
SinglyLinkedListNode(int x) : data(x), next(nullptr) {}
};
SinglyLinkedListNode* insertAtEnd(SinglyLinkedListNode* head, int data) {
SinglyLinkedListNode* current = head;
if (!head) return new SinglyLinkedListNode(data);
while (current->next) current = current->next;
current->next = new SinglyLinkedListNode(data);
return head;
}
void printList(SinglyLinkedListNode* head) {
while (head) {
cout << head->data << (head->next ? " " : "\n");
head = head->next;
}
}
SinglyLinkedListNode* reverse(SinglyLinkedListNode* head) {
SinglyLinkedListNode *prev = nullptr, *current = head, *nextNode = nullptr;
while (current) {
nextNode = current->next;
current->next = prev;
prev = current;
current = nextNode;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
}
return prev;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t, n, val;
cin >> t;
while (t--) {
cin >> n;
SinglyLinkedListNode* head = nullptr;
while (n--) {
cin >> val;
head = insertAtEnd(head, val);
}
SinglyLinkedListNode* reversedHead = reverse(head);
printList(reversedHead);
while (reversedHead) {
SinglyLinkedListNode* temp = reversedHead;
reversedHead = reversedHead->next;
delete temp;
}
}
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
Ques 2:
Write the postOrder function. It received 1 parameter: a pointer to the root of a binary
tree. It must print the values in the tree's postorder traversal as a single line of space-
separated values.
Code:
#include <bits/stdc++.h>
using namespace std;
class Graph {
public:
unordered_map<int, vector<int>> adjList;
int t;
cin >> t;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
while (t--) {
int n, m;
cin >> n >> m; // n is the number of nodes, m is the number of edges
Graph g;
for (int i = 0; i < m; i++) {
int u, v;
cin >> u >> v;
g.addEdge(u, v); // Add directed edge u -> v
}
int startNode;
cin >> startNode;
vector<int> postOrder = g.getPostOrder(startNode, n);
for (int node : postOrder) {
cout << node << " ";
}
cout << "\n";
}
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
3. You are given a pointer to the root of a binary search tree and values to be inserted into the tree.
Insert the values into their appropriate position in the binary search tree and return the root of the
updated binary tree.
CODE:-
#include <bits/stdc++.h>
using namespace std;
struct TreeNode {
int data;
TreeNode* left;
TreeNode* right;
TreeNode(int x) : data(x), left(nullptr), right(nullptr) {}
};
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t, n, val;
cin >> t;
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
while(t--){
cin >> n;
vector<int> arr(n);
for(auto &x: arr) cin >> x;
TreeNode* root = nullptr;
for(auto x: arr) root = insert(root, x);
inOrderTraversal(root);
cout << "\n";
// Memory cleanup
if(root){
queue<TreeNode*> q; q.push(root);
while(!q.empty()){
TreeNode* curr = q.front(); q.pop();
if(curr->left) q.push(curr->left);
if(curr->right) q.push(curr->right);
delete curr;
}
}
}
return 0;
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
4. Huffman Coding assigns variable length codewords to fixed length input characters based on their
frequencies. More frequent characters are assigned shorter codewords and less frequent characters are assigned
longer codewords. All edges along the path to a character contain a code digit. If they are on the left side of the
tree, they will be a 0 (zero). If on the right, they'll be a 1 (one). Only the leaves will contain a letter and its
frequency count. All other nodes will contain a null instead of a character, and the count of the frequency of all
of it and its descendant characters.
For instance, consider the string ABRACADABRA. There are a total of 11 characters in the string. This
number should match the count in the ultimately determined root of the tree. Our frequencies are A= 5, B=2,
R=2,C=1 and D-1. The two smallest frequencies are for C and D, both equal to 1 , so we'll create a tree with
them. The root node will contain the sum of the counts of its descendants, in this case 1+1 =2 . The left node
will be the first character encountered, C , and the right will contain D . Next we have 3 items with a character
count of 2 : the tree we just created, the character B and the character R. The tree came first, so it will go on
the left of our new root node. B will go on the right. Repeat until the tree is complete, then fill in the 1 's and 0
's for the edges.
Code:-
#include<bits/stdc++.h>
using namespace std;
struct TreeNode{
char data;
int freq;
TreeNode* left;
TreeNode* right;
TreeNode(char x, int f): data(x), freq(f), left(NULL), right(NULL){}
};
struct Compare{
bool operator()(TreeNode* a, TreeNode* b){
if(a->freq == b->freq) return a->data > b->data;
return a->freq > b->freq;
}
};
return decoded;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin>>t;
while(t--){
string s; cin>>s;
map<char, int> freq; for(char c:s) freq[c]++;
TreeNode* root = buildHuffmanTree(freq);
map<char, string> codes; getCodes(root, "", codes);
string encoded = encodeString(s, codes);
cout<<decode_huff(root, encoded)<<"\n";
// Cleanup
queue<TreeNode*> q; if(root) q.push(root);
while(!q.empty()){
TreeNode* curr = q.front(); q.pop();
if(curr->left) q.push(curr->left);
if(curr->right) q.push(curr->right);
delete curr;
}
}
}
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
5. we're covering a divide-and-conquer algorithm called Quicksort (also known as Partition Sort).
This challenge is a modified version of the algorithm that only addresses partitioning. It is
implemented as follows:
Step 1: Divide
Choose some pivot element, p, and partition your unsorted array, arr, into three smaller arrays: ,left
,right and equal, where each element in left < p, each element in right > p, and each element in equal
=p.
CODE:
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin>>t;
while(t--){
int n; cin>>n;
vector<int> arr(n); for(auto &x: arr) cin>>x;
vector<int> sorted = quickSort(arr);
DEPARTMENT OF
COMPUTER SCIENCE & ENGINEERING
6. - Given an amount and the denominations of coins available, determine how many ways change
can be made for amount. There is a limitless supply of each coin type.
CODE:
#include<bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t; cin>>t;
while(t--){
int n,m; cin>>n>>m;
vector<int> c(m); for(auto &x: c) cin>>x;
cout<<getWays(n,c)<<"\n";
}
}