2叉排序树的平均查找长度,请高手帮忙
// 2chapaixu.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include"iostream.h"
const int n=10;
int c[n+1];
static int lev=0;
static bool found;
class btreenode
{public:
int c[n+1];
int data;
btreenode *left,*right;
};
btreenode *insert(btreenode *bst,int x)
{
btreenode *p,*q,*s=new btreenode;
s->data=x;
s->left=s->right=NULL;
p=bst;
while(p!=NULL)
{q=p;if(p->data>x) p=p->left;else p=p->right;}
if(bst==NULL) return s;
if(q->data>x) q->left=s;
else q->right=s;
return bst;
}
btreenode *creat(int n)
{ int x;
btreenode *bst=NULL;
for(int i=1;i<=n;i++)
{cin>>x;
c[i]=x;
bst=insert(bst,x);
}
return bst;
}
void inorder(btreenode *root)
{
if(root!=NULL)
{inorder(root->left);
cout<<root->data<<" ";
inorder(root->right);
}
}
btreenode *find(btreenode *bst,int x)
{if(bst==NULL)
return NULL;
else
{if(bst->data==x)
return bst;
else if(bst->data>x)
return find(bst->left,x);
else return find(bst->right,x);
}
}
int level(btreenode *root,int key)
{ // 求二叉树节点 key 层数
if(root!=NULL && !found)
{
lev++;
if(root->data==key)
found=true;
else
{
level(root->left,key); // 遍历左子树
level(root->right,key); // 遍历右子树
if(!found) lev--;
}
}
return lev;
}
int main(int argc, char* argv[])
{ int m;
int a[11];
btreenode *t,*p;
t=creat(n);
cin>>m;
p=find(t,m);
if(p==NULL)
cout<<"找不到"<<endl;
else cout<<"zhaodao"<<endl;
inorder(t);
float asl=0;
a[1]=level(t,c[10]); //正确
a[2]=level(t,c[9]); //一样没有改变
a[3]=level(t,c[8]);
a[4]=level(t,c[7]);
a[5]=level(t,c[6]);
a[6]=level(t,c[5]);
a[7]=level(t,c[4]);
a[8]=level(t,c[3]);
a[9]=level(t,c[2]);
a[10]=level(t,c[3]);
for(int i=1;i<=10;i++)
{ asl=asl+a[i];
}
cout<<b/n<<endl;
return 0;
}
我的代码为什么无法显示出平均查找长度呢,而且除了第一个节点的层数对了之外,其余每个节点的层数都是一样