.. include:: global.rst *************************** Code Examples *************************** Binary Search Example ===================== .. code-block:: cpp :linenos: #include #include using namespace std; int binarySearch(const vector& arr, int target) { int left = 0; int right = arr.size() - 1; while (left <= right) { int mid = left + (right - left) / 2; if (arr[mid] == target) return mid; else if (arr[mid] < target) left = mid + 1; else right = mid - 1; } return -1; } int main() { vector sortedArray = {2, 4, 7, 10, 23, 38, 45, 59}; int target = 23; int result = binarySearch(sortedArray, target); if (result != -1) cout << "Element found at index " << result << endl; else cout << "Element not found in the array." << endl; return 0; } Cloned stack example ==================== .. code-block:: cpp :linenos: #include using namespace std; // Node structure for the linked list struct Node { int data; Node* next; Node(int val) : data(val), next(nullptr) {} }; // Stack implemented using a linked list class LinkedStack { private: Node* head; public: LinkedStack() : head(nullptr) {} // Push an element onto the stack void push(int val) { Node* newNode = new Node(val); newNode->next = head; head = newNode; } // Pop the top element void pop() { if (head) { Node* temp = head; head = head->next; delete temp; } } // Get the top element int top() const { if (head) return head->data; throw runtime_error("Stack is empty"); } // Check if the stack is empty bool isEmpty() const { return head == nullptr; } // Clone the stack and return a new LinkedStack LinkedStack clone() const { LinkedStack reversed, cloned; Node* current = head; // Step 1: Reverse the original stack into 'reversed' while (current) { reversed.push(current->data); current = current->next; } // Step 2: Push elements from 'reversed' into 'cloned' to preserve order current = reversed.head; while (current) { cloned.push(current->data); current = current->next; } return cloned; } // Display stack contents (for debugging) void print() const { Node* current = head; while (current) { cout << current->data << " "; current = current->next; } cout << endl; } // Destructor to clean up memory ~LinkedStack() { while (head) { pop(); } } }; int main() { LinkedStack original; original.push(10); original.push(20); original.push(30); cout << "Original stack: "; original.print(); LinkedStack cloned = original.clone(); cout << "Cloned stack: "; cloned.print(); return 0; } Binary tree example ==================== .. code-block:: cpp :linenos: #include // Definition for a binary tree node. struct TreeNode { int val; TreeNode* left; TreeNode* right; // Constructor to initialize a new node TreeNode(int x) : val(x), left(nullptr), right(nullptr) {} }; // Function to perform a deep clone of a binary tree TreeNode* cloneTree(TreeNode* original) { // Base case: if the node is null, return null if (original == nullptr) { return nullptr; } // Create a new node and copy the data TreeNode* clonedNode = new TreeNode(original->val); // Recursively clone the left and right subtrees clonedNode->left = cloneTree(original->left); clonedNode->right = cloneTree(original->right); // Return the new node return clonedNode; }