From 470ac0b438ffa7abf80811ccfe6b7e2505de1ee3 Mon Sep 17 00:00:00 2001 From: Alik Agarwala Date: Wed, 12 Jul 2023 20:58:30 +0530 Subject: [PATCH 1/9] Correct heading in README.md Corrected the heading with "Linked list". It was previously written as "Linklist". --- 11. Linked List/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/11. Linked List/README.md b/11. Linked List/README.md index a209c884..127657f6 100644 --- a/11. Linked List/README.md +++ b/11. Linked List/README.md @@ -1,5 +1,5 @@ # Aug 13 -## Linklist +## Linked list 1. merge-two-sorted-linked-lists
2.reverse-a-linked-list
From 52a4846b3ce9eac642a1a851a996fdd89f6bf015 Mon Sep 17 00:00:00 2001 From: Alik Agarwala Date: Wed, 12 Jul 2023 21:02:14 +0530 Subject: [PATCH 2/9] Add OOP implementation of doubly linked list. Added the doublyLinkedListInOOP.cpp file which contains a perfect Object Oriented Implementation of doubly linked list with all the necessary helper functions. Working with multiple doubly linked lists are easy in this implementation. --- 11. Linked List/doublyLinkedListInOOP.cpp | 157 ++++++++++++++++++++++ 1 file changed, 157 insertions(+) create mode 100644 11. Linked List/doublyLinkedListInOOP.cpp diff --git a/11. Linked List/doublyLinkedListInOOP.cpp b/11. Linked List/doublyLinkedListInOOP.cpp new file mode 100644 index 00000000..935c0d21 --- /dev/null +++ b/11. Linked List/doublyLinkedListInOOP.cpp @@ -0,0 +1,157 @@ +#include +using namespace std; +class Node +{ +public: + int data; + Node *next, *prev; + Node(int data) + { + this -> data = data; + this -> next = nullptr; + this -> prev = nullptr; + } +}; +class LinkedList +{ + Node *head = nullptr, *tail = nullptr; + int countNode() + { + int count = 0; + Node *temp = head; + while (temp) + { + count++; + temp = temp -> next; + } + return count; + } +public: + void createDoublyLinkedList(int data) + { + Node *newNode = new Node(data); + if (!head) + head = tail = newNode; + else + { + tail -> next = newNode; + newNode -> prev = tail; + tail = newNode; + } + } + void insertAtBeginning(int data) + { + Node *newNode = new Node(data); + head -> prev = newNode; + newNode -> next = head; + head = newNode; + } + void insertAtEnd(int data) + { + Node *newNode = new Node(data); + tail -> next = newNode; + newNode -> prev = tail; + tail = newNode; + } + void insertAtPosition(int data, int position) + { + if (position < 1 || position > countNode()) + exit(1); + else if (position == 1) + insertAtBeginning(data); + else + { + Node *newNode = new Node(data); + Node *temp = head; + for (int i = 1; i < position - 1; i++) + temp = temp -> next; + newNode -> prev = temp; + newNode -> next = temp -> next; + temp -> next = newNode; + newNode -> next -> prev = newNode; + } + } + void deleteFromBeginning() + { + Node *temp = head; + if (!head) + exit(2); + head = head -> next; + head -> prev = nullptr; + delete temp; + } + void deleteFromEnd() + { + if (!tail) + exit(3); + Node *temp = tail; + tail -> prev -> next = nullptr; + tail = tail -> prev; + delete temp; + } + void deleteFromPosition(int position) + { + if (position < 1 || position > countNode()) + exit(4); + if (position == 1) + deleteFromBeginning(); + else if (position == countNode()) + deleteFromEnd(); + else + { + Node *temp = head; + for (int i = 1; i < position; i++) + temp = temp -> next; + temp -> prev -> next = temp -> next; + temp -> next -> prev = temp -> prev; + delete temp; + } + } + void reverseList() + { + Node *currentNode, *nextNode; + currentNode = head; + while (currentNode) + { + nextNode = currentNode -> next; + currentNode -> next = currentNode -> prev; + currentNode -> prev = nextNode; + currentNode = nextNode; + } + currentNode = head; + head = tail; + tail = currentNode; + } + void displayList() + { + if (!head) + exit(2); + Node *temp = head; + while (temp) + { + cout << temp -> data << " "; + temp = temp -> next; + } + cout << endl; + } +}; +int main() +{ + LinkedList l1; + l1.createDoublyLinkedList(3); + l1.createDoublyLinkedList(4); + l1.createDoublyLinkedList(5); + l1.insertAtBeginning(1); + l1.insertAtPosition(2, 2); + l1.insertAtEnd(6); + // l1.deleteFromBeginning(); + // l1.deleteFromEnd(); + // l1.deleteFromPosition(2); + // l1.deleteFromPosition(1); + // l1.deleteFromPosition(2); + // l1.displayList(); + l1.displayList(); + l1.reverseList(); + l1.displayList(); + return 0; +} From b6d48182194da85215104a40de5ddcf7bd402b1a Mon Sep 17 00:00:00 2001 From: Alik Agarwala Date: Wed, 12 Jul 2023 21:05:04 +0530 Subject: [PATCH 3/9] Add OOP implementation of two types of Linked List Added two files which are "singlyCIrcularLinkedListOOP.cpp" and "singlyLinkedListOOP.cpp". They contain Object Oriented implementation of singly linked list and singly circular linked list. Working with multiple linked lists are easy in these implementations. --- .../singlyCIrcularLinkedListOOP.cpp | 156 ++++++++++++++++++ 11. Linked List/singlyLinkedListOOP.cpp | 152 +++++++++++++++++ 2 files changed, 308 insertions(+) create mode 100644 11. Linked List/singlyCIrcularLinkedListOOP.cpp create mode 100644 11. Linked List/singlyLinkedListOOP.cpp diff --git a/11. Linked List/singlyCIrcularLinkedListOOP.cpp b/11. Linked List/singlyCIrcularLinkedListOOP.cpp new file mode 100644 index 00000000..fc2a320c --- /dev/null +++ b/11. Linked List/singlyCIrcularLinkedListOOP.cpp @@ -0,0 +1,156 @@ +#include +using namespace std; +class Node +{ +public: + int data; + Node *next; + Node(int data) + { + this -> data = data; + this -> next = nullptr; + } +}; +class LinkedList +{ + Node *tail = nullptr; + // void createList(int data) //by maintaining both head and tail pointer + // { + // Node *newNode = new Node(data); + // if (!head) + // head = tail = newNode; + // else + // { + // tail -> next = newNode; + // tail = newNode; + // } + // tail -> next = head; + // } +public: + void createList(int data) //only maintaining a tail pointer + { + Node *newNode = new Node(data); + if (!tail) + { + tail = newNode; + tail -> next = newNode; + } + else + { + newNode -> next = tail -> next; + tail -> next = newNode; + tail = newNode; + } + } + void displayList() + { + if (!tail) + exit(1); + Node *temp = tail -> next; + while (temp -> next != tail -> next) + { + cout << temp -> data << " "; + temp = temp -> next; + } + cout << temp -> data << endl; + } + void insertAtBeginning(int data) + { + Node *newNode = new Node(data); + if (!tail) + tail = newNode; + else + newNode -> next = tail -> next; + tail -> next = newNode; + } + void insertAtEnd(int data) + { + Node *newNode = new Node(data); + if (!tail) + { + tail = newNode; + tail -> next = newNode; + } + else + { + newNode -> next = tail -> next; + tail -> next = newNode; + tail = newNode; + } + } + void insertAtPosition(int data, int position) + { + if (position == 1) + insertAtBeginning(data); + else + { + Node *newNode = new Node(data); + Node *temp = tail -> next; + for (int i = 1; i < position - 1; i++) + temp = temp -> next; + newNode -> next = temp -> next; + temp -> next = newNode; + } + } + void deleteFromBeginning() + { + if (!tail) + exit(2); + Node *temp = tail -> next; + if (temp -> next == temp) + tail = nullptr; + else + tail -> next = temp -> next; + delete temp; + } + void deleteFromEnd() + { + if (!tail) + exit(3); + Node *current = tail -> next, *previous; + if (current -> next == current) + tail = nullptr; + else + { + while (current -> next != tail -> next) + { + previous = current; + current = current -> next; + } + previous -> next = tail -> next; + tail = previous; + } + delete current; + } + void deleteFromPosition(int position) + { + if (!tail) + exit(3); + if (position == 1) + deleteFromBeginning(); + else + { + Node *current = tail -> next, *nextNode; + for (int i = 1; i < position - 1; i++) + current = current -> next; + nextNode = current -> next; + current -> next = nextNode -> next; + delete nextNode; + } + } +}; +int main() +{ + LinkedList l1; + l1.createList(5); + l1.createList(6); + l1.createList(7); + l1.insertAtBeginning(3); + l1.insertAtEnd(8); + l1.insertAtPosition(4, 2); + l1.deleteFromBeginning(); + l1.deleteFromEnd(); + l1.deleteFromPosition(3); + l1.displayList(); + return 0; +} diff --git a/11. Linked List/singlyLinkedListOOP.cpp b/11. Linked List/singlyLinkedListOOP.cpp new file mode 100644 index 00000000..dfdf0c75 --- /dev/null +++ b/11. Linked List/singlyLinkedListOOP.cpp @@ -0,0 +1,152 @@ +#include +using namespace std; +class Node +{ +public: + int data; + Node *next; + Node(int data) + { + this -> data = data; + this -> next = nullptr; + } +}; +class LinkedList +{ + Node *head = nullptr; + int countNode() + { + int count = 0; + Node *temp = head; + while (temp) + { + count++; + temp = temp -> next; + } + return count; + } +public: + void insertAtBeginning(int data) + { + Node *newNode = new Node(data); + newNode -> next = head; + head = newNode; + } + void insertAtLast(int data) + { + if (!head) + insertAtBeginning(data); + Node *newNode = new Node(data); + Node *temp = head; + while (temp -> next) + temp = temp -> next; + temp -> next = newNode; + } + void insertAtPosition(int data, int position) + { + if (position > countNode()) + exit(1); + Node *newNode = new Node(data); + Node *temp = head; + int i = 1; + while (i < position) + { + temp = temp -> next; + i++; + } + newNode -> next = temp -> next; + temp -> next = newNode; + } + void deleteFromBeginning() + { + if (!head) + exit(3); + Node *temp = head; + head = head -> next; + delete temp; + } + void deleteFromEnd() + { + if (!head) + exit(4); + Node *temp, *previousNode; + while (temp -> next) + { + previousNode = temp; + temp = temp -> next; + } + if (temp == head) + head = nullptr; + else + previousNode -> next = nullptr; + delete temp; + } + void deleteFromPosition(int position) + { + if (!head) + exit(5); + Node *nextNode; + int i = 1; + Node *temp = head; + while (i < position - 1) + { + temp = temp -> next; + i++; + } + nextNode = temp -> next; + temp -> next = nextNode -> next; + delete nextNode; + } + void displayList() + { + if (!head) + exit(2); + Node *temp = head; + while (temp) + { + cout << temp -> data << " "; + temp = temp -> next; + } + cout << endl; + } + void reverseList() + { + Node *currentNode, *previousNode, *nextNode; + currentNode = head; + previousNode = nullptr; + while (currentNode) + { + nextNode = currentNode -> next; + currentNode -> next = previousNode; + previousNode = currentNode; + currentNode = nextNode; + } + head = previousNode; + } +}; +int main() +{ + LinkedList l1; + // l1.insertAtBeginning(2); + // l1.insertAtBeginning(1); + // l1.insertAtLast(4); + // l1.insertAtPosition(3, 2); + // l1.displayList(); + // l1.deleteFromBeginning(); + // l1.deleteFromPosition(2); + // l1.deleteFromEnd(); + // l1.displayList(); + // output 2 + + l1.insertAtBeginning(1); + l1.insertAtBeginning(2); + l1.insertAtBeginning(3); + l1.insertAtBeginning(4); + l1.insertAtBeginning(5); + l1.displayList(); + l1.reverseList(); + l1.displayList(); + + + return 0; +} From 6176916ec221301dfcebb3fe1f19803a106fd5f1 Mon Sep 17 00:00:00 2001 From: Alik Agarwala Date: Wed, 12 Jul 2023 21:07:07 +0530 Subject: [PATCH 4/9] Update parenthesis-checker.cpp with proper functions --- 12. Stack/parenthesis-checker.cpp | 78 +++++++++++-------------------- 1 file changed, 28 insertions(+), 50 deletions(-) diff --git a/12. Stack/parenthesis-checker.cpp b/12. Stack/parenthesis-checker.cpp index 599b0bb1..3d4591ea 100644 --- a/12. Stack/parenthesis-checker.cpp +++ b/12. Stack/parenthesis-checker.cpp @@ -1,54 +1,32 @@ - -// { Driver Code Starts -#include +#include +#include using namespace std; - - - // } Driver Code Ends - - -class Solution +bool checkValidPair(char opening, char closing) { - public: - //Function to check if brackets are balanced or not. - bool ispar(string x) - { - int n= x.length(); - stack a; - int d=0,b=0; - if(x[0]=='}' || x[0]==')'||x[0]==']') return false; - for(int i=0;i s; + for (int i = 0; i < expression.length(); i++) + if (expression[i] == '(' || expression[i] == '{' || expression[i] == '[') + s.push(expression[i]); + else if (expression[i] == ')' || expression[i] == '}' || expression[i] == ']') + if (s.empty() || !checkValidPair(s.top(), expression[i])) + return false; + else + s.pop(); + return s.empty() ? true : false; +} int main() { - int t; - string a; - cin>>t; - while(t--) - { - cin>>a; - Solution obj; - if(obj.ispar(a)) - cout<<"balanced"<> expression; + if (checkParenthesesBalanced(expression)) + cout << "Balanced"; + else + cout << "Not balanced"; + return 0; +} From 6e419079acb1f961749a922d59b18000b6290274 Mon Sep 17 00:00:00 2001 From: Alik Agarwala Date: Wed, 12 Jul 2023 21:08:30 +0530 Subject: [PATCH 5/9] Add linked list implementation of Queue --- 13. Queue/linkedListImplementation.cpp | 56 ++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 13. Queue/linkedListImplementation.cpp diff --git a/13. Queue/linkedListImplementation.cpp b/13. Queue/linkedListImplementation.cpp new file mode 100644 index 00000000..b68f192b --- /dev/null +++ b/13. Queue/linkedListImplementation.cpp @@ -0,0 +1,56 @@ +#include +using namespace std; +class Node +{ +public: + int data; + Node *next; + Node(int data) + { + this -> data = data; + this -> next = nullptr; + } +}; +class Queue +{ + Node *front = nullptr, *rear = nullptr; +public: + void enqueue(int data) + { + Node *newNode = new Node(data); + if (!front && !rear) + front = rear = newNode; + else + { + rear -> next = newNode; + rear = newNode; + } + } + void dequeue() + { + if (!front) + exit(1); + Node *temp = front; + if (front == rear) + front = rear = nullptr; + else + front = front -> next; + delete temp; + } + int peek() + { + if (!front) + exit(2); + return front -> data; + } +}; +int main() +{ + Queue q; + q.enqueue(1); + q.enqueue(2); + q.enqueue(3); + q.dequeue(); + cout << q.peek(); + return 0; +} From 14d8fd525c3126e9239a00ae3337de7c752826b7 Mon Sep 17 00:00:00 2001 From: Alik Agarwala Date: Wed, 12 Jul 2023 21:09:13 +0530 Subject: [PATCH 6/9] Add array implementation of Queue --- 13. Queue/arrayImplementation.cpp | 52 +++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 13. Queue/arrayImplementation.cpp diff --git a/13. Queue/arrayImplementation.cpp b/13. Queue/arrayImplementation.cpp new file mode 100644 index 00000000..269a483b --- /dev/null +++ b/13. Queue/arrayImplementation.cpp @@ -0,0 +1,52 @@ +#include +#define n 10 +using namespace std; +class Queue +{ + int arr[n]; + int front = -1, rear = -1; +public: + bool isEmpty() + { + return (front == -1 && rear == -1); + } + bool isFull() + { + return (((rear + 1) % n) == front); + } + void enqueue(int data) + { + if (isFull()) + exit(1); + if (isEmpty()) + front = rear = 0; + else + rear = (rear + 1) % n; + arr[rear] = data; + } + void dequeue() + { + if (isEmpty()) + exit(2); + else if (front == rear) + rear = front = -1; + else + front = (front + 1) % n; + } + int peek() + { + if (front == -1) + exit(3); + return arr[front]; + } +}; +int main() +{ + Queue q; + q.enqueue(1); + q.enqueue(2); + q.enqueue(3); + q.dequeue(); + cout << q.peek(); + return 0; +} From a61c2144e4efb4900d5086dded50a4678dea2b89 Mon Sep 17 00:00:00 2001 From: Alik Agarwala Date: Wed, 12 Jul 2023 21:10:04 +0530 Subject: [PATCH 7/9] Add prefix evaluation code --- 12. Stack/evaluatePrefix.cpp | 52 ++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) create mode 100644 12. Stack/evaluatePrefix.cpp diff --git a/12. Stack/evaluatePrefix.cpp b/12. Stack/evaluatePrefix.cpp new file mode 100644 index 00000000..1e413ce8 --- /dev/null +++ b/12. Stack/evaluatePrefix.cpp @@ -0,0 +1,52 @@ +//This code works for single digit values only. Ex : '+ 2 6' +#include +#include +using namespace std; +int performOperation(char operation, int operand1, int operand2) +{ + if (operation == '+') + return operand1 + operand2; + else if (operation == '-') + return operand1 - operand2; + else if (operation == '*') + return operand1 * operand2; + else if (operation == '/') + return operand1 / operand2; + else + exit(1); +} +bool isValidOperator(char ch) +{ + return (ch == '+' || ch == '-' || ch == '*' || ch == '/') ? true : false; +} +bool isValidNumber(char ch) +{ + return (ch >= '0' && ch <= '9') ? true : false; +} +int evaluatePrefix(string expression) +{ + stack s; + for (int i = expression.length() - 1; i >= 0; i--) + { + if (expression[i] == ' ' || expression[i] == ',') + continue; + else if (isValidOperator(expression[i])) + { + int operand1 = s.top(); + s.pop(); + int operand2 = s.top(); + s.pop(); + s.push(performOperation(expression[i], operand1, operand2)); + } + else if (isValidNumber(expression[i])) + s.push(expression[i] - '0'); + } + return s.top(); +} +int main() +{ + string expression; + getline(cin, expression); + cout << evaluatePrefix(expression); + return 0; +} From aeab2a70dcce8df01d289aaed4b557ae390a8336 Mon Sep 17 00:00:00 2001 From: Alik Agarwala Date: Wed, 12 Jul 2023 21:11:13 +0530 Subject: [PATCH 8/9] Add array implementation of Stack Added array implementation of Stack data structure using OOP concept. --- 12. Stack/arrayImplementation.cpp | 60 +++++++++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 12. Stack/arrayImplementation.cpp diff --git a/12. Stack/arrayImplementation.cpp b/12. Stack/arrayImplementation.cpp new file mode 100644 index 00000000..135fb036 --- /dev/null +++ b/12. Stack/arrayImplementation.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; +#define size 10 +class Stack +{ + int arr[size]; + int top = -1; +public: + void push(int data) + { + if (top == size - 1) + { + cout << "Stack overflow !" << endl << "Exiting..." << endl; + exit(1); + } + arr[++top] = data; + } + void pop() + { + if (top == -1) + { + cout << "Stack underflow !" << endl << "Exiting..." << endl; + exit(2); + } + top--; + } + int peek() + { + if (top == -1) + { + cout << "Stack empty !" << endl << "Exiting..." << endl; + exit(3); + } + return arr[top]; + } + bool isEmpty() + { + if (top == -1) + return true; + return false; + } + void display() + { + if (isEmpty()) + cout << "Stack empty !"; + else + for (int i = 0; i <= top; i++) + cout << arr[i] << " "; + cout << endl; + } + +}; +int main() +{ + Stack s; + s.push(1); + s.push(2); + s.display(); + return 0; +} From 15ba9ac2c36ab7de0bab3de3e2f09d9fa9506c2d Mon Sep 17 00:00:00 2001 From: Alik Agarwala Date: Wed, 12 Jul 2023 21:12:38 +0530 Subject: [PATCH 9/9] Add codes of Binary Search Tree Added OOP implementation of Binary Search Tree and some important codes. --- 15. Tree/checkBSTorNOT.cpp | 59 +++++++++++++++++++ 15. Tree/findHeight.cpp | 51 ++++++++++++++++ 15. Tree/findMinMaxFromBST.cpp | 69 ++++++++++++++++++++++ 15. Tree/implementBST.cpp | 60 +++++++++++++++++++ 15. Tree/treeTraversal.cpp | 103 +++++++++++++++++++++++++++++++++ 5 files changed, 342 insertions(+) create mode 100644 15. Tree/checkBSTorNOT.cpp create mode 100644 15. Tree/findHeight.cpp create mode 100644 15. Tree/findMinMaxFromBST.cpp create mode 100644 15. Tree/implementBST.cpp create mode 100644 15. Tree/treeTraversal.cpp diff --git a/15. Tree/checkBSTorNOT.cpp b/15. Tree/checkBSTorNOT.cpp new file mode 100644 index 00000000..298d6a82 --- /dev/null +++ b/15. Tree/checkBSTorNOT.cpp @@ -0,0 +1,59 @@ +#include +#include +using namespace std; +class Node +{ +public: + int data; + Node *left, *right; + Node(int data) + { + this->data = data; + this->left = this->right = nullptr; + } +}; +class BST +{ + Node *root = nullptr; + void insertNodeinBST(Node *&root, int data) + { + if (!root) + root = new Node(data); + else if (data < root->data) + insertNodeinBST(root->left, data); + else + insertNodeinBST(root->right, data); + } + bool isBinarySearchTree(Node *root, int minValue, int maxValue) + { + if (!root) + return true; + if (root->data > minValue && root->data < maxValue && + isBinarySearchTree(root->left, minValue, root->data) && + isBinarySearchTree(root->right, root->data, maxValue) + ) + return true; + return false; + } +public: + void insert(int data) + { + insertNodeinBST(root, data); + } + bool checkValidBST() + { + return isBinarySearchTree(root, INT_MIN, INT_MAX); + } +}; +int main() +{ + BST t; + t.insert(15); + t.insert(10); + t.insert(20); + t.insert(25); + t.insert(8); + t.insert(12); + cout << t.checkValidBST(); + return 0; +} diff --git a/15. Tree/findHeight.cpp b/15. Tree/findHeight.cpp new file mode 100644 index 00000000..9f63b52e --- /dev/null +++ b/15. Tree/findHeight.cpp @@ -0,0 +1,51 @@ +#include +using namespace std; +class Node +{ +public: + int data; + Node *left, *right; + Node(int data) + { + this->data = data; + this->left = this->right = nullptr; + } +}; +class BST +{ + Node *root = nullptr; + void insertNode(Node *&root, int data) + { + if (!root) + root = new Node(data); + else if (data <= root->data) + insertNode(root->left, data); + else + insertNode(root->right, data); + } + int calculateHeightBST(Node *root) + { + if (!root) + return -1; + return max(calculateHeightBST(root->left), calculateHeightBST(root->right)) + 1; + } +public: + void insert(int data) + { + insertNode(root, data); + } + int findHeight() + { + return calculateHeightBST(root); + } +}; +int main() +{ + BST t; + t.insert(5); + t.insert(3); + t.insert(8); + t.insert(2); + cout << t.findHeight(); + return 0; +} diff --git a/15. Tree/findMinMaxFromBST.cpp b/15. Tree/findMinMaxFromBST.cpp new file mode 100644 index 00000000..c90a77e0 --- /dev/null +++ b/15. Tree/findMinMaxFromBST.cpp @@ -0,0 +1,69 @@ +#include +using namespace std; +class Node +{ +public: + int data; + Node *left, *right; + Node(int data) + { + this->data = data; + this->left = this->right = nullptr; + } +}; +class BST +{ + Node *root = nullptr; + void insertNode(Node *&root, int data) + { + if (!root) + root = new Node(data); + else if (data <= root->data) + insertNode(root->left, data); + else + insertNode(root->right, data); + } + int findMinBST(Node *root) + { + if (!root) + exit(1); + else if (root->left == nullptr) + return root->data; + else + return findMinBST(root->left); + } + int findMaxBST(Node *root) + { + if (!root) + exit(1); + else if (root->right == nullptr) + return root->data; + else + return findMaxBST(root->right); + } +public: + void insert(int data) + { + insertNode(root, data); + } + int findMin() + { + return findMinBST(root); + } + int findMax() + { + return findMaxBST(root); + } +}; +int main() +{ + BST t; + t.insert(15); + t.insert(10); + t.insert(20); + t.insert(25); + t.insert(8); + t.insert(12); + cout << t.findMin() << " " << t.findMax(); + return 0; +} diff --git a/15. Tree/implementBST.cpp b/15. Tree/implementBST.cpp new file mode 100644 index 00000000..41d47d89 --- /dev/null +++ b/15. Tree/implementBST.cpp @@ -0,0 +1,60 @@ +#include +using namespace std; +class Node +{ +public: + int data; + Node *left, *right; + Node(int data) + { + this->data = data; + this->left = this->right = nullptr; + } +}; +class BST +{ + Node *root = nullptr; + void insertNode(Node *&root, int data){ + if (!root) + root = new Node(data); + else if (data <= root->data) + insertNode(root->left, data); + else + insertNode(root->right, data); + } + bool searchNode(Node *root, int data) + { + if (!root) + return false; + else if (root->data == data) + return true; + else if (data <= root->data) + return searchNode(root->left, data); + else + return searchNode(root->right, data); + } +public: + void insert(int data) + { + insertNode(root, data); + } + bool search(int data) + { + return searchNode(root, data); + } +}; +int main() +{ + BST t; + t.insert(15); + t.insert(10); + t.insert(20); + t.insert(25); + t.insert(8); + t.insert(12); + cout << t.search(15) << endl; + cout << t.search(31) << endl; + cout << t.search(20) << endl; + cout << t.search(7) << endl; + return 0; +} diff --git a/15. Tree/treeTraversal.cpp b/15. Tree/treeTraversal.cpp new file mode 100644 index 00000000..31be95ac --- /dev/null +++ b/15. Tree/treeTraversal.cpp @@ -0,0 +1,103 @@ +#include +#include +using namespace std; +class Node +{ +public: + int data; + Node *left, *right; + Node(int data) + { + this->data = data; + this->left = this->right = nullptr; + } +}; +class BST +{ + Node *root = nullptr; + void insertNodeBST(Node *&root, int data) + { + if (!root) + root = new Node(data); + else if (data <= root->data) + insertNodeBST(root->left, data); + else + insertNodeBST(root->right, data); + } + void performLevelOrderTraversal(Node *root) + { + if (!root) + return; + queue q; + q.push(root); + while (!q.empty()) + { + Node *current = q.front(); + cout << current->data << " "; + if (current->left) + q.push(current->left); + if (current->right) + q.push(current->right); + q.pop(); + } + cout << endl; + } + void preorder(Node *root) + { + if (!root) + return; + cout << root->data << " "; + preorder(root->left); + preorder(root->right); + } + void inorder(Node *root) + { + if (!root) + return; + preorder(root->left); + cout << root->data << " "; + preorder(root->right); + } + void postorder(Node *root) + { + if (!root) + return; + preorder(root->left); + preorder(root->right); + cout << root->data << " "; + } +public: + void insert(int data) + { + insertNodeBST(root, data); + } + void levelOrderTraversal() + { + cout << "Level order : "; + performLevelOrderTraversal(root); + } + void depthFirstTraversal() + { + cout << "Preorder : "; + preorder(root); + cout << endl; + cout << "Inorder : "; + inorder(root); + cout << endl; + cout << "Postorder : "; + postorder(root); + cout << endl; + } + +}; +int main() +{ + BST t; + t.insert(5); + t.insert(3); + t.insert(8); + t.insert(2); + t.levelOrderTraversal(); + t.depthFirstTraversal(); + return 0; +}