二叉搜索树
![](https://images2017.cnblogs.com/blog/1078167/201709/1078167-20170904111242304-819522560.png)
#include <iostream> using namespace std; template <typename T> class BSTree; template <typename type> class BSTNode { friend class BSTree<type>; public: BSTNode() : data(type()), leftChild(NULL), rightChild(NULL) {} BSTNode(type d, BSTNode *left = NULL, BSTNode *right = NULL) : data(d), leftChild(left), rightChild(right) {} ~BSTNode() {} private: BSTNode *leftChild; BSTNode *rightChild; type data; }; template <typename T> class BSTree { public: BSTree() : root(NULL) { } BSTree(T *ar, int n) : root(NULL) { for (int i = 0; i < n; ++i) insert(ar[i]); } public: bool insert(const T &x) { return insert(root, x); } T& max() { return max(root); } const T& max() const { return max(root); } T& min() { return min(root); } const T& min() const { return min(root); } BSTNode<T>* search(const T &key) const { return search(root, key); } BSTNode<T >* parent(const T &key) const { return parent(root, key); } bool remove(const T &key) { return remove(root, key); } void makeEmpty() { return makeEmpty(root); } bool equal(const BSTree<T> &bst) const { return equal(root, bst.root); } void copy(const BSTree<T> &bst) { root = copy(bst.root); } void sortPrint() const { return sortPrint(root); } protected: BSTNode<T>* copy(BSTNode<T> *root) { if (root == NULL) return NULL; BSTNode<T> *p = new BSTNode<T>(root->data); p->leftChild = copy(root->leftChild); p->rightChild = copy(root->rightChild); return p; } bool equal(const BSTNode<T> *root1, const BSTNode<T> *root2) const { if (NULL == root1 && NULL == root2) return true; if (root1 != NULL && root2 != NULL && root->data == root->data && equal(root1->leftChild, root2->leftChild) && equal(root1->rightChild, root2->rightChild)) return true; return false; } bool remove(BSTNode<T> *&root, const T &key) { if (NULL == root) return false; if (root->data > key) remove(root->leftChild, key); else if (root->data < key) remove(root->rightChild, key); else { if (root->leftChild == NULL && root->rightChild == NULL) { delete root; root = NULL; } else if (root->leftChild != NULL && root->rightChild == NULL) { BSTNode<T> *p = root; root = p->leftChild; delete p; p = NULL; } else if (root->leftChild == NULL && root->rightChild != NULL) { BSTNode<T> *p = root; root = p->rightChild; delete p; p = NULL; } else //左右子树都存在,该节点右子树中最小的一个节点值替换当前节点值,并删除该最小节点 { BSTNode<T> *p = root->rightChild; while (p->leftChild != NULL) p = p->leftChild; root->data = p->data; remove(root->rightChild, p->data); } } } void makeEmpty(BSTNode < T> *&root) { if (NULL != root) { makeEmpty(root->leftChild); makeEmpty(root->rightChild); delete root; root = NULL; } } bool insert(BSTNode<T>* &t, const T &x) { if (NULL == t) { t = new BSTNode<T>(x); return true; } else if (t->data > x) return insert(t->leftChild, x); else if (t->data < x) return insert(t->rightChild, x); else //该数据值已存在 return false; } T& max(BSTNode<T> *root) { BSTNode<T> *p = root; while (p->rightChild != NULL) p = p->rightChild; return p->data; } const T& max(BSTNode<T> *root) const { BSTNode<T> *p = root; while (p->rightChild != NULL) p = p->rightChild; return p->data; } T& min(BSTNode<T> *root) { BSTNode<T> *p = root; while (p->leftChild != NULL) p = p->leftChild; return p->data; } const T& min(BSTNode<T> *root) const { BSTNode<T> *p = root; while (p->leftChild != NULL) p = p->leftChild; return p->data; } BSTNode<T>* search(BSTNode<T> * root, const T &key) const { if (NULL == root) return NULL; else if (key == root->data) return root; else if (root->data > key) return search(root->leftChild, key); else return search(root->rightChild, key); } BSTNode<T >* parent(BSTNode<T> *root, const T &key) const { BSTNode<T> *p = search(key); if (p == NULL) return NULL; if (root->leftChild == p || root->rightChild == p) return root; else if (root->data > p->data) return parent(root->rightChild, key); else if (root->data < p->data) return parent(root->leftChild, key); return NULL; } //BST中序遍历 void sortPrint(BSTNode<T> *root) const { if (root != NULL) { sortPrint(root->leftChild); cout << root->data << " "; sortPrint(root->rightChild); } } private: BSTNode<T> *root; };