448,501 Members | 1,266 Online
Need help? Post your question and get tips & solutions from a community of 448,501 IT Pros & Developers. It's quick & easy.

# simple question about B tree

 P: n/a Hi I am relearning datastructure... but got kinda stuck in a basic delete node operation. what it does is to delete the first node it finds when the item is equal the input item. the end result is that the node is deleted and the b-tree is still kept as b-tree. CAn someone critique on my implementation? Any suggestion for improvement? Thanks The code I have as follows: #include using namespace std; class Btree; class Node { private: int value; Node* left; Node* right; friend Btree; public: Node(int item): value(item),left(0),right(0){}; ~Node(){cout<<"value is :"<left) &&(!cur->right) } } //Helper functions int Btree::swap(Node* node1, Node* node2){ int ret=0; if(!node1 || !node2){ ret =0;} else { int temp = node2->value; node2->value=node1->value; node1->value=temp; ret =1; } return ret; }; #include using namespace std; class Btree; class Node { private: int value; Node* left; Node* right; friend Btree; public: Node(int item): value(item),left(0),right(0){}; ~Node(){cout<<"value is :"<left) &&(!cur->right) } } int Btree::swap(Node* node1, Node* node2){ int ret=0; if(!node1 || !node2){ ret =0;} else { int temp = node2->value; node2->value=node1->value; node1->value=temp; ret =1; } return ret; }; int Btree::removeNode(Node* cur, int item){ int ret=0; if(!cur) { ret =0;} else { if(cur->value == item) { /*get rid of node*/ // if there is a left child // keep promoting the left child up as long as it has one. if (isLeafNode(cur)){ delete cur;} // if I am leaf node delete myself else { // do I have a left node? yes promote, // get rid of the last one in chain while (cur->left) { swap(cur,cur->left); cur=cur->left; } while (cur->right) { swap(cur,cur->right); cur=cur->right; } //should be leaf node pointed by cur if(isLeafNode(cur)){delete cur;} // no promote right node // get rid of the last one in chain } } else{ //if item < cur->value, tell left to do the deletion, // else tell right if (itemvalue) removeNode(cur->left,item); else removeNode(cur->right,item); } } return ret; } Nov 28 '06 #1