二叉排序树
2013-01-29 01:47
190 查看
二叉排序树也即二叉查找树,写的过程中指针的传参问题在一次凸显,这里我采用的是指针引用。
#include <iostream> using namespace std; struct BinaryTreeNode { int data; BinaryTreeNode *lchild,*rchild; }; class BinarySearchTree { private: BinaryTreeNode *root; public: BinarySearchTree():root(NULL) {} //构造函数 void Insert(BinaryTreeNode *&t,int key); //插入元素 void PreOrder(BinaryTreeNode *t); //前序遍历 void InOrder(BinaryTreeNode *t); //中序遍历 void PostOrder(BinaryTreeNode *t); //后序遍历 bool Search(BinaryTreeNode *t,int key); //搜索元素 BinaryTreeNode *GetRoot() { return root;} //获取根节点 void DelNode(BinaryTreeNode *&t,int key); //删除key元素 void Del(BinaryTreeNode *&t); //删除 }; void BinarySearchTree::Insert(BinaryTreeNode *&t,int key) { if(!t) { BinaryTreeNode *NewNode=new BinaryTreeNode; NewNode->data=key; NewNode->rchild=NewNode->lchild=NULL; t=NewNode; return; } BinaryTreeNode *p,*prev=0; p=t; while(p) { prev=p; if(key<p->data) p=p->lchild; else if(key==p->data) { cout<<"树中已有该节点!"<<endl; return ;} else if(key>p->data) p=p->rchild; } if(key<prev->data) { BinaryTreeNode *NewNode=new BinaryTreeNode; NewNode->data=key; NewNode->rchild=NewNode->lchild=NULL; prev->lchild=NewNode; } else if(key>prev->data) { BinaryTreeNode *NewNode=new BinaryTreeNode; NewNode->data=key; NewNode->rchild=NewNode->lchild=NULL; prev->rchild=NewNode; } } void BinarySearchTree::PreOrder(BinaryTreeNode *t) { if(t) { cout<<t->data<<" "; PreOrder(t->lchild); PreOrder(t->rchild); } } void BinarySearchTree::InOrder(BinaryTreeNode *t) { if(t) { InOrder(t->lchild); cout<<t->data<<" "; InOrder(t->rchild); } } void BinarySearchTree::PostOrder(BinaryTreeNode *t) { if(t) { PostOrder(t->lchild); PostOrder(t->rchild); cout<<t->data<<" "; } } bool BinarySearchTree::Search(BinaryTreeNode *t,int key) { if(!t) { cout<<"Not find!"<<endl; return false; } else if(key<t->data) return Search(t->lchild,key); else if(key>t->data) return Search(t->rchild,key); else if(key==t->data) { cout<<"Has been found!"<<endl; return true;} } void BinarySearchTree::Del(BinaryTreeNode *&t) { BinaryTreeNode *del=NULL,*p=NULL; if(!t->rchild) //如果右子树为空 { del=t; t=t->lchild; delete del; } else if(!t->lchild) //如果左子树为空 { del=t; t=t->rchild; delete del; } else if(t->lchild&&t->rchild) //存在左右子女 { p=t; del=t->lchild; while(!del->rchild) { p=del; del=del->rchild; } t->data=del->data; if(p!=t) p->rchild=del->lchild; else p->lchild=del->lchild; delete del; } } void BinarySearchTree::DelNode(BinaryTreeNode *&t,int key) { if(!t) return ; if(key==t->data) Del(t); else if(key<t->data) DelNode(t->lchild,key); else if(key>t->data) DelNode(t->rchild,key); } int main() { BinarySearchTree b; BinaryTreeNode *root=NULL; int a[10]={4,66,3,77,-4,7,88,56,93,0}; cout<<"二叉搜索树测试"<<endl; for(int i=0;i<10;++i) b.Insert(root,a[i]); cout<<"前序遍历"<<endl; b.PreOrder(root); cout<<endl; cout<<"中序遍历"<<endl; b.InOrder(root); cout<<endl; cout<<"后序遍历"<<endl; b.PostOrder(root); cout<<endl; cout<<"搜索测试,搜索0"<<endl; b.Search(root,0); cout<<"搜索测试,搜索13"<<endl; b.Search(root,13); cout<<"删除测试,删除93"<<endl; b.DelNode(root,93); cout<<"已删除93"<<endl; cout<<"删除后结果"<<endl; b.InOrder(root); cout<<"插入测试"<<endl; cout<<"插入6后!"<<endl; b.Insert(root,6); b.InOrder(root); system("pause"); return 0; }
相关文章推荐
- 二叉排序树
- pascal语言实现二叉排序树
- 二叉排序树
- 二叉排序树构造学生管理系统
- BinarySortTree二叉排序树(2010年12月07日)
- 数据结构与算法学习之二叉排序树及二叉排序树的相关操作
- 1404 二叉排序树中查找
- 二叉查找树(二叉排序树)的详细实现
- 二叉排序树的创建
- 二叉排序树经典算法速成:”二叉排序树“。
- 树表查找之二叉排序树
- 二叉排序树非升序排序数列
- 二叉排序树
- 二叉树的各种操作 先序 中序 后续 层次 遍历 求树高度 节点深度 知先序中序求后续 二叉排序树
- 二叉排序树的相关操作
- VC++2012编程演练数据结构《12》二叉排序树
- 二叉排序树的实现
- 二叉排序树删除操作
- 动态查找表之二叉排序树
- 1367判断一个数组是否为二叉排序树的后序遍历结果