首页 > 其他 > 详细

NYOJ 220 (红黑树--模拟)

时间:2015-02-02 14:13:11      阅读:535      评论:0      收藏:0      [点我收藏+]

链接:click here 

题意:题目其实很简单,绕一大圈,原来就是叫你输出输出中序遍历,Orz~~~红黑树经过旋转后中序遍历其实是不变的,所以与下面的旋转没有关系~~--- _ --.

思路:直接数组模拟,或用结构体:包含(数据域,左子树,右子树)

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn=20;
const int inf =0x3f3f3f3f;
int N,F,D;
int leftt[maxn],rightt[maxn];
struct node
{
    int data;
    int leftchild,rightchild;
} tree[maxn];
void inorder(int key)
{
    if(key!=-1)
    {
        inorder(tree[key].leftchild);
        printf("%d\n",tree[key].data);
        inorder(tree[key].rightchild);
    }
}
int main()
{
    int T,root,ld,rd;
    scanf("%d",&T);
    while(T--)
    {
        int n;
        scanf("%d",&n);
        for(int i=0; i<n; i++)
        {
            scanf("%d%d%d",&root,&ld,&rd);
            tree[root].data=root;
            tree[root].leftchild=ld;
            tree[root].rightchild=rd;
            // scanf("%d%d%d",&tree[i].data,&tree[i].leftchild,&tree[i].rightchild);
        }
        int m,ve,mv;
        scanf("%d",&m);
        while(m--)
        {
            scanf("%d%d",&ve,&mv);
        }
        inorder(0);
        printf("\n");
    }
    return 0;
}


NYOJ 220 (红黑树--模拟)

原文:http://blog.csdn.net/u013050857/article/details/43406911

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!