二叉树查找指针使用的问题,求大家帮忙解决下
#include<stdio.h>#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct Node
{
int weight; //值
struct Node *zuo;//左子树
struct Node *you;//右子树
int xuhao; //序号
int lchild; //左孩子存在为1,否则为0
int rchild; //右孩子存在为1,否则为0
}ECNode;
void zishu(ECNode *p,ECNode *q);
int chazhao(ECNode *L,int key);
main()
{
int n,i,key;
ECNode *L,*p,*q; //小的放左节点,大的放右节点
cout<<"要输入多少个数"<<endl;
cin>>n;
int a[n];
cout<<"输入数组:"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
cout<<"您输入的数是"<<endl;
for(i=0;i<n;i++) cout<<a[i]<<" ";
cout<<endl;
L=(ECNode*)malloc(sizeof(ECNode));
p=L;
L->weight=a[i]; L->xuhao=1;
L->lchild=0; L->rchild=0; //等于0为没有,其他为序号
for(i=1;i<n;i++)
{
q=(ECNode*)malloc(sizeof(ECNode));
q->weight=a[i];q->xuhao=i+1;
if(q->weight<p->weight)
zishu(p,q);
}
cout<<"输入要查找的数:"<<endl;
cin>>key;
cout<<"要查找的是第"<<chazhao(L,key)<<"个数"<<endl;;
}
void zishu(ECNode *p,ECNode *q)
{
int key1;
if(q->weight<p->weight)
{
if(!p->lchild) {p->zuo=q;p->lchild=1;}
else zishu(p->zuo,q);
}
else
{
if(!p->rchild){ p->you=q;p->rchild=1;}
else zishu(p->you,q);
}
}
int chazhao(ECNode *L,int key)
{
ECNode *k;
k=L;
if(L->weight>key) { k=k->zuo;chazhao(L->zuo,key); }
else if(L->weight<key) { k=k->you;chazhao(L->you,key); }
else return k->xuhao;
}