#ifndef __BS_TREE_WITH_NODES_HPP__ #define __BS_TREE_WITH_NODES_HPP__ #include #include template class bnode { public: T data; bnode* left, * right; bnode() { left = right = NULL; } bnode(const T& x) { data = x; left = right = NULL;} bnode(const bnode& node2); // copy constructor ~bnode(); bool find(const T& key) const; void binsert(const T& key); bnode * bremove(const T& key); void inorder(std::ostream& out); T predecessor() const; }; template class btree { private: bnode* root; public: btree() { root = NULL; } btree(const btree& tree2); ~btree(); bool find(const T& key) const; void binsert(const T& key); void bremove(const T& key); void inorder(std::ostream& out); }; template btree::btree(const btree& tree2) { root = NULL; if (tree2.root != NULL) { root = new bnode(*(tree2.root)); } } template btree::~btree() { if (root != NULL) delete root; } template bnode::~bnode() { if (left != NULL) delete left; if (right != NULL) delete right; } template bnode::bnode(const bnode& node2) { data = node2.data; left = right = NULL; if (node2.left != NULL) { left = new bnode(*(node2.left)); } if (node2.right != NULL) { right = new bnode(*(node2.right)); } } template bool btree::find(const T& key) const { if (root == NULL) return false; return root->find(key); } template bool bnode::find(const T& key) const { if (data == key) return true; if (key <= data) { // search in the left subtree if (left == NULL) return false; else return left->find(key); } else { // search in the right subtree if (right == NULL) return false; else return right->find(key); } } template void btree::binsert(const T& key) { // tree is empty if (root == NULL) { root = new bnode(key); return; } root->binsert(key); } template void bnode::binsert(const T& key) { if (key <= data) { // insertion into left subtree if (left == NULL) { left = new bnode(key); } else { left->binsert(key); } } else { // insertion into right subtree if (right == NULL) { right = new bnode(key); } else { right->binsert(key); } } } template T bnode::predecessor() const { if (right == NULL) return data; else { return right->predecessor(); } } template void btree::bremove(const T& key) { if (find(key) == true) root->bremove(); } template bnode* bnode::bremove(const T& key) { if (key == data) { if (left == NULL && right == NULL) { delete this; return NULL; } if (left != NULL && right == NULL) // left subtree only { bnode* temp = left; left = NULL; // important delete this; return temp; } if (left == NULL && right != NULL) // right subtree only { bnode* temp = right; right = NULL; // important delete this; return temp; } if (left != NULL && right != NULL) { T x = left->predecessor(); data = x; left->bremove(x); return this; } } else { if (key <= data) { left = left->bremove(key); } else { right = right->bremove(key); } return this; } } template void btree::inorder(std::ostream& out) { if (root != NULL) root->inorder(out); } template void bnode::inorder(std::ostream& out) { if (left != NULL) left->inorder(out); out << " " << data; if (right != NULL) right->inorder(out); } #endif