二叉查找树的一些实现,一些心得都嵌入其中的注释
- #include "iostream"
- using namespace std;
- //因为查找树左小右大
- template <class T>
- class BinarySearchTree
- {
- public:
- BinarySearchTree(); //构造
- BinarySearchTree(const BinarySearchTree &rhs); //复制构造
- ~BinarySearchTree();
- const BinarySearchTree &operator=(const BinarySearchTree &rhs);
- const T &findMin()const; //找打最小element那个结点及其值
- const T &findMax()const; //找打最大element那个结点及其值
- bool contains(const T &x); //其中有无此element的结点,有返回true,否则返回false;
- bool isempty() const; //判断是否为空
- void printTree() const; //打印树,遍历显示
- void makeEmpty(); //置空
- void insert(const T &x); //插入结点
- void remove(const T &x); //删除结点
- private:
- struct BinaryNode
- {
- T element;
- BinaryNode *leftChild;
- BinaryNode *rightChild;
- BinaryNode(const T &elem, BinaryNode *lt, BinaryNode *rt)
- :element(elem), leftChild(lt), rightChild(rt){} //结点复制构造时使用
- };
- BinaryNode *root;
- void insert(const T &x, BinaryNode * &root) const; //指向引用的指针
- void remove(const T &x, BinaryNode * &root) const; //当要进行内存分配的时候,利用new 或者malloc 时用 "* &t"
- BinaryNode *findMin(BinaryNode *root) const;
- BinaryNode *findMax(BinaryNode *root) const;
- bool contains(const T &x, BinaryNode *root) const;
- void makeEmpty(BinaryNode * &root);
- void printTree(BinaryNode * root) const;
- BinaryNode *clone(BinaryNode *root) const; //复制返回的根节点
- };
- template <class T>
- BinarySearchTree<T>::BinarySearchTree()
- {
- root = NULL;
- }
- template <class T>
- BinarySearchTree<T>::BinarySearchTree(const BinarySearchTree &rhs)
- {
- operator=(rhs);
- }
- template <class T>
- BinarySearchTree<T>::~BinarySearchTree()
- {
- //delete root;
- }
- template <class T>
- const BinarySearchTree<T> &BinarySearchTree<T>::operator=(const BinarySearchTree &rhs)
- {
- if(this = &this)
- return *this;
- delete root;
- root = new BinaryNode;
- root = rhs.root;
- return *this;
- }
- template <class T>
- bool BinarySearchTree<T>::isempty() const
- {
- if(!root)
- return true;
- else
- return false;
- }
- template <class T>
- const T &BinarySearchTree<T>::findMin()const
- {
- T _min;
- BinaryNode *minNode = new BinaryNode;
- minNode = findMin(root);
- _min = minNode.element;
- return _min;
- }
- template <class T>
- const T &BinarySearchTree<T>::findMax()const
- {
- T _max;
- BinaryNode *maxNode = new BinaryNode;
- maxNode = findMax(maxNode);
- _max = maxNode.element;
- return _max;
- }
- //声明这个是类型typename
- template <class T>
- typename BinarySearchTree<T>::BinaryNode *BinarySearchTree<T>::findMin(BinaryNode *root) const
- {
- if(root)
- {
- if(root->leftChild)
- {
- return findMin(root->leftChild);
- }
- else
- return root;
- }
- else
- return root;
- }
- template <class T>
- typename BinarySearchTree<T>::BinaryNode *BinarySearchTree<T>::findMax(BinaryNode *root) const
- {
- if(root)
- {
- if(root->rightChild)
- {
- return findMax(root->rightChild); //返回这个小的结点
- }
- else
- return root;
- }
- else
- return root;
- }
- template <class T>
- bool BinarySearchTree<T>::contains(const T &x)
- {
- return contains(x, root);
- }
- template <class T>
- bool BinarySearchTree<T>::contains(const T &x, BinaryNode *root) const
- {
- if(!root)
- return false;
- else if(x < root->element)
- return contains(x, root->leftChild);
- else if(x > root->element)
- return contains(x, root->rightChild);
- else
- return true;
- }
- template <class T>
- void BinarySearchTree<T>::insert(const T &x)
- {
- insert(x, root);
- }
- template <class T>
- void BinarySearchTree<T>::insert(const T &x, BinaryNode * &root) const //指向引用的指针
- {
- if(!root)
- {
- root = new BinaryNode(x, NULL, NULL); //那个在结点中构造函数的作用,哈哈
- }
- if(root && x !=root->element)
- {
- if(x < root->element)
- insert(x, root->leftChild);
- else
- insert(x, root->rightChild);
- }
- else
- return;
- }
- template <class T>
- void BinarySearchTree<T>::remove(const T &x)
- {
- remove(x, root);
- }
- //删除,要的那个结点,若左存在,从他的作结点中找出中序遍历的最后一个结点,替换这个;若左不存在,则用右替换
- template <class T>
- void BinarySearchTree<T>::remove(const T &x, BinaryNode * &root) const //当要进行内存分配的时候,利用new 或者malloc 时用 "* &t"
- {
- if(!root)
- return;
- else
- {
- if(x > root->element)
- {
- remove(x, root->rightChild);
- }
- else if(x < root->element)
- remove(x, root->leftChild);
- else if(NULL != root->leftChild && NULL != root->rightChild)
- {
- root->element = findMin(root->rightChild)->element;
- remove(root->element, root->rightChild);
- }
- else
- {
- BinaryNode *oldNode = root;
- root = (root->leftChild != NULL) ? root->leftChild : root->rightChild;
- delete oldNode;
- }
- }
- }
- template <class T>
- void BinarySearchTree<T>::makeEmpty()
- {
- makeEmpty(root);
- }
- template <class T>
- void BinarySearchTree<T>::makeEmpty(BinaryNode * &root)
- {
- if(!root)
- return;
- else
- {
- if(root->leftChild)
- makeEmpty(root->leftChild);
- if(root->rightChild)
- makeEmpty(root->rightChild);
- delete root;
- }
- }
- int main()
- {
- BinarySearchTree<int> binaryTree;
- binaryTree.insert(10);
- binaryTree.insert(5);
- binaryTree.insert(6);
- binaryTree.insert(12);
- binaryTree.insert(15);
- binaryTree.remove(6);
- cout << binaryTree.contains(12) << endl;
- cout << binaryTree.isempty() << endl;
- binaryTree.makeEmpty(); //注意在这里已经delete root一次, 那么在析构中就不应该delete 一次了
- return 0;
- }