数据结构二叉查找树C++实现
来源:互联网 发布:js如何创建数组 编辑:程序博客网 时间:2024/05/29 07:50
#include <iostream>using namespace std;template<class Comparable>class BinarySearchTree{public: BinarySearchTree(); BinarySearchTree(const BinarySearchTree& rhs); //copy constructor ~BinarySearchTree(); //destructorpublic: const Comparable& findMin() const; //find the minimum value const Comparable& findMax() const; //find the maximum value bool contains(const Comparable& x) const; //Determine whether the tree contains x bool isEmpty() const; //Determine whether the tree is empty void printTree() const; //print the binary Tree void makeEmpty(); //make binary tree empty void insert(const Comparable& x); //insert a value into binary tree void remove(const Comparable& x); //remove a value from binary tree const BinarySearchTree& operator= (const BinarySearchTree& rhs); //overload operator =private: struct BinaryNode { Comparable element; BinaryNode* left; //a pointer to left child BinaryNode* right; // a pointer to right child BinaryNode(){} BinaryNode(const Comparable& theElem, BinaryNode* lt, BinaryNode* rt) : element(theElem), left(lt), right(rt){}//constructor }; //the binary tree node type BinaryNode* root; //define the root node void insert(const Comparable& x, BinaryNode *&t) const; void remove(const Comparable& x, BinaryNode *&t) const; BinaryNode* findMin(BinaryNode* t) const;BinaryNode* findMax(BinaryNode* t) const; bool contains(const Comparable& x, BinaryNode* t) const; void printTree(BinaryNode* t) const; void makeEmpty(BinaryNode *&t) const; BinaryNode* clone(BinaryNode *t) const;};template<class Comparable>BinarySearchTree<Comparable>::BinarySearchTree(){ root = new BinaryNode(); //constructor construct a tree without subtree root = NULL;}template<class Comparable>BinarySearchTree<Comparable>::BinarySearchTree(const BinarySearchTree& rhs){ root = new BinaryNode(); root = NULL; makeEmpty(); root = clone(rhs.root);}template<class Comparable>BinarySearchTree<Comparable>::~BinarySearchTree(){ makeEmpty();}template<class Comparable>const Comparable& BinarySearchTree<Comparable>::findMin() const{ return findMin(root)->element;}template<class Comparable>const Comparable& BinarySearchTree<Comparable>::findMax() const{ return findMax(root)->element;}template<class Comparable>bool BinarySearchTree<Comparable>::contains(const Comparable& x) const{ return contains(x, root);}template<class Comparable>bool BinarySearchTree<Comparable>::isEmpty() const{ return root == NULL;}template<class Comparable>void BinarySearchTree<Comparable>::printTree() const{ printTree(root);}template<class Comparable>void BinarySearchTree<Comparable>::makeEmpty(){ makeEmpty(root);}template<class Comparable>void BinarySearchTree<Comparable>::insert(const Comparable& x){ insert(x, root);}template<class Comparable>void BinarySearchTree<Comparable>::remove(const Comparable& x){ remove(x, root);}template<class Comparable>const BinarySearchTree<Comparable>& BinarySearchTree<Comparable>::operator= (const BinarySearchTree& rhs){ if(this != &rhs) { makeEmpty(); root = clone(rhs.root); } return *this;}template<class Comparable>void BinarySearchTree<Comparable>::insert(const Comparable& x, BinaryNode *&t) const{ if(t == NULL) t = new BinaryNode(x, NULL, NULL); else if(x < t -> element) insert(x, t -> left); else if(t -> element < x) insert(x, t -> right); else ; //do nothing}template<class Comparable>void BinarySearchTree<Comparable>::remove(const Comparable& x, BinaryNode *&t) const{ if (t == NULL) return; if(x < t -> element) remove(x, t -> left); else if(t -> element < x) remove(x, t -> right); else if(t -> left != NULL && t -> right != NULL) { t -> element = findMin(t -> right) -> element; remove(t->element, t->right); } else { BinaryNode *oldNode = t; t = (t -> left != NULL) ? t -> left : t -> right; delete oldNode; }}template<class Comparable>typename BinarySearchTree<Comparable>::BinaryNode* BinarySearchTree<Comparable>::findMin(BinaryNode *t) const{ if(t == NULL) return NULL; if(t -> left == NULL) return t; return findMin(t -> left);}template<class Comparable>typename BinarySearchTree<Comparable>::BinaryNode* BinarySearchTree<Comparable>::findMax(BinaryNode *t) const{ if(t == NULL) return NULL; if(t -> right == NULL) return t; return findMax(t -> right);}template<class Comparable>bool BinarySearchTree<Comparable>::contains(const Comparable& x, BinaryNode* t) const{ if(t == NULL) return false; else if(x < t -> element) return contains(x, t -> left); else if(t -> element < x) return contains(x, t -> right); else return true;}template<class Comparable>void BinarySearchTree<Comparable>::makeEmpty(BinaryNode*& t) const{ if(t != NULL) { makeEmpty(t -> left); makeEmpty(t -> right); delete t; } t = NULL;}template<class Comparable>void BinarySearchTree<Comparable>::printTree(BinaryNode* t) const{ if(t != NULL) { printTree(t -> left); cout << t ->element << "\t"; printTree(t -> right); }}template<class Comparable>typename BinarySearchTree<Comparable>::BinaryNode* BinarySearchTree<Comparable>::clone(BinaryNode *t) const{ if(t == NULL) return NULL; return new BinaryNode(t -> element, clone(t -> left), clone(t -> right));}int main(){ return 0;}
0 0
- C 实现数据结构二叉查找树
- 数据结构:二叉查找树(C语言实现)
- 数据结构——二叉查找树的详细实现(c++)
- 【二叉查找树】c实现
- 数据结构二叉查找树C++实现
- 数据结构--二叉查找树的java实现
- Java实现数据结构之二叉查找树
- [数据结构]二叉查找树 简单实现
- 数据结构:JavaScript实现二叉查找树
- 数据结构:二叉数查找树基本实现
- 数据结构实战java实现二叉查找树
- 数据结构之Java实现二叉查找树
- 二叉查找树 - C语言实现(摘自数据结构与算法分析 C语言描述)
- 二叉查找树 - C语言实现(摘自数据结构与算法分析 C语言描述)
- 【学习点滴-数据结构-二叉树】二叉查找树源码实现
- 数据结构:二叉查找树
- 数据结构--二叉查找树
- 【数据结构】二叉查找树
- 数据结构队列C++实现 链式和循环
- XE7 BCB不带Midas.dll发布的方法
- 修改linux主机名称
- pow(x, n),有时递归的时候要注意用变量保存中间结果
- [ZedBoard移植嵌入式Linux教程(4)]创建设备树(Device-Tree)文件dts
- 数据结构二叉查找树C++实现
- Codeforces 6C. Alice, Bob and Chocolate
- 【LeetCode】Combinations 解题报告
- android SlidingMenu sample的j简单写法
- C++如何合并若干个vector
- C++多态的实现原理
- lua实现sleep
- iOS_版本用户更新
- Android手机耗电深度解析:3G耗电是WiFi四倍