二叉查找树 BinarySearchTree 的实现
2010-11-06 00:06
796 查看
header.h
BST.h
template<class type> class BinarySearchTree { private: struct node { type data; node *left; node *right; node(type a,node*lt=NULL,node *rt=NULL){data=a;left=lt;right=rt;} }; node *root; bool find(type x,node *t); void insert(type x,node *&t); void remove(type x,node *&t); public: BinarySearchTree(node *t=NULL){root=t;} bool find(type x); void insert(type x); void remove(type x); void makeEmpty(node *&root); ~BinarySearchTree(){makeEmpty(root);} }; //================================== template<class type> bool BinarySearchTree<type>::find(type x) { return find(x,root); } template<class type> bool BinarySearchTree<type>::find(type x,node *t) { if(t==NULL) return false; else if(x>t->data) return find(x,t->right); else if(x<t->data) return find(x,t->left); else return true; } //================================== template<class type> void BinarySearchTree<type>::insert(type x) { insert(x,root); } template<class type> void BinarySearchTree<type>::insert(type x,node *&t) { if(t==NULL){t=new node(x); return;} else if(x<t->data) insert(x,t->left); else if(x>t->data) insert(x,t->right); } //================================== template<class type> void BinarySearchTree<type>::remove(type x) { remove(x,root); } template<class type> void BinarySearchTree<type>::remove(type x,node *&t) { if(t==NULL){cout<<x<<" doesn't exist!"<<endl; return;} if(x<t->data) remove(x,t->left); else if(x>t->data) remove(x,t->right); else if(t->left!=NULL && t->right!=NULL) { node *p=t->right; while(p->left!=NULL) p=p->left; t->data=p->data; remove(t->data,t->right); } else { node* p=t; t=(t->left!=NULL)?t->left:t->right; delete p; } } //================================== template<class type> void BinarySearchTree<type>::makeEmpty(node *&root) { if(root==NULL) return; else if(root->left==NULL && root->right==NULL) delete root; else { makeEmpty(root->left); makeEmpty(root->right);} }
BST.h
#include <iostream> #include "header.h" using namespace std; int main() { int i=0; int a[]={10,8,6,21,87,56,4,0,11,3,22,7,5,34,1,2,9}; BinarySearchTree<int> bst; int len=sizeof(a)/sizeof(int); for(i=0;i<len;i++) bst.insert(a[i]); cout<<"find 2 is "<<(bst.find(2)?"true":"false")<<endl; bst.remove(2); cout<<"after remove 2,find 2 is "<<(bst.find(2)?"true":"false")<<endl; cout<<"find 3 is "<<(bst.find(3)?"true":"false")<<endl; bst.remove(3); cout<<"after remove 3,find 3 is "<<(bst.find(2)?"true":"false")<<endl; bst.remove(123); return 0; }
相关文章推荐
- 二叉查找树的原理与实现 Binary Search Tree
- 基于数组的二叉查找树 Binary Search Tree (Java实现)
- BST(Binary Search Tree,二叉查找树,二叉排序树)c的实现(部分函数不知如何调用)
- BinarySearchTree(1)二叉查找树链式递归实现
- BinarySearchTree(2)二叉查找树链式非递归实现
- PAT04-树6 Complete Binary Search Tree 【JAVA实现】
- BinarySearchTree查找二叉树独立实现
- 【LeetCode-面试算法经典-Java实现】【109-Convert Sorted List to Binary Search Tree(排序链表转换成二叉排序树)】
- Leetcode 98 Validate Binary Search Tree 验证二叉查找树
- Convert Sorted List to Binary Search Tree ------C++ 递归创建平衡二叉查找树
- LintCode Insert a Node in a Binary Search Tree 在二叉查找树中插入节点
- BST(Binary Search Tree 二叉查找树模版)
- 235. Lowest Common Ancestor of a Binary Search Tree | 二叉查找树两个节点的最低共同祖先
- BinarySearchTree Implements(Java 实现的二叉搜索树算法)
- lintcode 中等题:unique Binary Search Tree 不同的二叉查找树
- Validate Binary Search Tree--判断一个树是不是二叉查找树(重重重)
- PAT程序设计考题——甲级1043(Is It a Binary Search Tree ) C++实现
- 二叉查找树(binary search tree (BST))--算法导论示例
- Leetcode 108 Convert Sorted Array to Binary Search Tree 有序数组构造平衡二叉查找树
- 泛型Binary Search Tree实现,And和STL map比较的经营业绩