diff --git a/BST.cpp b/BST.cpp new file mode 100644 index 0000000..577bd01 --- /dev/null +++ b/BST.cpp @@ -0,0 +1,373 @@ +/* + * C++ Program To Implement BST In all the senario + */ +# include +# include +using namespace std; +/* + * Node Declaration + */ +struct node +{ + int info; + struct node *left; + struct node *right; +}*root; + +/* + * Class Declaration + */ +class BST +{ + public: + void find(int, node **, node **); + void insert(node *, node *); + void del(int); + void case_a(node *,node *); + void case_b(node *,node *); + void case_c(node *,node *); + void preorder(node *); + void inorder(node *); + void postorder(node *); + void display(node *, int); + BST() + { + root = NULL; + } +}; +/* + * Main Contains Menu + */ +int main() +{ + int choice, num; + BST bst; + node *temp; + while (1) + { + cout<<"-----------------"<>choice; + switch(choice) + { + case 1: + temp = new node; + cout<<"Enter the number to be inserted : "; + cin>>temp->info; + bst.insert(root, temp); + case 2: + if (root == NULL) + { + cout<<"Tree is empty, nothing to delete"<>num; + bst.del(num); + break; + case 3: + cout<<"Inorder Traversal of BST:"<info) + { + *loc = root; + *par = NULL; + return; + } + if (item < root->info) + ptr = root->left; + else + ptr = root->right; + ptrsave = root; + while (ptr != NULL) + { + if (item == ptr->info) + { + *loc = ptr; + *par = ptrsave; + return; + } + ptrsave = ptr; + if (item < ptr->info) + ptr = ptr->left; + else + ptr = ptr->right; + } + *loc = NULL; + *par = ptrsave; +} + +/* + * Inserting Element into the Tree + */ +void BST::insert(node *tree, node *newnode) +{ + if (root == NULL) + { + root = new node; + root->info = newnode->info; + root->left = NULL; + root->right = NULL; + cout<<"Root Node is Added"<info == newnode->info) + { + cout<<"Element already in the tree"<info > newnode->info) + { + if (tree->left != NULL) + { + insert(tree->left, newnode); + } + else + { + tree->left = newnode; + (tree->left)->left = NULL; + (tree->left)->right = NULL; + cout<<"Node Added To Left"<right != NULL) + { + insert(tree->right, newnode); + } + else + { + tree->right = newnode; + (tree->right)->left = NULL; + (tree->right)->right = NULL; + cout<<"Node Added To Right"<left == NULL && location->right == NULL) + case_a(parent, location); + if (location->left != NULL && location->right == NULL) + case_b(parent, location); + if (location->left == NULL && location->right != NULL) + case_b(parent, location); + if (location->left != NULL && location->right != NULL) + case_c(parent, location); + free(location); +} + +/* + * Case A + */ +void BST::case_a(node *par, node *loc ) +{ + if (par == NULL) + { + root = NULL; + } + else + { + if (loc == par->left) + par->left = NULL; + else + par->right = NULL; + } +} + +/* + * Case B + */ +void BST::case_b(node *par, node *loc) +{ + node *child; + if (loc->left != NULL) + child = loc->left; + else + child = loc->right; + if (par == NULL) + { + root = child; + } + else + { + if (loc == par->left) + par->left = child; + else + par->right = child; + } +} + +/* + * Case C + */ +void BST::case_c(node *par, node *loc) +{ + node *ptr, *ptrsave, *suc, *parsuc; + ptrsave = loc; + ptr = loc->right; + while (ptr->left != NULL) + { + ptrsave = ptr; + ptr = ptr->left; + } + suc = ptr; + parsuc = ptrsave; + if (suc->left == NULL && suc->right == NULL) + case_a(parsuc, suc); + else + case_b(parsuc, suc); + if (par == NULL) + { + root = suc; + } + else + { + if (loc == par->left) + par->left = suc; + else + par->right = suc; + } + suc->left = loc->left; + suc->right = loc->right; +} + +/* + * Pre Order Traversal + */ +void BST::preorder(node *ptr) +{ + if (root == NULL) + { + cout<<"Tree is empty"<info<<" "; + preorder(ptr->left); + preorder(ptr->right); + } +} +/* + * In Order Traversal + */ +void BST::inorder(node *ptr) +{ + if (root == NULL) + { + cout<<"Tree is empty"<left); + cout<info<<" "; + inorder(ptr->right); + } +} + +/* + * Postorder Traversal + */ +void BST::postorder(node *ptr) +{ + if (root == NULL) + { + cout<<"Tree is empty"<left); + postorder(ptr->right); + cout<info<<" "; + } +} + +/* + * Display Tree Structure + */ +void BST::display(node *ptr, int level) +{ + int i; + if (ptr != NULL) + { + display(ptr->right, level+1); + cout<: "; + else + { + for (i = 0;i < level;i++) + cout<<" "; + } + cout<info; + display(ptr->left, level+1); + } +} \ No newline at end of file