diff --git a/drivercode/BSTDriver b/drivercode/BSTDriver new file mode 100755 index 0000000..667ea48 Binary files /dev/null and b/drivercode/BSTDriver differ diff --git a/drivercode/BSTDriver.cpp b/drivercode/BSTDriver.cpp index 5bc7709..c2cb560 100644 --- a/drivercode/BSTDriver.cpp +++ b/drivercode/BSTDriver.cpp @@ -3,7 +3,8 @@ #include #include #include - +#include +#include using namespace std; @@ -11,68 +12,203 @@ class TreeNode { public: int val; TreeNode * left; - TreeNode * right; - // Add constructor. + TreeNode * right; + + // Add constructor. + TreeNode(){ + + this->right = this->left = NULL; + } + TreeNode(int val){ + this->val = val; + this->right = this->left = NULL; + } }; vector generateRandomArray(int size) { - return vector(size); + vector arr(size); + for(int i=0;ileft),height(root->right)) + 1; + +} +void inorder(TreeNode* root){ + if(root==NULL) + return; + inorder(root->left); + cout<val<<" "; + inorder(root->right); + return; +} +void preorder(TreeNode* root){ + if(root == NULL) + return; + cout<val<<" "; + preorder(root->left); + preorder(root->right); + return; +} +void postorder(TreeNode * root){ + if(root == NULL) + return; + postorder(root->left); + postorder(root->right); + cout<val<<" "; + return; } - void printTraversals(TreeNode * root) { // print inorder + cout<<"Inorder"<val == val) + return root; + if(root->val > val) + return search(root->left, val); + + return search(root->right, val); } // Return parent even if val is not present in tree. // Basically return the parent of the location where val should have been. TreeNode* searchParent(TreeNode * root, int val) { - return NULL; + if(root == NULL) + return root; + + if(root->left != NULL and root->left->val == val) + return root; + if(root->right != NULL and root->right->val == val) + return root; + if(root->val>val) + return searchParent(root->left,val); + return searchParent(root->right,val); } TreeNode * insert(TreeNode * root, int val) { - return root; + if (root == NULL) { + TreeNode* t=new TreeNode(val); + return t; + } + + + if (val< root->val) { + root->left = insert(root->left, val); + } + else { + root->right = insert(root->right, val); + } + + return root; + + } int maxValue(TreeNode * root) { - return INT_MAX; + if(root->right == NULL) + return root->val; + + return maxValue(root->right); } int minValue(TreeNode * root) { - return INT_MIN; + if(root->left == NULL) + return root->val; + + return minValue(root->left); } // Return NULL if val has no successor, ie, val is greater than maxValue(root); // Successor should be returned even if val is not present in the tree. -TreeNode * findSuccessor(TreeNode * root) { - return NULL; +TreeNode * findSuccessor(TreeNode * root,TreeNode* Root) { + + if(root == NULL) + { return root;} + if(root->right) + return search(root->right,minValue(root->right)); + TreeNode * t = searchParent(Root,root->val); + while( t!=NULL and root == t->right){ + root = t; + t = searchParent(Root,root->val); + } + return t; } // Return NULL if val has no predecessor, ie, val is smaller than minValue(root); // Predecessor should be returned even if val is not present in the tree. -TreeNode * findPredecessor(TreeNode * root) { - return NULL; +TreeNode * findPredecessor(TreeNode * root,TreeNode* Root) { + if(root == NULL) + return root; + if(root->left) + return search(root->left,maxValue(root->left)); + TreeNode * t = searchParent(Root,root->val); + while( t!=NULL and root == t->left){ + root = t; + t = searchParent(Root,root->val); + } + return t; } // First generate random array. Then build this tree by calling insert() on each element of array. TreeNode * buildTree(vector & v) { // Call insert() repeatedly to build the tree. - TreeNode * root = new TreeNode(); - return root; + TreeNode* root = NULL; + for(auto it:v) + { + root = insert(root,it); + } + return root; } +void LevelOrderBFS(TreeNode* root){ + if(root == NULL) + return ; + queue q; + q.push(root); + q.push(NULL); + while(!q.empty()){ + TreeNode* temp = q.front(); + q.pop(); + if(temp == NULL) + { + cout<<"\n"; + if(!q.empty()) + q.push(NULL); + } + else{ + cout<val<<" "; + if(temp->left) + q.push(temp->left); + if(temp->right) + q.push(temp->right); + } + + } + return; +} int main() { @@ -80,20 +216,51 @@ int main() { // https://www.youtube.com/watch?v=K6nw5TvhX2s&list=PLrYpW0KwQ3sMemsZOzhawxMpFet3Hb3SN&index=39 // 1. Generate a random array. - // 2. Create an empty tree and insert one value from the array at a time. - // 3. Inorder traversal of this binary search tree should be equal to the sorted value of random array. + vector arr = generateRandomArray(10); + for(auto it:arr) + cout<val<<" is "<val<<" \n"; + temp = succ; + succ = findSuccessor(temp,root); + } + // of the value just obtained. Print again and so on. // 7. This list should be equal to the sorted value of the array. // 8. Find the maximum value in the array. + int mx = maxValue(root); + cout<<"Max value in tree : "<val<<" is "<val<<" \n"; + temp = psucc; + psucc = findPredecessor(temp,root); + } } diff --git a/drivercode/linked_list_sol.cpp b/drivercode/linked_list_sol.cpp new file mode 100644 index 0000000..ea3a11b --- /dev/null +++ b/drivercode/linked_list_sol.cpp @@ -0,0 +1,220 @@ +// Linkedlist Driver Code + +// Write include statements. +#include + +using namespace std; + +// Linkedlist node +class Node { + public: + int val; + Node * next; + Node () { + this->next = NULL; + } + Node(int val) { + this->val = val; + this->next = NULL; + } +}; + + +// Print linked list +void printList(Node *head) { + while(head != NULL){ + cout<val<<" "; + head = head->next; + } + cout< generateRandomArray(int size ){ + vector arr(size); + for(int i=1;i<=size;i++){ + arr[i-1] = rand() % 117; + } + sort(arr.begin(),arr.end()); + return arr; +} + + +// Convert array to linkedlist +Node * convertArrayToLinkedList(vector & v) { + + Node* head =new Node(); + Node* temp = head; + for(auto it:v){ + Node * temp = new Node(it); + head->next = temp; + head = head->next; + } + return temp->next; +} + + + +// Insert into a linked list at given position. Position = 1 means insert before head. +Node * insertNode (Node * head, int val, int position) { + if(position == 1 ){ + Node* temp = new Node(val); + temp->next = head; + head = temp; + return head; + } + int cnt = 1; + Node * temp = head; + while(cnt!=position-1){ + temp = temp->next; + cnt++; + + } + Node* temp2 = new Node(val); + temp2->next = temp->next; + temp->next = temp2; + + + + return head; +} + + +// Delete from a linked list. +Node *deleteNode (Node * head, int position) { + if(position == 1){ + return head->next; + } + if(head->next == NULL){ + Node *temp = new Node(); + return temp; + } + int cnt = 1; + Node * temp = head; + while(cnt != position-1){ + temp = temp->next; + cnt++; + } + temp->next = temp->next->next; + + + return head; +} + + +// Compare linkedlists. +bool areEqual(Node *head1, Node * head2) { + while(head1 != NULL and head2 !=NULL){ + if(head1->val != head2->val) + return false; + head1 = head1->next; + head2 = head2->next; + } + if(head1 != NULL or head2 !=NULL) + return false; + return true; +} + +// K = 1 means delete last node. +Node *deleteKthNodeFromEnd(Node * head, int k) { + Node * temp1 = head; + Node* temp2 = head; + + while(k-- and temp1!=NULL){ + temp1 = temp1->next; + } + if(temp1 == NULL) + return head->next; + while(temp1->next != NULL){ + temp2 = temp2->next; + temp1 = temp1->next; + } + temp2->next = temp2->next->next; + + return head; +} + + + +// Reverse a linkedlist. +Node * reverseList(Node *head) { + Node * next = head; + Node* curr = head; + Node * prev = NULL; + while(curr != NULL){ + next = curr->next; + curr->next = prev; + prev = curr; + curr = next; + } + return prev; +} + +// Merge sorted lists. +Node * mergeSortedLists(Node * head1, Node * head2) { + Node *h1 = head1; + if(head1->val < head2->val){ + head1 = head1->next; + } + else{ + h1 = head2; + head2 = head2->next; + } + Node* temp = h1; + while(head1 != NULL and head2 != NULL){ + if(head1->val < head2->val){ + + h1->next = head1; + h1 = h1->next; + head1 = head1->next; + } + else{ + h1->next = head2; + h1 = h1->next; + head2 = head2->next; + } + } + while(head1 != NULL){ + h1->next = head1; + head1 = head1->next; + h1 = h1->next; + } + while(head2 != NULL){ + h1->next = head1; + head2 = head2->next; + h1 = h1->next; + } + +return temp; +} + +int main() { + // 1. Get a random array. + // 2. Convert array to LinkedList List1. + // 3. Insert a number at position P into the list. + // 4. Delete this number from position P. + // 5. Create another linkedlist (List2) from the same random array. + // 6. Compare that List1 is the same as List2. + + int size = 4; + vector arr = generateRandomArray(size); + vector arr2 = generateRandomArray(size+2); + Node *head = convertArrayToLinkedList(arr); + Node *head2 = convertArrayToLinkedList(arr2); +// printList(head); +// head = insertNode(head,312,11); +// printList(head); +// head = deleteNode(head,10); + printList(head); + printList(head2); +// printList(head2); +// cout<< areEqual(head,head2)<