diff --git a/Binary_Search.cpp b/Binary_Search.cpp index 088c87d..e9a68aa 100644 --- a/Binary_Search.cpp +++ b/Binary_Search.cpp @@ -1,46 +1,57 @@ #include +#include // For std::sort using namespace std; -int lin(int arr[],int n,int key){ - int low=0; - int high=n-1; - int num; - - while(low<=high) - { - num=(low+high)/2; - if(key==arr[num]) - return num; - else if(keynum){ - else{ - low=num+1; + +int lin(int arr[], int n, int key) { + int low = 0; + int high = n - 1; + + while (low <= high) { + int mid = (low + high) / 2; + if (key == arr[mid]) { + return mid; + } else if (key < arr[mid]) { + high = mid - 1; + } else { + low = mid + 1; } } - return -1; + return -1; // Key not found } -int main() -{ - int key,n,i; - int arr[n]; - cout<<"enter range "; - cin>>n; - for(i=0;i>arr[i]; +int main() { + int n; + + cout << "Enter the number of elements: "; + cin >> n; + + if (n <= 0) { + cout << "Array size must be positive." << endl; + return 1; // Exit if invalid size + } + + int* arr = new int[n]; // Dynamically allocate array + + for (int i = 0; i < n; i++) { + cout << "Enter element " << i + 1 << ": "; + cin >> arr[i]; + } + + cout << "Enter the key to search: "; + int key; + cin >> key; + + // Sort the array before performing binary search + sort(arr, arr + n); + + int index = lin(arr, n, key); + if (index == -1) { + cout << "The number you entered is not in the array." << endl; + } else { + cout << "The number is found at index: " << index << endl; } - //int num - cout<<"enter the key "; - //cin>>key; - int data=lin(arr,n,5); - if(data==-1) - cout<<"the number you entered is out of scope"; - else - cout<<"the number is placed at "< +#include +#include using namespace std; -class Node{ - - public: - +class Node { +public: int data; - Node *left; - Node *right; - - - public: - - Node(int x){ - - data = x; - - left = NULL; - - right = NULL; - - } - + Node(int x) : data(x), left(nullptr), right(nullptr) {} }; -Node *insert_BST(Node *tree, int val) - -{ - - if(tree == NULL){ - - cout<data >= val) - - tree->left = insert_BST(tree->left, val); - - else - - tree->right = insert_BST(tree->right, val); - - return tree; - -} - -bool searchInBST(Node *root , int k) { - // Write your code here - if(root==NULL){ - return false; - } - if(root->data==k){ - return true; - } - else if(root->data>k){ - return searchInBST(root->left ,k); +Node* insert_BST(Node* tree, int val) { + if (tree == nullptr) { + cout << val << " is inserted into BST successfully" << endl; + return new Node(val); } - else if(root->dataright ,k); + if (val <= tree->data) { + tree->left = insert_BST(tree->left, val); + } else { + tree->right = insert_BST(tree->right, val); } - -} -Node* delete_BST(Node *tree, int val) - -{ - - if(tree == NULL){ - - cout<<"value is not present in BST"<data > val) - - tree->left = delete_BST(tree->left, val); - - else if(tree->data < val) - - tree->right = delete_BST(tree->right, val); - - else{ - - if(tree->left == NULL){ - - Node *temp = tree->right; - - free(tree); - - tree= temp; +bool searchInBST(Node* root, int k) { + if (root == nullptr) return false; + if (root->data == k) return true; + return (k < root->data) ? searchInBST(root->left, k) : searchInBST(root->right, k); +} +Node* findMin(Node* node) { + while (node->left != nullptr) { + node = node->left; } + return node; +} - - else if(tree->right == NULL){ - - Node *temp = tree->left; - - tree = temp; - - free(temp); - +Node* delete_BST(Node* tree, int val) { + if (tree == nullptr) { + cout << "Value is not present in BST" << endl; + return tree; } - - else{ - - Node *temp = tree->left; - - while(temp->right->right!=NULL){ - - temp = temp->right; - - } - - tree->data = temp->right->data; - - Node *temp2 = temp->right->left; - - free(temp->right); - - temp->right = temp2; + if (val < tree->data) { + tree->left = delete_BST(tree->left, val); + } else if (val > tree->data) { + tree->right = delete_BST(tree->right, val); + } else { + // Node to be deleted found + if (tree->left == nullptr) { + Node* temp = tree->right; + delete tree; + return temp; + } else if (tree->right == nullptr) { + Node* temp = tree->left; + delete tree; + return temp; + } + // Node with two children: get the inorder successor (smallest in the right subtree) + Node* temp = findMin(tree->right); + tree->data = temp->data; // Copy the inorder successor's value + tree->right = delete_BST(tree->right, temp->data); // Delete the inorder successor } - - } - - return tree; - + return tree; } -void inorder(Node *tree) - -{ - - if (tree != NULL) - - { - +void inorder(Node* tree) { + if (tree != nullptr) { inorder(tree->left); - - cout<data<<" "; - + cout << tree->data << " "; inorder(tree->right); - } - } -void printLevelATNewLine(Node *root) { - if (root == NULL) { - return; - } - queue q; + +void printLevelATNewLine(Node* root) { + if (root == nullptr) return; + + queue q; q.push(root); - q.push(NULL); + q.push(nullptr); // Marker for end of level + while (!q.empty()) { - Node *first = q.front(); + Node* node = q.front(); q.pop(); - if (first == NULL) { - if (q.empty()) { - break; + if (node == nullptr) { + cout << endl; // End of level + if (!q.empty()) { + q.push(nullptr); // Add marker for next level + } + } else { + cout << node->data << " "; + if (node->left != nullptr) { + q.push(node->left); + } + if (node->right != nullptr) { + q.push(node->right); } - cout << endl; - q.push(NULL); - continue; - } - cout << first->data << " "; - if (first->left != NULL) { - q.push(first->left); - } - if (first->right != NULL) { - q.push(first->right); } } } -int main(){ - cout<<"BRITISH COUNCIL CLASS"<>x; - // cout<> z; - switch(z){ - case 1: - cout<<"Enter the student roll no. as they enter the class "<>id; - root=insert_BST(root, id); - inorder(root); - break; - case 2: - int k; - cout<<"Enter the student Roll No. for search "<>k; - if(searchInBST(root,k)==false){ - cout<<"Student is NOT PRESENT in the class"<> z; + switch (z) { + case 1: { + cout << "Enter the student roll no. as they enter the class: "; + int id; + cin >> id; + root = insert_BST(root, id); + break; + } + case 2: { + cout << "Enter the student Roll No. for search: "; + int k; + cin >> k; + cout << (searchInBST(root, k) ? "Student is PRESENT in the class" : "Student is NOT PRESENT in the class") << endl; + break; + } + case 3: { + cout << "Enter the student's Roll No. who exited: "; + int m; + cin >> m; + root = delete_BST(root, m); + break; + } + case 4: + inorder(root); + cout << endl; + break; + case 5: + printLevelATNewLine(root); + break; + case 6: + break; + default: + cout << "Invalid option. Please try again." << endl; + break; } - break; - case 3: - int m; - cout<<"Enter the student's Roll No. who exited "<>m; - delete_BST(root, m); - //printLevelATNewLine(root); - break; - case 4: - inorder(root); - break; - case 5: - printLevelATNewLine(root); - break; - case 6: - z=6; - break; - } - } - // cout << ((searchInBST(root, k)) ? "true" : "false"); - cout<<"*******EXIT********"<