Metal Steak

Hard to eat

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

公告

aaaaaaaaaaaa

常用链接

留言簿(1)

我参与的团队

搜索

  •  

最新评论

#include <iostream>
using namespace std;

int
tmp[
501][501], n, _root_;

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

void
__createtree__(
int _fath_);

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

    cin 
>> n;
    
for(int i = 1; i <= n; i++)
    {
        
int
        x, y;
        cin 
>> x >> y;
        tmp[x][y] 
= true;
    }
    
for(int i = 1; i <= n; i++)
        
if(tmp[i][0])
        {
            _root_ 
= i;
            
break;
        }
    tree[_root_].x 
= _root_;
}

void
__createtree__(
int _fath_)
{
    
for(int i = 1; i <= n; i++)
        
if(tmp[i][_fath_])
        {
            
if(tree[_fath_].lf == 0)
            {
                tree[_fath_].lf 
= i;
                tree[i].x 
= i;
            }
            
else
            {
                
int temp = tree[_fath_].lf;
                
while(tree[temp].rt != 0)
                    temp 
= tree[temp].rt;
                tree[temp].rt 
= i;
                tree[i].x 
= i;
            }
        __createtree__(i);
    }
}

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

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

int
main()
{
    __readandinit__();
    __createtree__(_root_);

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

    
return 0;
}

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

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