Professional Documents
Culture Documents
Document
Document
] 💔: Binary tree
struct Node {
int data;
Node* left;
Node* right;
class BinaryTree {
private:
Node* root;
public:
BinaryTree() : root(nullptr) {}
int main() {
BinaryTree tree;
tree.insert(5);
tree.insert(3);
tree.insert(7);
tree.insert(1);
tree.insert(4);
return 0;
}
[03-01, 11:08 a.m.] 💔: In order
struct Node {
int data;
Node* left;
Node* right;
Node(int value) {
data = value;
left = nullptr;
right = nullptr;
}
};
inorderTraversal(root->left);
cout << root->data << " ";
inorderTraversal(root->right);
}
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
return 0;
}
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
return 0;
}
struct Node {
int data;
Node* left;
Node* right;
Node(int val) : data(val), left(nullptr), right(nullptr) {}
};
void postorder(Node* root) {
if (root == nullptr) {
return;
}
postorder(root->left);
postorder(root->right);
cout << root->data << " ";
}
int main() {
Node* root = new Node(1);
root->left = new Node(2);
root->right = new Node(3);
root->left->left = new Node(4);
root->left->right = new Node(5);
return 0;
}
class Node {
public:
int *keys;
int t;
Node **C;
int n;
bool leaf;
Node(int _t, bool _leaf);
void insertNonFull(int k);
void splitChild(int i, Node *y);
void traverse();
Node *search(int k);
friend class Tree;
};
class Tree {
public:
Node *root;
int t;
Tree(int _t);
void insert(int k);
void traverse();
Node *search(int k);
};
void Node::traverse() {
int i;
for (i = 0; i < n; i++) {
if (leaf == false)
C[i]->traverse();
cout << " " << keys[i];
}
if (leaf == false)
C[i]->traverse();
}
Node *Node::search(int k) {
int i = 0;
while (i < n && k > keys[i])
i++;
if (keys[i] == k)
return this;
if (leaf == true)
return NULL;
return C[i]->search(k);
}
void Tree::traverse() {
if (root != NULL)
root->traverse();
}
Node *Tree::search(int k) {
return (root == NULL) ? NULL : root->search(k);
}
int main() {
Tree t(3);
t.insert(10
#include <vector>
#include <algorithm>
class MaxHeap {
private:
std::vector<int> heap;
if (largest != index) {
std::swap(heap[index], heap[largest]);
heapify(largest);
}
}
public:
void insert(int value) {
heap.push_back(value);
int index = heap.size() - 1;
int extractMax() {
if (heap.empty()) {
throw std::out_of_range("Heap is empty");
}
int max = heap[0];
heap[0] = heap.back();
#include <list>
using namespace std;
class Graph {
int V;
list<int> *adj;
public:
Graph(int V);
void addEdge(int v, int w);
void printGraph();
};
Graph::Graph(int V) {
this->V = V;
adj = new list<int>[V];
}
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);
g.printGraph();
return 0;
}
#include <list>
class Graph {
int V;
std::list<int> *adj;
public:
Graph(int V);
void addEdge(int v, int w);
void printGraph();
};
Graph::Graph(int V) {
this->V = V;
adj = new std::list<int>[V];
}
void Graph::printGraph() {
for (int i = 0; i < V; ++i) {
std::cout << "Adjacency list of vertex " << i << "\n";
std::cout << "head";
for (int val : adj[i]) {
std::cout << " -> " << val;
}
std::cout << std::endl;
}
}
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(1, 3);
g.printGraph();
return 0;
}
#include <vector>
#include <utility>
class WeightedGraph {
private:
int V;
vector<pair<int, int>> *adj;
public:
WeightedGraph(int V) {
this->V = V;
adj = new vector<pair<int, int>>[V];
}
void printGraph() {
for (int i = 0; i < V; ++i) {
cout << "Vertex " << i << " is connected to:\n";
for (auto& edge : adj[i]) {
cout << edge.first << " with weight " << edge.second << "\n";
}
cout << "\n";
}
}
};
int main() {
WeightedGraph graph(4);
graph.addEdge(0, 1, 5);
graph.addEdge(0, 2, 3);
graph.addEdge(1, 2, 2);
graph.addEdge(1, 3, 8);
graph.addEdge(2, 3, 1);
graph.printGraph();
return 0;
}
#include <list>
class Graph {
int V;
std::list<int> *adj;
public:
Graph(int V);
void addEdge(int v, int w);
void printGraph();
};
Graph::Graph(int V) {
this->V = V;
adj = new std::list<int>[V];
}
void Graph::printGraph() {
for (int i = 0; i < V; ++i) {
std::cout << "Adjacency list of vertex " << i << "\nhead";
for (int x : adj[i])
std::cout << " -> " << x;
std::cout << std::endl;
}
}
int main() {
Graph g(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 3);
g.printGraph();
return 0;
}
[03-01, 11:18 a.m.] 💔: Dijestra graph
#include <vector>
#include <queue>
#include <climits>
while (!pq.empty()) {
int u = pq.top().second;
int d = pq.top().first;
pq.pop();
int main() {
int n = 6; // Number of nodes
vector<vector<pii>> graph(n);
dijkstra(graph, 0);
return 0;
}
#include <vector>
class HashTable {
private:
static const int TABLE_SIZE = 10;
std::vector<std::pair<int, std::string>> table[TABLE_SIZE];
public:
void insert(int key, const std::string& value) {
int index = hashFunction(key);
table[index].push_back(std::make_pair(key, value));
}
int main() {
HashTable ht;
ht.insert(5, "Apple");
ht.insert(15, "Banana");
ht.insert(25, "Cherry");
ht.remove(15);
return 0;
}
class Chain {
public:
Chain(int value) : value(value) {}
void print() {
std::cout << "Current value: " << value << std::endl;
}
private:
int value;
};
int main() {
Chain chain(10);
chain.add(5).subtract(3).add(10).subtract(2).print();
return 0;
}
const int N = 8;
int solution[N];
int main() {
backtracking(0);
return 0;
}
int main() {
int arr[] = {12, 4, 7, 9, 2, 15};
int n = sizeof(arr) / sizeof(arr[0]);
int minVal = findMin(arr, n);
std::cout << "Minimum value in the array: " << minVal << std::endl;
return 0;
}
return maxVal;
}
int main() {
int arr[] = {10, 30, 20, 50, 40};
int size = sizeof(arr) / sizeof(arr[0]);
int maxElement = findMax(arr, size);
std::cout << "The maximum element in the array is: " << maxElement << std::endl;
return 0;
}
int main() {
int arr[] = {64, 25, 12, 22, 11};
int n = sizeof(arr) / sizeof(arr[0]);
selectionSort(arr, n);
std::cout << "Sorted array: ";
for (int i = 0; i < n; i++) {
std::cout << arr[i] << " ";
}
return 0;
}