coolComputer 2016-11-01 15:09 采纳率: 12.5%
浏览 981
已采纳

二叉排序树,求大神帮忙

#include
using namespace std;
template
struct BiNode{
DataType data;
BiNode *lchild;
BiNode *rchild;
};
template
class BiSortTree
{
public:
BiSortTree(DataType a[],int n);
~BiSortTree(){
DestoryBiSortTree(root);
}
void InsertBST(BiNode *s){
InsertBST(root,s);
}
void DeleteBST(BiNode *f){
DeleteBST(root,f);
}
BiNode *SearchBST(DataType k){
return SearchBST(root,k);
}
void Levershow();
void Inshow(){
Inshow(root);
}
void Preshow(){
Preshow(root);
}
private:
BiNode *root;
void DestoryBiSortTree(BiNode *bt);
void Preshow(BiNode *root);
void Inshow(BiNode *root);
void InsertBST(BiNode *root,BiNode *s);
void DeleteBST(BiNode *p,BiNode *f);
BiNode *SearchBST(BiNode *root,DataType k);
} ;
template
void BiSortTree::InsertBST(BiNode *root,BiNode *s)
{
if(root==NULL) root=s;
else if(root->data>s->data)
InsertBST(root->lchild,s);
else
InsertBST(root->rchild,s);
}
template
BiSortTree::BiSortTree(DataType a[],int n)
{
//root=new BiNode;
root=NULL;
BiNode *s;
for(int i=0;i s=new BiNode;
s->data=a[i];
s->lchild=NULL;
s->rchild=NULL;
InsertBST(root,s);
}
cout<<"完成构造函数" < }
template
void BiSortTree::DeleteBST(BiNode *p,BiNode *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->rchild==NULL){
f->lchild=p->rchild;
delete p;
}else{
BiNode *parent,*s;
parent=p;
s=p->rchild;
while(s->lchild!=NULL){
parent=s;
s=s->lchild;
}
p->data=s->data;
if(parent==p) parent->rchild=s->rchild;
else parent->lchild=s->rchild;
delete s;
}

}
template
void BiSortTree::DestoryBiSortTree(BiNode *bt)
{
if(bt!=NULL){
DestoryBiSortTree(bt->lchild);
DestoryBiSortTree(bt->rchild);
delete bt;
}
}
template
BiNode *BiSortTree::SearchBST(BiNode *root,DataType k)
{
if(root==NULL) return NULL;
else if(root->data==k) return root;
else if(root->datarchild,k);
else return SearchBST(root->lchild,k);
}
template
void BiSortTree::Levershow(){
cout<<"采用了层序遍历"< BiNode *Q[1000];
int rear=-1,front=-1;
if(root==NULL) return;
Q[++rear]=root;
BiNode *T;
while(rear!=front){
T=Q[++front];
cout<data<<" ";
if(T->lchild!=NULL) Q[++rear]=T->lchild;
if(T->rchild!=NULL) Q[++rear]=T->rchild;
}

}
template
void BiSortTree::Preshow(BiNode *root)
{
cout<<"采用前序非递归算法:";
BiNode *Q[1000];
int top=-1;
if(root==NULL) return;
Q[++top]=root;
while(top!=-1&&root!=NULL)
{
BiNode *T;
while(root!=NULL)
{
T=Q[--top];
cout<data<<" ";
root=root->lchild;
}
if(top!=-1){
root=Q[--top];
root=root->rchild;
}
}
}
template
void BiSortTree::Inshow(BiNode *root)
{
cout<<"采用中序递归算法"< if(root==NULL) return;
else{
Inshow(root->lchild);
cout<<" "<data;
Inshow(root->rchild);
}
}
int main()
{
int n=10;
int a[10]={63,55,90,42,58,70,10,45,67,83};
int k=55;
//cout<<"Inpput the value you want to find:";
//cin>>k;
BiSortTree example(a,n);
example.Inshow();
cout<<"查找节点值为55的节点";
BiNode *p;
p=example.SearchBST(k);
if(p==NULL) cout<<"lll"< //coutdata;
//example.DeleteBST(p->lchild,p);
example.Levershow();
cout<<"插入一个节点值为58的值:";
BiNode *s;
s->data=58;
example.InsertBST(s);
//example.Preshow();
return 0;
}

对于遍历算法可以不用考虑,只需看二叉排序树的建立上为什么建立不成功?

  • 写回答

4条回答 默认 最新

  • LLZK_ 2016-11-04 03:00
    关注

    你这个插入函数InsertBST的参数设定应该有问题,你在构造函数里面给这个函数传参的时候传的是指针s和指针root,若想改变s和root,传参的时候应该传指针的引用,或者二级指针。

    改成

    组合1⃣️二级指针
    InsertBST(BNode**root,BNode**s)

    传参改成 InsertBST(&root,&s)

    组合2⃣️传引用

    InsertBST(BNode&*root,BNode&*s)

    传参不改InsertBST(&root,&s)

    你这个问题应该是值传递,临时拷贝的问题。

    可以了解下参数传值,传指针,传引用的区别。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)