/* bintree.h CIS 250 2012-04-11 Dave Klick Templated binary search tree implementation */ #ifndef __BINTREE_H__ #define __BINTREE_H__ #include using std::ostream; using std::cout; template struct Node { public: // If allowing duplicate values by keeping a count in the node, // you will need to add a private integer count variable to the node. // You will also need to add methods to increment and decrement // the count. The insertion operator overload should also be // modified to display the count. T data; Node* left; Node* right; Node() { left = right = NULL; } Node(const T thingie) { left = right = NULL; data = thingie; } T getData() const { return data; } friend ostream& operator<<(ostream& strm, const Node& node) { return strm << node.data; } }; template class bintree { private: Node* root; void inOrder(const Node* p) const; void preOrder(const Node* p) const; void postOrder(const Node* p) const; void deleteTree(Node*); public: bintree() { root = NULL; } ~bintree(); bool deleteNode(const T&); bool insert(const T&); bool find(const T&) const; void inOrder() const { inOrder(root); } void preOrder() const { preOrder(root); } void postOrder() const { postOrder(root); } }; template void bintree::deleteTree(Node* p) { if (p != NULL) { if (p->left) deleteTree(p->left); if (p->right) deleteTree(p->right); delete p; } } template void bintree::inOrder(const Node* p) const { if (p != NULL) { inOrder(p->left); cout << *p << '\n'; inOrder(p->right); } } template void bintree::preOrder(const Node* p) const { if (p != NULL) { cout << *p << '\n'; preOrder(p->left); preOrder(p->right); } } template void bintree::postOrder(const Node* p) const { if (p != NULL) { postOrder(p->left); postOrder(p->right); cout << *p << '\n'; } } template bintree::~bintree() { deleteTree(root); root = NULL; } template bool bintree::deleteNode(const T& data) { Node *parent, *delNode, *succ, *parentSucc; bool isLeftChild = true; if (root == NULL) return false; // find node to be deleted parent = NULL; delNode = root; while (data != delNode->data) { parent = delNode; if (data < delNode->data) { if (delNode->left == NULL) return false; delNode = delNode->left; isLeftChild = true; } else { if (delNode->right == NULL) return false; delNode = delNode->right; isLeftChild = false; } } // If a count is being stored in the node for duplicates, // then you would check that count here and decrement that // count and return true if it is greater than 1. Otherwise, // you would continue on with the code below. if (delNode->left == NULL && delNode->right == NULL) { // do delete for node with no children if (delNode == root) root = NULL; else if (isLeftChild) parent->left = NULL; else parent->right = NULL; } else if (delNode->right == NULL || delNode->left == NULL) { // do delete for node with one child succ = delNode->right == NULL ? delNode->left : delNode->right; if (delNode == root) root = succ; else if (isLeftChild) parent->left = succ; else parent->right = succ; } else { // do delete for node with two children parentSucc = delNode; succ = delNode->right; // once to right while (succ->left != NULL) { // left as far as possible parentSucc = succ; succ = succ->left; } if (delNode == root) root = succ; else if (isLeftChild) parent->left = succ; else parent->right = succ; if (parentSucc != delNode) parentSucc->left = succ->right; succ->left = delNode->left; if (delNode->right != succ) succ->right = delNode->right; } delete delNode; return true; } template bool bintree::insert(const T& data) { Node *newNode, *prev, *curr; newNode = new Node(data); if (root == NULL) root = newNode; else { curr = root; while (curr != NULL) { prev = curr; // If duplicates are being allowed, then you have two // options for changing the code below. If the duplicates // are stored as unique nodes, then you can add the new // node as either a predecessor or successor by eliminating // the equality test and adding equality to either of the // other two tests. // If the duplicates are stored by increasing a count // stored in the node, then the action taken on equality // should be to increase that count and return true. If // this is the case, then don't forget to release // the newNode you created (or choose to only create it // if it is needed). if (data == curr->data) return false; else if (data < curr->data) curr = curr->left; else curr = curr->right; } if (data < prev->data) prev->left = newNode; else prev->right = newNode; } return true; } template bool bintree::find(const T& data) const { Node *curr; bool found = false; if (root != NULL) { curr = root; while (curr != NULL && !found) { if (data == curr->data) found = true; else if (data < curr->data) curr = curr->left; else curr = curr->right; } } return found; } #endif