Advanced Data Structures
Advanced Data Structures
Sample Programs:
1. Write a main function to create objects of DISTANCE Class Input two
distances and output the sum
#include <iostream>
class DISTANCE {
public:
int feet;
float inches;
void input() {
std::cout << "Enter feet: ";
std::cin >> feet;
std::cout << "Enter inches: ";
std::cin >> inches;
}
int main() {
DISTANCE d1, d2, d3;
return 0;
}
#include <iostream>
class MyClass {
public:
void display() {
};
int main() {
MyClass obj;
ptr->display();
return 0;
3. Construct B-Tree an order of 5 with a set of 100 random elements stored in array.
Implement searching, insertion and deletion operations.
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#define ORDER 5
class BTreeNode {
public:
int *keys;
int t;
BTreeNode **C;
int n;
bool leaf;
void traverse();
};
class BTree {
public:
BTreeNode *root;
int t;
BTree(int _t) {
root = nullptr;
t = _t;
void traverse() {
BTreeNode* search(int k) {
};
t = t1;
leaf = leaf1;
keys = new int[2*t-1];
n = 0;
void BTreeNode::traverse() {
int i;
if (leaf == false)
C[i]->traverse();
if (leaf == false)
C[i]->traverse();
BTreeNode *BTreeNode::search(int k) {
int i = 0;
i++;
if (keys[i] == k)
return this;
if (leaf == true)
return nullptr;
return C[i]->search(k);
void BTree::insert(int k) {
if (root == nullptr) {
root->keys[0] = k;
root->n = 1;
} else {
if (root->n == 2*t-1) {
s->C[0] = root;
s->splitChild(0, root);
int i = 0;
if (s->keys[0] < k)
i++;
s->C[i]->insertNonFull(k);
root = s;
} else
root->insertNonFull(k);
void BTreeNode::insertNonFull(int k) {
int i = n-1;
if (leaf == true) {
keys[i+1] = keys[i];
i--;
keys[i+1] = k;
n = n+1;
} else {
i--;
if (C[i+1]->n == 2*t-1) {
splitChild(i+1, C[i+1]);
if (keys[i+1] < k)
i++;
}
C[i+1]->insertNonFull(k);
z->n = t - 1;
z->keys[j] = y->keys[j+t];
if (y->leaf == false) {
z->C[j] = y->C[j+t];
y->n = t - 1;
C[j+1] = C[j];
C[i+1] = z;
keys[j+1] = keys[j];
keys[i] = y->keys[t-1];
n = n + 1;
int main() {
srand(time(0));
BTree t(ORDER);
std::vector<int> arr(100);
t.insert(arr[i]);
return 0;
4. Construct Min and Max Heap using arrays, delete any element and display the content
of the Heap.
#include <iostream>
#include <vector>
#include <algorithm>
int largest = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (isMaxHeap) {
largest = l;
largest = r;
} else {
largest = l;
largest = r;
if (largest != i) {
std::swap(arr[i], arr[largest]);
int n = arr.size();
if (it != arr.end()) {
std::swap(arr[index], arr.back());
arr.pop_back();
int main() {
buildHeap(minHeap, false);
buildHeap(maxHeap, true);
displayHeap(minHeap);
displayHeap(maxHeap);
int num;
displayHeap(minHeap);
displayHeap(maxHeap);
return 0;
5. Implement BFT and DFT for given graph, when graph is represented by
a) Adjacency Matrix b) Adjacency Lists
Adjacency Matrix
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
std::queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
q.push(i);
visited[i] = true;
std::stack<int> s;
s.push(start);
visited[start] = true;
while (!s.empty()) {
int v = s.top();
s.pop();
s.push(i);
visited[i] = true;
int main() {
std::vector<std::vector<int>> adjMatrix = {
{0, 1, 1, 0, 0},
{1, 0, 0, 1, 1},
{1, 0, 0, 0, 1},
{0, 1, 0, 0, 1},
{0, 1, 1, 1, 0}
};
BFTMatrix(adjMatrix, 0);
DFTMatrix(adjMatrix, 0);
return 0;
#include <iostream>
#include <vector>
#include <queue>
#include <stack>
std::queue<int> q;
q.push(start);
visited[start] = true;
while (!q.empty()) {
int v = q.front();
q.pop();
if (!visited[u]) {
q.push(u);
visited[u] = true;
}
std::stack<int> s;
s.push(start);
visited[start] = true;
while (!s.empty()) {
int v = s.top();
s.pop();
if (!visited[u]) {
s.push(u);
visited[u] = true;
int main() {
std::vector<std::vector<int>> adjList = {
{1, 2},
{0, 3, 4},
{0, 4},
{1, 4},
{1, 2, 3}
};
BFTList(adjList, 0);
DFTList(adjList, 0);
return 0;
#include <list>
#include <stack>
#include <vector>
class Graph {
int V;
std::list<int> *adj;
void BCCUtil(int u, int disc[], int low[], std::stack<int> *st, int parent[]);
public:
Graph(int V);
void BCC();
};
Graph::Graph(int V) {
this->V = V;
adj[v].push_back(w);
adj[w].push_back(v);
}
void Graph::BCCUtil(int u, int disc[], int low[], std::stack<int> *st, int parent[]) {
int children = 0;
if (disc[v] == -1) {
children++;
parent[v] = u;
st->push(u * V + v);
if ((disc[u] == 1 && children > 1) || (disc[u] > 1 && low[v] >= disc[u])) {
while (st->top() != u * V + v) {
std::cout << st->top() / V << "--" << st->top() % V << " ";
st->pop();
st->pop();
st->push(u * V + v);
void Graph::BCC() {
parent[i] = -1;
if (disc[i] == -1) {
while (!st->empty()) {
std::cout << st->top() / V << "--" << st->top() % V << " ";
st->pop();
int main() {
Graph g(5);
g.addEdge(1, 0);
g.addEdge(0, 2);
g.addEdge(2, 1);
g.addEdge(0, 3);
g.addEdge(3, 4);
g.BCC();
return 0;
}
7. Implement Quick sort and Merge sort and observe the execution time for various
input sizes (Average, Worst and Best cases).
#include <iostream>
#include <vector>
#include <chrono>
#include <algorithm>
#include <cstdlib>
quickSort(arr, pi + 1, high);
i++;
std::swap(arr[i], arr[j]);
return (i + 1);
if (l < r) {
int m = l + (r - l) / 2;
mergeSort(arr, l, m);
mergeSort(arr, m + 1, r);
merge(arr, l, m, r);
int n1 = m - l + 1;
int n2 = r - m;
int i = 0, j = 0, k = l;
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
k++;
arr[k] = L[i];
i++;
k++;
}
while (j < n2) {
arr[k] = R[j];
j++;
k++;
std::cout << "Execution time: " << diff.count() << " s\n";
int main() {
std::vector<int> arr1(1000);
std::vector<int> arr2(1000);
arr2[i] = arr1[i];
measureExecutionTime(quickSort, arr1);
measureExecutionTime(mergeSort, arr2);
return 0;
}
8. Compare the performance of Single Source Shortest Paths using Greedy method when
the graph is represented by adjacency matrix and adjacency lists.
#include <iostream>
#include <vector>
#include <limits.h>
#include <set>
#define V 9
return min_index;
dist[src] = 0;
sptSet[u] = true;
printSolution(dist);
setds.insert(std::make_pair(0, src));
dist[src] = 0;
while (!setds.empty()) {
setds.erase(setds.begin());
int u = tmp.second;
int v = (*i).first;
if (dist[v] != INT_MAX)
setds.erase(setds.find(std::make_pair(dist[v], v)));
setds.insert(std::make_pair(dist[v], v));
printSolution(dist);
int main() {
std::vector<std::vector<int>> graph = {
{0, 4, 0, 0, 0, 0, 0, 8, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{0, 0, 2, 0, 0, 0, 6, 7, 0}
};
adjList[0].emplace_back(1, 4);
adjList[0].emplace_back(7, 8);
adjList[1].emplace_back(0, 4);
adjList[1].emplace_back(2, 8);
adjList[1].emplace_back(7, 11);
adjList[2].emplace_back(1, 8);
adjList[2].emplace_back(3, 7);
adjList[2].emplace_back(5, 4);
adjList[2].emplace_back(8, 2);
adjList[3].emplace_back(2, 7);
adjList[3].emplace_back(4, 9);
adjList[3].emplace_back(5, 14);
adjList[4].emplace_back(3, 9);
adjList[4].emplace_back(5, 10);
adjList[5].emplace_back(2, 4);
adjList[5].emplace_back(3, 14);
adjList[5].emplace_back(4, 10);
adjList[5].emplace_back(6, 2);
adjList[6].emplace_back(5, 2);
adjList[6].emplace_back(7, 1);
adjList[6].emplace_back(8, 6);
adjList[7].emplace_back(0, 8);
adjList[7].emplace_back(1, 11);
adjList[7].emplace_back(6, 1);
adjList[7].emplace_back(8, 7);
adjList[8].emplace_back(2, 2);
adjList[8].emplace_back(6, 6);
adjList[8].emplace_back(7, 7);
dijkstraMatrix(graph, 0);
dijkstraList(adjList, 0);
return 0;
#include <iostream>
#include <vector>
#include <algorithm>
struct Job {
char id;
int deadline;
int profit;
};
bool slot[n];
slot[i] = false;
if (slot[j] == false) {
result[j] = i;
slot[j] = true;
break;
if (slot[i])
int main() {
printJobScheduling(arr, arr.size());
return 0;
10. Write a program to solve 0/1 Knapsack problem Using Dynamic Programming.
#include <iostream>
#include <vector>
#include <algorithm>
if (i == 0 || w == 0) {
dp[i][w] = 0;
} else {
return dp[n][W];
int main() {
std::cin >> n;
std::cin >> W;
std::cout << "Maximum value in knapsack: " << knapSack(W, wt, val, n) << std::endl;
return 0;
#include <iostream>
#include <vector>
if (board[row][i])
return false;
if (board[i][j])
return false;
if (board[i][j])
return false;
return true;
if (col >= n)
return true;
// Consider this column and try placing this queen in all rows one by one
board[i][col] = 1;
return true;
board[i][col] = 0;
// If the queen cannot be placed in any row in this column, return false
return false;
bool solveNQ(int n) {
return false;
printSolution(board);
return true;
int main() {
int n;
std::cin >> n;
solveNQ(n);
return 0;
#include <iostream>
#include <vector>
#include <algorithm>
struct Item {
int weight;
int value;
};
// Base case: if we've considered all items or the current weight exceeds the limit
if (currWeight <= W) {
return;
int maxValue = 0;
knapsackBacktrack(items, W, 0, 0, 0, maxValue);
return maxValue;
int main() {
std::cin >> n;
std::cin >> W;
std::vector<Item> items(n);
std::cout << "Maximum value in knapsack: " << knapsack(items, W) << std::endl;
return 0;