Code Examples

Binary Search Example

 1#include <iostream>
 2#include <vector>
 3using namespace std;
 4
 5int binarySearch(const vector<int>& arr, int target) {
 6    int left = 0;
 7    int right = arr.size() - 1;
 8
 9    while (left <= right) {
10        int mid = left + (right - left) / 2;
11
12        if (arr[mid] == target)
13            return mid;
14        else if (arr[mid] < target)
15            left = mid + 1;
16        else
17            right = mid - 1;
18    }
19
20    return -1;
21}
22
23int main() {
24    vector<int> sortedArray = {2, 4, 7, 10, 23, 38, 45, 59};
25    int target = 23;
26
27    int result = binarySearch(sortedArray, target);
28
29    if (result != -1)
30        cout << "Element found at index " << result << endl;
31    else
32        cout << "Element not found in the array." << endl;
33
34    return 0;
35}

Cloned stack example

  1 #include <iostream>
  2
  3 using namespace std;
  4
  5 // Node structure for the linked list
  6 struct Node {
  7 int data;
  8 Node* next;
  9
 10 Node(int val) : data(val), next(nullptr) {}
 11 };
 12
 13 // Stack implemented using a linked list
 14 class LinkedStack {
 15 private:
 16      Node* head;
 17
 18 public:
 19 LinkedStack() : head(nullptr) {}
 20
 21 // Push an element onto the stack
 22 void push(int val) {
 23      Node* newNode = new Node(val);
 24      newNode->next = head;
 25      head = newNode;
 26 }
 27
 28 // Pop the top element
 29 void pop() {
 30      if (head) {
 31            Node* temp = head;
 32            head = head->next;
 33            delete temp;
 34      }
 35 }
 36
 37 // Get the top element
 38 int top() const {
 39      if (head) return head->data;
 40      throw runtime_error("Stack is empty");
 41 }
 42
 43 // Check if the stack is empty
 44 bool isEmpty() const {
 45      return head == nullptr;
 46 }
 47
 48 // Clone the stack and return a new LinkedStack
 49 LinkedStack clone() const {
 50      LinkedStack reversed, cloned;
 51      Node* current = head;
 52
 53      // Step 1: Reverse the original stack into 'reversed'
 54      while (current) {
 55            reversed.push(current->data);
 56            current = current->next;
 57      }
 58
 59      // Step 2: Push elements from 'reversed' into 'cloned' to preserve order
 60      current = reversed.head;
 61      while (current) {
 62            cloned.push(current->data);
 63            current = current->next;
 64      }
 65
 66      return cloned;
 67 }
 68
 69 // Display stack contents (for debugging)
 70 void print() const {
 71      Node* current = head;
 72      while (current) {
 73            cout << current->data << " ";
 74            current = current->next;
 75      }
 76      cout << endl;
 77 }
 78
 79 // Destructor to clean up memory
 80 ~LinkedStack() {
 81      while (head) {
 82            pop();
 83      }
 84 }
 85    };
 86 int main() {
 87 LinkedStack original;
 88 original.push(10);
 89 original.push(20);
 90 original.push(30);
 91
 92 cout << "Original stack: ";
 93 original.print();
 94
 95 LinkedStack cloned = original.clone();
 96
 97 cout << "Cloned stack: ";
 98 cloned.print();
 99
100 return 0;
101 }

Binary tree example

 1#include <iostream>
 2
 3// Definition for a binary tree node.
 4struct TreeNode {
 5    int val;
 6    TreeNode* left;
 7    TreeNode* right;
 8
 9    // Constructor to initialize a new node
10    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
11};
12
13// Function to perform a deep clone of a binary tree
14TreeNode* cloneTree(TreeNode* original) {
15    // Base case: if the node is null, return null
16    if (original == nullptr) {
17        return nullptr;
18    }
19
20    // Create a new node and copy the data
21    TreeNode* clonedNode = new TreeNode(original->val);
22
23    // Recursively clone the left and right subtrees
24    clonedNode->left = cloneTree(original->left);
25    clonedNode->right = cloneTree(original->right);
26
27    // Return the new node
28    return clonedNode;
29}