diff --git a/BST.cpp b/BST.cpp new file mode 100644 index 0000000..f1ec138 --- /dev/null +++ b/BST.cpp @@ -0,0 +1,280 @@ +#include +using namespace std; +struct BST +{ + int id; + BST *left = NULL, *right = NULL; +}; +BST *root = NULL; +void insertNode(BST *curr) +{ + if (root == NULL) + { + root = curr; + } + else + { + BST *p = root, *q = NULL; + while (p != NULL) + { + q = p; + if (p->id >curr->id) + { + p = p->left; + } + else + { + p = p->right; + } + } + if (q->id >curr->id) + { + q->left = curr; + } + else + { + q->right = curr; + } + } +} +void autoFillBST() +{ + int arr[] = {80, 60, 100, 110, 200, 210, 105, 50, 20, 40, 65, 75, 82, 84, 85}; + int size = 15; + for (int i = 0; i < size; i++) + { + BST *curr = new BST; curr->id = arr[i]; + insertNode(curr); + } + cout<<"\nBinary Search Tree Constructed"<left); + cout<<"ID: "<id<right); + } +} +void postOrderTraversal(BST *p) +{ + if (p!=NULL) + { + inorderTraversal(p->left); + inorderTraversal(p->right); + cout<<"ID: "<id<id<left); + inorderTraversal(p->right); + } +} +BST* searchNode(int key) +{ + BST *p = root; + while (p!=NULL) + { + if(p->id == key) + { + return p; + } + else if (p->id > key) + { + p = p->left; + } + else + { + p = p->right; + } + } + return NULL; +} + +BST* rightLeastNode(BST *p) +{ + while (p->left!=NULL) + { + cout<<"Finding right least value: "<id<left; + } + cout<<"Finding right least value: "<id<left !=NULL && nodeToDelete->right != NULL) + { + BST *rightLeast = rightLeastNode(nodeToDelete->right); + nodeToDelete->id = rightLeast->id; + cout<<"Found node to delete: "<id<right; + while (p != rightLeast) + { + cout<<"Finding the node to delete: "<id<left; + } + cout<<"Finding the node to delete: "<id<id<left != NULL && nodeToDelete->right == NULL) + { + nodeToDelete->id = nodeToDelete->left->id; + nodeToDelete->left = nodeToDelete->left->left; + } + else if (nodeToDelete->left == NULL && nodeToDelete->right != NULL) + { + nodeToDelete->id = nodeToDelete->right->id; + nodeToDelete->right = nodeToDelete->right->right; + } + else + { + delete nodeToDelete; nodeToDelete = NULL; + } +} +int countleaf = 0; //variable which will store num of leaf NODES +void countLeafNodes(BST *p) +{ + if (p!=NULL) + { + if(p->left== NULL&&p->right ==NULL) + { + countleaf++; + } + + countLeafNodes(p->left); + countLeafNodes(p->right); + } +} +int count = 0; //variable which will store number of NODES +void countNumOfNodes(BST *p) +{ + if (p!=NULL) + { + count++; + countNumOfNodes(p->left); + countNumOfNodes(p->right); + } +} +int max(int leftHeight, int rightHeight) +{ + if (leftHeight > rightHeight) + { + return leftHeight; + } + return rightHeight; +} +int findHeight(BST *p) +{ + if (p == NULL) + { + return -1; + } + int leftHeight = findHeight(p->left); + int rightHeight = findHeight(p->right); + + return max(leftHeight, rightHeight) + 1; +} +void opt() +{ + cout<<"\n1- Insert a Node"<>choice; + + switch (choice) + { + case 1: + cout<<"\nINSERT A NODE METHOD"<>curr->id; + insertNode(curr); opt(); break; + + case 2: + cout<<"\nAUTOFILL BST METHOD"<>val; + BST *nodeToDelete; nodeToDelete = searchNode(val); + deleteNode(nodeToDelete); opt(); break; + + case 4: + cout<<"\nSEARCH A NODE METHOD"<>key; + if (searchNode(key) != NULL) + { + cout<<"Id found: "<id<right); + cout<<"Number of Nodes at right of Tree is: "<