Metal Steak

Hard to eat

  C++博客 :: 首页 :: 联系 :: 聚合  :: 管理
  0 Posts :: 79 Stories :: 0 Comments :: 0 Trackbacks

公告

aaaaaaaaaaaa

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

#include <iostream>
using namespace std;

int _root_, father[1001];

struct
node
{
    
int n;
    
int lf, rt;
    node()
    {
        n 
= lf = rt = 0;
    }
}tree[
1001];

void
__readandinit__();
int
__findroot__();
void
visitB(node x);
void
visitM(node x);
void
visitP(node x);

int
main()
{
    __readandinit__();

    visitP(tree[_root_]); cout 
<< endl;
    visitM(tree[_root_]); cout 
<< endl;
    visitB(tree[_root_]);

    
return 0;
}

void
__readandinit__()
{
    memset(father, 
0sizeof father);

    
int HowManyNodes, p;
    cin 
>> HowManyNodes;
    
for(int i = 1; i <= HowManyNodes; i++)
    {
        cin 
>> p >> tree[p].lf >> tree[p].rt;
        tree[p].n 
= p;
        father[tree[p].lf] 
= father[tree[p].rt] = p;
    }
    _root_ 
= __findroot__();
}

int
__findroot__()
{
    
int i = 1;
    
while(father[i] != 0)
        i 
= father[i];
    
return i;
}

void
visitP(node x)
{
    cout 
<< x.n << endl;
    
if(x.lf != 0)
        visitP(tree[x.lf]);
    
if(x.rt != 0)
        visitP(tree[x.rt]);
}

void
visitM(node x)
{
    
if(x.lf != 0)
        visitM(tree[x.lf]);
    cout 
<< x.n << endl;
    
if(x.rt != 0)
        visitM(tree[x.rt]);
}

void
visitB(node x)
{
    
if(x.lf != 0)
        visitB(tree[x.lf]);
    
if(x.rt != 0)
        visitB(tree[x.rt]);
    cout 
<< x.n << endl;
}

posted on 2009-09-15 21:34 mad4alcohol 阅读(83) 评论(0)  编辑 收藏 引用

只有注册用户登录后才能发表评论。
网站导航:   博客园   博客园最新博文   博问   管理