Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct node{
- int data;
- node *left;
- node *right;
- };
- class BST{
- public:
- node *root;
- node *t;
- BST(){
- t = NULL;
- root = NULL;
- }
- node* getroot(){
- return this->root;
- }
- void insert(int key){
- node* nn = new node;
- nn -> data = key;
- nn -> left = NULL;
- nn -> right = NULL;
- if(root == NULL){
- root = nn;
- }
- else{
- node* t = root;
- node* p = NULL;
- while(t != NULL){
- p = t;
- if(t -> data < nn -> data){
- t = t -> right;
- }
- else if(t -> data > nn -> data){
- t = t -> left;
- }
- }
- if(p -> data < nn -> data){
- p -> right = nn;
- }
- else{
- p -> left = nn;
- }
- }
- }
- void inorder(struct node* temp){
- if (!temp)
- return;
- inorder(temp -> left);
- cout << temp -> data << " ";
- inorder(temp -> right);
- }
- void preorder(struct node* temp){
- if (!temp)
- return;
- cout << temp -> data << " ";
- preorder(temp -> left);
- preorder(temp -> right);
- }
- void postorder(struct node* temp){
- if (!temp){
- return;
- }
- postorder(temp -> left);
- postorder(temp -> right);
- cout << temp -> data << " ";
- }
- void del(int key){
- node* p = NULL;
- node* t = root;
- while(t!=NULL && t->data != key){
- p = t;
- if(key<t->data)
- t = t->left;
- else if(key> t->data)
- t = t->right;
- }
- if(t==NULL){
- cout<<"Data Not found"<<endl;
- return;
- }
- if(t->left == NULL && t->right == NULL ){
- if(p!=NULL){
- if(p->left == t)
- p->left = NULL;
- else if(p->right == t)
- p->right = NULL;
- }
- else{
- root = NULL;
- }
- }
- else if(t->left == NULL && t->right != NULL ){
- if(p!=NULL){
- if(p->left == t)
- p->left = t->right;
- else if(p->right == t)
- p->right = t->right;
- }
- else{
- root = t->right;
- }
- }
- else if(t->left != NULL && t->right == NULL ){
- if(p!=NULL){
- if(p->left == t)
- p->left = t->left;
- else if(p->right == t)
- p->right = t->left;
- }
- else{
- root = t->left;
- }
- }
- else if(t->left != NULL && t->right != NULL ){
- node* inSuc = t->right;
- while(inSuc->left !=NULL)
- inSuc = inSuc->left;
- int val = inSuc->data;
- del(inSuc->data);
- t->data = val;
- }
- }
- /* void del(int key){
- node *parent = NULL;
- node *t = root;
- while(t != NULL && t -> data != key){
- if(key < t -> data){
- parent = t;
- t = t -> left;
- }
- else if(key > t -> data){
- t = t -> right;
- }
- }
- if(t == NULL){
- cout << "Data not there" << endl;
- }
- else if(t -> left == NULL && t -> right == NULL){
- if(parent != NULL){
- if(parent -> left == t){
- parent -> left = NULL;
- }
- else if(parent -> right == t){
- parent -> right = NULL;
- }
- }
- }
- else if(t -> left == NULL && t -> right != NULL ){
- if(parent != NULL){
- if(parent -> left == t)
- parent -> left = t -> right;
- else if(parent -> right == t)
- parent -> right = t -> right;
- }
- else{
- root = t -> right;
- }
- }
- else if(t -> left != NULL && t -> right == NULL ){
- if(parent != NULL){
- if(parent -> left == t)
- parent -> left = t -> left;
- else if(parent -> right == t)
- parent -> right = t -> left;
- }
- else{
- root = t -> left;
- }
- }
- else if(t -> left != NULL && t -> right != NULL ){
- node* suc = t -> right;
- while(suc -> left != NULL)
- suc = suc -> left;
- int val = suc -> data;
- del(suc -> data);
- t -> data = val;
- }
- }
- */
- /*void del(int key){
- node* nn=new node;
- nn->left=nn->right=NULL;
- node* p=NULL;
- node* t=root;
- while(t!=NULL && t->data!=key){
- p=t;
- if(key>t->data){
- t=t->right;
- }
- else if(key<t->data){
- t=t->right;
- }
- }
- if(t==NULL){
- cout<<"data not found\n";
- }
- else{
- if(key<p->data){
- p->left=nn;
- }
- else if(key>p->data){
- p->right=nn;
- }
- }
- free(t);
- }
- */
- };
- int main(){
- BST b;
- int ch = 10;
- while(ch != 0){
- cout << "\nEnter your choices: " << endl;
- cout << "1: Add data" << endl;
- cout << "2: Display inorder" << endl;
- cout << "3: Display preorder" << endl;
- cout << "4: Display postorder" << endl;
- cout << "5: Delete data" << endl;
- cout << "0: Exit" << endl;
- cin >> ch;
- switch(ch){
- case 1:
- int key;
- cout << "Enter the key" << endl;
- cin >> key;
- b.insert(key);
- break;
- case 2:
- b.inorder(b.getroot());
- break;
- case 3:
- b.preorder(b.getroot());
- break;
- case 4:
- b.postorder(b.getroot());
- break;
- case 5:
- cout << "Enter data to delete" << endl;
- int k;
- cin >> k;
- b.del(k);
- break;
- }
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement