diff --git a/CPP/bst.c b/CPP/bst.c new file mode 100644 index 0000000..823695c --- /dev/null +++ b/CPP/bst.c @@ -0,0 +1,359 @@ +# include +# include +using namespace std; +struct nod//node declaration +{ + int info; + struct nod *l; + struct nod *r; +}*r; +class BST +{ + public://functions declaration + void search(nod *, int); + void find(int, nod **, nod **); + void insert(nod *, nod *); + void del(int); + void casea(nod *,nod *); + void caseb(nod *,nod *); + void casec(nod *,nod *); + void preorder(nod *); + void inorder(nod *); + void postorder(nod *); + void show(nod *, int); + BST() + { + r = NULL; + } +}; +void BST::find(int i, nod **par, nod **loc)//find the position of the item +{ + nod *ptr, *ptrsave; + if (r == NULL) + { + *loc = NULL; + *par = NULL; + return; + } + if (i == r→info) + { + *loc = r; + *par = NULL; + return; + } + if (i < r→info) + ptr = r→l; + else + ptr = r→r; + ptrsave = r; + while (ptr != NULL) + { + if (i == ptr→info) + { + *loc = ptr; + *par = ptrsave; + return; + } + ptrsave = ptr; + if (i < ptr→info) + ptr = ptr→l; + else + ptr = ptr→r; + } + *loc = NULL; + *par = ptrsave; +} +void BST::search(nod *root, int data) //searching +{ + int depth = 0; + nod *temp = new nod; + temp = root; + while(temp != NULL) + { + depth++; + if(temp→info == data) + { + cout<<"\nData found at depth: "< data) + temp = temp→l; + else + temp = temp→r; + } + cout<<"\n Data not found"< newnode→info) + { + if (tree→l != NULL) + { + insert(tree→l, newnode); + } + else + { + tree→l= newnode; + (tree→l)→l = NULL; + (tree→l)→r= NULL; + cout<<"Node Added To Left"<>c; + switch(c) + { + case 1: + t = new nod; + cout<<"Enter the number to be inserted : "; + cin>>t→info; + bst.insert(r, t); + break; + case 2: + if (r == NULL) + { + cout<<"Tree is empty, nothing to delete"<>n; + bst.del(n); + break; + case 3: + cout<<"Search:"<>item; + bst.search(r,item); + break; + case 4: + cout<<"Inorder Traversal of BST:"<