I am writing a simple binary search tree (nodes are int nodes) with a BSTNode class and a BST class. I have followed the instructions from my C++ book, and now I am trying to get a remove method working. But before I get to the remove, I need to get my find method working. Basically, I am trying to get a "find" method working that will search for a giving int value, and return the node with that value. I have designed my current find with the help of my book, but for some off reason, when I run my program and try to find the values I do, it returns "10" (the root) everytime, and will not complete when I try to find a number thats not in the tree. Any help would be great!
Code:
#include <iostream> using std::cout; using std::endl; class BSTNode { friend class BST; public: BSTNode(int); int getValue(); void print(); private: int value; BSTNode *leftPtr; BSTNode *rightPtr; }; class BST { public: BST(); void insertNode(const int &); BSTNode find(const int); private: BSTNode *rootPtr; void insertNodeHelper(BSTNode**, const int &); BSTNode findHelper(BSTNode**, const int); }; /**** MAIN *****/ int main(void) { cout << "TESTING MY BSTNode CLASS:" << endl << "creating nodes with values 3, 4, and 5" << endl << endl; BSTNode node1 = BSTNode(3); cout << "node1 has value of: " << node1.getValue() << endl; BSTNode node2 = BSTNode(4); cout << "node2 has value of: " << node2.getValue() << endl; BSTNode node3 = BSTNode(5); cout << "node3 has value of: " << node3.getValue() << endl; cout << endl << "Using the print method, the values are:" << endl; node1.print(); node2.print(); node3.print(); cout << endl << endl << "TESTING MY BST CLASS:" << endl << "adding 10, 5, 15, 20, 8, and 10 to the tree" << endl; //Binary Search Tree BST tree = BST(); tree.insertNode(10); tree.insertNode(5); tree.insertNode(15); tree.insertNode(20); tree.insertNode(8); tree.insertNode(10); cout << endl << "testing the find method" << endl; cout << tree.find(10).getValue() << " "; tree.find(5).print(); tree.find(15).print(); tree.find(20).print(); return 0; } /*************** BSTNode Methods ****************/ //CONSTRUCTOR BSTNode::BSTNode(int d) : value(d), leftPtr(NULL), rightPtr(NULL) {} //METHODS int BSTNode::getValue() {return value;} void BSTNode::print() {cout << value << " ";} /*********** BST Methods ************/ //CONSTRUCTOR BST::BST() {rootPtr = NULL;} //INSERT METHOD void BST::insertNode(const int &value) {insertNodeHelper(&rootPtr, value);} void BST::insertNodeHelper(BSTNode **ptr, const int &value) { if (*ptr == NULL) *ptr = new BSTNode(value); else { if (value < (*ptr)->value) insertNodeHelper(&((*ptr)->leftPtr), value); else { if (value > (*ptr)->value) insertNodeHelper(&((*ptr)->rightPtr), value); else //SKIPS DUPLICATE NUMBERS cout << " " << value << " already exists in the tree... no duplicates will be added" << endl; } } } //FIND METHOD FOR USE WITH REMOVE BSTNode BST::find(const int value) {return findHelper(&rootPtr, value);} BSTNode BST::findHelper(BSTNode **ptr, const int value) { if (value == (*ptr)->value) return **ptr; else { if ((*ptr)->value > value) findHelper(&((*ptr)->leftPtr), value); else { if ((*ptr)->value < value) findHelper(&((*ptr)->rightPtr), value); else {cout << "node not found"; return BSTNode(-1);} } } }
Comment