为生存而奔跑

   :: 首页 :: 联系 :: 聚合  :: 管理
  271 Posts :: 0 Stories :: 58 Comments :: 0 Trackbacks

留言簿(5)

我参与的团队

搜索

  •  

积分与排名

  • 积分 - 319841
  • 排名 - 75

最新评论

阅读排行榜

评论排行榜

#include<iostream>
#include
<algorithm>
using namespace std;
#define maxn 10
int a[]={0,1,2,3,4,5,6,7,8,9};
struct node
{
 
int key;
 
struct node * lchild,*rchild;
}
;
node 
* create(int pos)
{
 
if(pos>=maxn)
  
return NULL;
 node 
* root=(node*)malloc(sizeof(node));
 root
->key=a[pos];
 root
->lchild=create(2*pos);
 root
->rchild=create(2*pos+1);
 
return root;
}

void levelorder(node * root)
{
 node 
* q[maxn];
 
int top,tail;
 top
=0,tail=1;
 q[
0]=root;
 
while(top<tail)
 
{
  printf(
"%d\n",q[top]->key);
  
if(q[top]->lchild!=NULL)
   q[tail
++]=q[top]->lchild;
  
if(q[top]->rchild!=NULL)
   q[tail
++]=q[top]->rchild;
  top
++;
 }

}

int main()
{
 levelorder(create(
1));

}

posted on 2009-08-06 10:01 baby-fly 阅读(335) 评论(0)  编辑 收藏 引用 所属分类: Algorithm

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理