赞
踩
二叉排序树又称二叉查找树,它或是一棵空的二叉树,或是具有下列性质的二叉树:
- template<class T>
- struct BiNode
- {
- T data;
- BiNode<T>*lchild,rchild;
- }
- class BiSortTree
- {
- public:
- BiSortTree(int a[],int n);
- ~BiSortTree();
- void InsertBST(BiNode<int>*root,BiNode<int>*s);
- void DeleteBST(BiNode<int>*p,BiNode<int>*f);
- BiNode<int>*SearchBST(BiNode<int>*root,int k);
- private:
- BiNode<int>*root;
- }
-
- void BiSortTree::InsertBST(BiNode<int>*root,BiNode<int>*s)
- {
- if(root==NULL)
- root=s;
- else if(s->data<root->data)
- InsertBST(root->lchild,s);
- else
- InsertBST(root->rchild,s);
- }
-
- BiSortTree::BiSortTree(int r[],int n)
- {
- for(int i=0;i<n;i++)
- {
- BiNode<int>s=new BiNode<int>;
- s->data=r[i];
- s->lchild=s->rchild=NULL;
- InsertBST(root,s);
- }
- }
- //在二叉排序树中删除一个节点f的左孩子节点p的算法:
- //1.若节点p是叶子,则直接删除节点p
- //2.若节点p只有左子树,则需重接p的左子树;若节点p只有右子树,则需重接p的右子树
- //3.若节点p的左右子树都不为空,则
- // 3.1查找节点p的右子树上的最左下节点s以及节点s的双亲节点par
- // 3.2将节点s的数据域替换到被删除节点p的数据域
- // 3.3若节点p的右孩子无左子树,则将s的右子树接到par的右子树上;否则将s的右子树接到节点par的左子树上
- // 3.4删除节点s;
- void BiSortTree::DeleteBST(BiNode<int>*p,BiNode<int>*f)
- {
- if((p->lchild==NULL)&&(p->rchild)==NULL)
- {
- f->lchild=NULL;
- delete p;
- }
- else if(p->rchild==NULL)
- {
- f->lchild=p->lchild;
- delete p;
- }
- else if(p->lchild==NULL)
- {
- f->lchild=p->rchild;
- delete p;
- }
- else{
- BiNode<int>*par=p;
- BiNode<int>*s=p->rchild;
- while(s->lchild!=NULL)
- {
- par=s;
- s=s->lchild
- }
- p->data=s->data;
- if(par==p)
- par->rchild=s->rchild;
- else
- par->lchild=s->rchild;
- delete s;
-
- }
-
- }
- BiNode<int>*BiSortTree::SearchBST(BiNode<int>*root,int k)
- {
- if(root==NULL)
- return NULL;
- else if(root->data==k)
- return root;
- else if(root->date>k)
- return SearchBST(root->lchild,k);
- else if(root->data<k)
- return SearchBST(root->rchild,k);
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。