data structers lab programs
data structers lab programs
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(NULL), right(NULL) {}
};
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
inorder(root);
return 0;
}
Fibonacci (Recursive):
cpp
Copy code
#include <iostream>
using namespace std;
int fibonacci(int n) {
if (n <= 1)
return n;
return fibonacci(n - 1) + fibonacci(n - 2);
}
int main() {
int n = 10;
for (int i = 0; i < n; ++i)
cout << fibonacci(i) << " ";
return 0;
}
2. Iterative Function for Tree Traversal and Fibonacci
Tree Traversal (In-order, Iterative):
cpp
Copy code
#include <iostream>
#include <stack>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(NULL), right(NULL) {}
};
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
inorderIterative(root);
return 0;
}
Fibonacci (Iterative):
cpp
Copy code
#include <iostream>
using namespace std;
int fibonacciIterative(int n) {
if (n <= 1)
return n;
int a = 0, b = 1, c;
for (int i = 2; i <= n; ++i) {
c = a + b;
a = b;
b = c;
}
return b;
}
int main() {
int n = 10;
for (int i = 0; i < n; ++i)
cout << fibonacciIterative(i) << " ";
return 0;
}
3. Merge Sort and Quick Sort
Merge Sort:
cpp
Copy code
#include <iostream>
using namespace std;
int i = 0, j = 0, k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) arr[k++] = L[i++];
else arr[k++] = R[j++];
}
int main() {
int arr[] = {12, 11, 13, 5, 6, 7};
int arr_size = sizeof(arr) / sizeof(arr[0]);
int main() {
int arr[] = {10, 7, 8, 9, 1, 5};
int n = sizeof(arr) / sizeof(arr[0]);
quickSort(arr, 0, n - 1);
for (int i = 0; i < n; i++)
cout << arr[i] << " ";
return 0;
}
4. Binary Search Tree Implementation:
cpp
Copy code
#include <iostream>
using namespace std;
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(NULL), right(NULL) {}
};
int main() {
Node* root = NULL;
root = insert(root, 50);
insert(root, 30);
insert(root, 20);
insert(root, 40);
insert(root, 70);
insert(root, 60);
insert(root, 80);
inorder(root);
return 0;
}
For the remaining algorithms (Red-Black Tree, Heap, Fibonacci Heap,
Graph Traversals, Spanning Tree, Shortest Path, Matrix Chain
Multiplication, Activity Selection, and Huffman Coding), I can provide
similar C++ implementations. Let me know if you'd like any specific code
immediately, or if you'd like to prioritize any specific algorithm first!
4o
You said:
give me all the other also
ChatGPT said:
ChatGPT
Here are the C++ implementations for the remaining algorithms.
struct Node {
int data;
bool color;
Node *left, *right, *parent;
class RedBlackTree {
private:
Node* root;
if (pt->right != NULL)
pt->right->parent = pt;
pt_right->parent = pt->parent;
if (pt->parent == NULL)
root = pt_right;
else
pt->parent->right = pt_right;
pt_right->left = pt;
pt->parent = pt_right;
}
pt->left = pt_left->right;
if (pt->left != NULL)
pt->left->parent = pt;
pt_left->parent = pt->parent;
if (pt->parent == NULL)
root = pt_left;
else
pt->parent->right = pt_left;
pt_left->right = pt;
pt->parent = pt_left;
}
if (parent_pt == grand_parent_pt->left) {
Node* uncle_pt = grand_parent_pt->right;
rotateRight(root, grand_parent_pt);
swap(parent_pt->color, grand_parent_pt->color);
pt = parent_pt;
}
} else {
Node* uncle_pt = grand_parent_pt->left;
rotateLeft(root, grand_parent_pt);
swap(parent_pt->color, grand_parent_pt->color);
pt = parent_pt;
}
}
}
root->color = BLACK;
}
public:
RedBlackTree() { root = NULL; }
return root;
}
inorderHelper(root->left);
cout << root->data << " ";
inorderHelper(root->right);
}
int main() {
RedBlackTree tree;
tree.insert(7);
tree.insert(6);
tree.insert(5);
tree.insert(4);
tree.insert(3);
tree.insert(2);
tree.insert(1);
cout << "Inorder Traversal of Created Tree\n";
tree.inorder();
return 0;
}
class MaxHeap {
vector<int> heap;
if (largest != index) {
swap(heap[index], heap[largest]);
heapifyDown(largest);
}
}
public:
int parent(int i) { return (i - 1) / 2; }
int left(int i) { return 2 * i + 1; }
int right(int i) { return 2 * i + 2; }
int size() { return heap.size(); }
void insert(int key) {
heap.push_back(key);
heapifyUp(size() - 1);
}
void removeMax() {
if (size() == 0)
return;
int getMax() {
if (size() > 0)
return heap[0];
return -1;
}
void display() {
for (int i : heap)
cout << i << " ";
cout << endl;
}
};
int main() {
MaxHeap maxHeap;
maxHeap.insert(10);
maxHeap.insert(20);
maxHeap.insert(15);
maxHeap.insert(30);
maxHeap.insert(40);
maxHeap.removeMax();
cout << "After removing max: ";
maxHeap.display();
return 0;
}
class Graph {
int V;
list<int>* adj;
public:
Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
void BFS(int s) {
bool* visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
list<int> queue;
visited[s] = true;
queue.push_back(s);
while (!queue.empty()) {
s = queue.front();
cout << s << " ";
queue.pop_front();
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Breadth First Traversal (starting from vertex 2): ";
g.BFS(2);
return 0;
}
DFS:
cpp
Copy code
#include <iostream>
#include <list>
using namespace std;
class Graph {
int V;
list<int>* adj;
void DFS(int v) {
bool* visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
DFSUtil(v, visited);
}
};
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
cout << "Depth First Traversal (starting from vertex 2): ";
g.DFS(2);
return 0;
}
#define V 5
return min_index;
}
key[0] = 0;
parent[0] = -1;
printMST(parent, graph);
}
int main() {
int graph[V][V] = {{0, 2, 0, 6, 0},
{2, 0, 3, 8, 5},
{0, 3, 0, 0, 7},
{6, 8, 0, 0, 9},
{0, 5, 7, 9, 0}};
primMST(graph);
return 0;
}
10. Shortest Path Algorithms (Dijkstra's and Bellman-Ford)
Dijkstra's Algorithm:
cpp
Copy code
#include <iostream>
#include <climits>
using namespace std;
#define V 9
return min_index;
}
dist[src] = 0;
printSolution(dist);
}
int main() {
int graph[V][V] = {{0, 4, 0, 0, 0, 0, 0, 8, 0},
{4, 0, 8, 0, 0, 0, 0, 11, 0},
{0, 8, 0, 7, 0, 4, 0, 0, 2},
{0, 0, 7, 0, 9, 14, 0, 0, 0},
{0, 0, 0, 9, 0, 10, 0, 0, 0},
{0, 0, 4, 14, 10, 0, 2, 0, 0},
{0, 0, 0, 0, 0, 2, 0, 1, 6},
{8, 11, 0, 0, 0, 0, 1, 0, 7},
{0, 0, 2, 0, 0, 0, 6, 7, 0}};
dijkstra(graph, 0);
return 0;
}
Bellman-Ford Algorithm:
cpp
Copy code
#include <iostream>
#include <vector>
using namespace std;
struct Edge {
int src, dest, weight;
};
int main() {
int V = 5;
int E = 8;
vector<Edge> edges(E);
edges[0] = {0, 1, -1};
edges[1] = {0, 2, 4};
edges[2] = {1, 2, 3};
edges[3] = {1, 3, 2};
edges[4] = {1, 4, 2};
edges[5] = {3, 2, 5};
edges[6] = {3, 1, 1};
edges[7] = {4, 3, -3};
bellmanFord(edges, V, E, 0);
return 0;
}
int main() {
int arr[] = {1, 2, 3, 4};
int n = sizeof(arr) / sizeof(arr[0]);
struct Activity {
int start, finish;
};
int main() {
vector<Activity> activities = {{5, 9}, {1, 2}, {3, 4}, {0, 6}, {5, 7}, {8,
9}};
activitySelection(activities);
return 0;
}
Huffman Coding:
Huffman Coding implementation requires significant complexity with
priority queues and tree management. For simplicity, here is a basic
version:
cpp
Copy code
#include <iostream>
#include <queue>
#include <vector>
#include <map>
using namespace std;
struct Node {
char ch;
int freq;
Node *left, *right;
struct compare {
bool operator()(Node* l, Node* r) {
return (l->freq > r->freq);
}
};
if (root->ch != '$')
cout << root->ch << ": " << str << "\n";
while (minHeap.size() != 1) {
Node *left = minHeap.top(); minHeap.pop();
Node *right = minHeap.top(); minHeap.pop();
printCodes(minHeap.top(), "");
}
int main() {
map<char, int> freq = {{'a', 5}, {'b', 9}, {'c', 12}, {'d', 13}, {'e', 16},
{'f', 45}};
huffmanCoding(freq);
return 0;
}