| | |
Binary search tree removal
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 20
Reputation:
Solved Threads: 0
I am having trouble coming up with code for removing a node in a binary search tree. This is what I have so far:
The problem I am having is lets say for example I enter
10 8 12 11 15
If I remove the 15 it works fine. When I go back and try to remove the 11 after I remove the fifteen it gives me a segmentation fault. Any help would be appreciated.
C++ Syntax (Toggle Plain Text)
void remove(int n) { node *current=root; node *gptr; while(current != NULL){ if(n<current->key){ gptr=current; current=current->left; }else if(n>current->key){ gptr=current; current=current->right; }else if(n==current->key){ if(gptr->right->key==n){ cout<<"1st ==key = "<<gptr->key<<endl; cout<<"1st ==current = "<<current->key<<endl; delete current; gptr->right=NULL; }else if(gptr->left->key==n){ cout<<"1st ==key = "<<gptr->key<<endl; cout<<"1st ==current = "<<current->key<<endl; delete current; gptr->left=NULL; } } } }
The problem I am having is lets say for example I enter
10 8 12 11 15
If I remove the 15 it works fine. When I go back and try to remove the 11 after I remove the fifteen it gives me a segmentation fault. Any help would be appreciated.
•
•
Join Date: Nov 2004
Posts: 20
Reputation:
Solved Threads: 0
I have a root node.Here is the entire code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <cstdlib> #include <string> using namespace std; class node { public: int key; string value; node *left; node *right; node(int k, string v) { key = k; value = v; left = NULL; right = NULL; } }; class bst { private: node *root; public: bst() { root = NULL; } void put(int key, string value) { node *newp = new node(key,value); if(root != NULL) put(key, value, root); else { root=newp; } } void put(int key, string value, node *tmp) { node *newp = new node(key,value); if(key<tmp->key) { if(tmp->left != NULL) put(key,value,tmp->left); else { tmp->left=newp; } } else if(key>tmp->key) { if(tmp->right != NULL) put(key,value,tmp->right); else { tmp->right=newp; } } } string non_recursive_get(int n) { node *gptr=root; while(gptr != NULL){ if(n<gptr->key){ gptr=gptr->left; }else if(n>gptr->key){ gptr=gptr->right; }else if(n==gptr->key){ return gptr->value; }else{ return "no node"; } } } string recursive_get(int n) { recursive_get(n,root); return recursive_get(n,root); } string recursive_get(int n, node *tmp) { if(tmp!=NULL){ if(n==tmp->key){ return tmp->value; } else if(n<tmp->key){ return recursive_get(n,tmp->left); }else if(n>tmp->key){ return recursive_get(n, tmp->right); } }else if(tmp==NULL){ cout<<"NO NODES IN TREE"<<endl; } } void remove(int n) { node *current=root; node *gptr; while(current != NULL){ if(n<current->key){ gptr=current; current=current->left; }else if(n>current->key){ gptr=current; current=current->right; }else if(n==current->key){ if(gptr->right->key==n){ cout<<"1st ==key = "<<gptr->key<<endl; cout<<"1st ==current = "<<current->key<<endl; delete current; gptr->right=NULL; }else if(gptr->left->key==n){ cout<<"1st ==key = "<<gptr->key<<endl; cout<<"1st ==current = "<<current->key<<endl; delete current; gptr->left=NULL; } } } } }; int main(void) { bst mybst; string cmd; int k; string v; while (true) { cin >> cmd >> k >> v; cout << "MAIN: cmd= " << cmd << ", key= " << k << ", v= " << v << endl; if (cmd == "put") { mybst.put(k, v); }else if (cmd == "dump") { // traverse tree in ascending order mybst.dump(); }else if (cmd == "dump_rev") { // traverse tree in descending order mybst.dump_rev(); } else if (cmd == "get") { cout << "MAIN: get returns: " << mybst.non_recursive_get(k) << endl; } else if (cmd == "rget") { cout << "MAIN: rget returns: " << mybst.recursive_get(k) << endl; } else if (cmd == "rem") { mybst.remove(k); } else if (cmd == "quit") { exit(0); } } }
To remove a node with two children you need to find the inorder successor or predecessor to replace it with, then reduce to the case of removing the successor or predecessor. A fairly thorough tutorial on binary search trees can be found here.
I'm here to prove you wrong.
![]() |
Similar Threads
- Reading a file into a binary search tree (C++)
- Binary Search Tree (C++)
- searching and inserting node in a binary search tree (C)
- recursive findaverage function for Binary search tree (C)
- Insertion in a binary search tree (C++)
Other Threads in the C++ Forum
- Previous Thread: Help: Outputting Up and down arrows in C++
- Next Thread: Problems With Loops...
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






