?? 新建 文本文檔 (3).txt
字號:
你的問題還是一樣的,
h沒有定義成全局變量,所以得不到值,
第一,你可以把h改成全局量,或者
第二,把high(p,0)改成high(p,h),把void high(bnode *p,int h)
改成void high(bnode *p,int& h)
new_del
new_del 第2樓 回復(fù)于2004-8-15 11:21:00
--------------------------------------------------------------------------------
接受本回復(fù)作為正確回答
#include<stdio.h>
#include<malloc.h>
#define null 0
int counter=0;
int h,hl,hr;
typedef struct btreenode
{
int data;
struct btreenode *lchild;
struct btreenode *rchild;
}bnode;
bnode *create(int x,bnode *lbt,bnode *rbt)
{
bnode *p;
p=(bnode*)malloc(sizeof(bnode));
p->data=x;
p->lchild=lbt;
p->rchild=rbt;
return(p);
}
void ins_lchild(bnode *p,int x)
{
bnode *q;
if(p==null)
printf("Illegal insert.");
else
{
q=(bnode*)malloc(sizeof(bnode));
q->data=x;
q->lchild=null;
q->rchild=null;
p->lchild=q;
}
}
void ins_rchild(bnode *p,int x)
{
bnode *q;
if(p==null)
printf("Illegal insert");
else
{
q=(bnode*)malloc(sizeof(bnode));
q->data=x;
q->lchild=null;
q->rchild=null;
p->rchild=q;
}
}
int max(int a,int b)
{
if(a>b)
return(a);
else
{
if(a<b)
return(b);
else
return(a);
}
}
void high(bnode *p,int &h)
{
if(p==null)
h=0;
else
{
if(p->lchild!=null)
high(p->lchild,hl);
if(p->rchild!=null)
high(p->rchild,hr);
h=max(hl,hr)+1;
}
}
void main()
{
bnode *bt,*p,*q;
int x;
printf("Input root:");
scanf("%d",&x);
p=create(x,null,null);
bt=p;
scanf("%d",&x);
while(x!=-1)
{
p=bt;
q=p;
while(x!=p->data&&q!=null)
{
p=q;
if(x<p->data)
q=p->lchild;
else
q=p->rchild;
}
if(x==p->data)
{
printf("The number is always exit.");
}
else
if(x<p->data)
ins_lchild(p,x);
else
ins_rchild(p,x);
scanf("%d",&x);
}
p=bt;
high(p,h);
printf("\n");
printf("h=%d\n",h);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -