OJ 有序树转二叉树

Time Limit: 1 Sec Memory Limit: 4 MB

Description

计算输入有序树的深度和有序树转化为二叉树之后树的深度。

Input

输入包含多组数据。每组数据第一行为一个整数n(2<=n<=30000)代表节点的数量,接下来n-1行,两个整数a、b代表a是b的父亲结点。

Output

输出当前树的深度和转化成二叉树之后的深度。

Sample Input

5

1 5

1 3

5 2

1 4

Sample Output

3 4

算法一 : 二叉链表

用孩子-兄弟表示法表示有序树和二叉树,这也就意味着直接用二叉树的形式表示有序树,只不过每个节点的两个指针不是左右孩子的指针,而是一个指向第一个孩子、另一个指向下一个兄弟。

有序树转二叉树在遍历的时候,操作不一样。

在形式上有序树存成了二叉树的样子,看的角度不同,就是有序树与二叉树的区别。

遍历一个存成二叉树形式的有序树的算法:

int depth_Tree(CSTree t)
{
    if(t==NULL)
        return 0;
    int firstchild_depth = depth_Tree(t->firstchild);
    int nextsibling_depth = depth_Tree(t->nextsibling);

    return (firstchild_depth+1 > nextsibling_depth ? firstchild_depth+1 : nextsibling_depth);
}

完整程序: (OJ系统判断 TimeLimit Exceed)

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef int ElemType;
typedef int Status;
///孩子-兄弟表示法
typedef struct CSNode
{
    ElemType data;
    struct CSNode *firstchild,*nextsibling;
} CSNode,*CSTree;
///初始化树
int Init_CSTree(CSTree &t)
{
    t=NULL;
    //static int maxn = 0;
    return 0;
}
//查找数据元素
CSNode *Traverse(CSTree t,int x)
{
//查找数据元素x是否在二叉树root中
    //查找到则返回该节点指针,未查找到则返回空指针
    CSNode *f=NULL;
    if(t!=NULL)
    {

        if(t->data==x)
        {

            f=t;
        }
        else
        {

            f=Traverse(t->firstchild,x);//在左子树中找
            if(f==NULL)
            {

                f=Traverse(t->nextsibling,x);
            }
        }
    }

    return f;//返回查找标志

}
int Create_CSTree(CSTree &t,int a,int b)
{

    if(t==NULL)
    {
        ///t = (CSNode*)malloc(sizeof(CSNode));
        t=new CSNode;
        t->data = a;
        t->firstchild = new CSNode;
        t->nextsibling=NULL;
        t->firstchild->data = b;
        t->firstchild->firstchild=NULL;
        t->firstchild->nextsibling=NULL;

    }
    else
    {
        ///  查找 新输入的 a 是否在已存在的二叉链表中也有与a相等的
        ///节点 有的话 在其firstchild的节点上添加兄弟节点

        CSNode *p;
        p = Traverse(t,a); ///传值没问题
        if(p==NULL)
        {
            return -1;
            //printf("input wrong!\n");
        }
        if(p->firstchild != NULL)
        {
            CSNode *q;
            q = p->firstchild;
            for(int i = 0; ; i++)
            {
                if(q->nextsibling==NULL)
                {
                    break;
                }
                else
                {
                    q = q->nextsibling;
                }
            }
            q->nextsibling = new CSNode;
            q->nextsibling->data = b;
            q->nextsibling->firstchild=NULL;
            q->nextsibling->nextsibling=NULL;
        }
        else
        {
            p->firstchild = new CSNode;
            p->firstchild->data = b;
            p->firstchild->firstchild = NULL;
            p->firstchild->nextsibling = NULL;
        }

    }
    return 0;

}
int Judge(CSTree t) ///求二叉链表中节点个数
{
    if(t==NULL)
        return 0;

    int firstchild_num = Judge(t->firstchild);
    int nextsubling_num = Judge(t->nextsibling);
    int ret = firstchild_num + nextsubling_num +1;
    return ret;

}

int depth_Tree(CSTree t)
{
    if(t==NULL)
        return 0;
    int firstchild_depth = depth_Tree(t->firstchild);
    int nextsibling_depth = depth_Tree(t->nextsibling);

    return (firstchild_depth+1 > nextsibling_depth ? firstchild_depth+1 : nextsibling_depth);
}
int depth_BiTree(CSTree t)
{
    if(t==NULL)
        return 0;
    int firstBiTree_depth = depth_BiTree(t->firstchild);
    int siblingBiTree_depth = depth_BiTree(t->nextsibling);
    if(firstBiTree_depth>siblingBiTree_depth)
        return firstBiTree_depth+1;
    else
        return siblingBiTree_depth+1;
}
void freeTree(CSTree &root)
{
    maxn = 0;
    if (root!=NULL)
    {

        if (root->firstchild)
        {
            freeTree(root->firstchild);
            root->firstchild= NULL;
        }
        if (root->nextsibling)
        {
            freeTree(root->nextsibling);
            root->nextsibling = NULL;
        }
        if (root!=NULL)
        {
            free(root);
            root=NULL;
        }
    }
    //return 0;
}
int main()
{
    int n,flag = 1;
    while(scanf("%d",&n)!=EOF)
    {
        CSTree T;
        Init_CSTree(T);
        int c[2][n-1];

        for(int op =0; op<n-1 ; op++) ///创建树
        {

            int x,y;
            scanf("%d %d",&c[0][op],&c[1][op]);
            //Create_CSTree(T,x,y);
        }
        for(int w=0; w<n-1; w++)
        {
            int wr = c[0][w];
            int wt = c[1][w];
            Create_CSTree(T,wr,wt);
        }
        ///再来一个对firstchild 进行计数的函数 (即 这棵树有几层 就是有序树的深度)
        ///然后把这个孩子-兄第表示的树 看成二叉树 求深度即可
        int a,b;
        a = depth_Tree(T);
        b = depth_BiTree(T);

            printf("%d %d\n",a,b);

        freeTree(T);

    }
    return 0;
}

算法二 : 结构体数组 结构体数组的指针

以空间换时间,访问数组而不是遍历二叉树。

定义一个结构体,结构体中存有指向第一个孩子的指针和指向下一个兄弟的指针,以及是否存在的标记。

定义一个结构体数组用来存储这些结构体,而下标就表示输入的父亲孩子节点的数值。

其实如果用指针来指向结构体数组的其他元素的话,也可以看成二叉链表的形式(如上图右边的形式),只不过对其进行操作时是数组形式,更加简单方便。

#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct ArTree
{
    struct ArTree *firstchild;//,*lastsibling;
    struct ArTree *nextsibling;
    int  exist_status;
    ArTree()///mo ren gou zao han shu
    {
        firstchild =NULL;///结构体成员指针需要初始化
        //lastsibling = NULL;
        nextsibling=NULL;
        exist_status=0;
    }
} ArTree;
int Create_ArTree(struct ArTree *Ar,int a,int b,int &n)///  检测根结点是否发生变化
{
    if((Ar+a)->exist_status == 1 && (Ar+a)->firstchild==NULL)
    {
        (Ar+a)->firstchild = (Ar+b);
        (Ar+b)->exist_status = 1;
    }
    else if((Ar+a)->exist_status == 1 && (Ar+a)->firstchild!=NULL)
    {
        ArTree *qq=NULL;
        qq = (Ar+a)->firstchild;
        while((qq->nextsibling)!=NULL)
        {
            qq = qq->nextsibling;
        }
        qq->nextsibling = (Ar+b);
        (Ar+b)->exist_status = 1;
        ///free(qq);  

    }
    else if((Ar+b)->exist_status == 1)
    {


        (Ar+a)->firstchild = (Ar+b);
        (Ar+a)->exist_status = 1;
        n = a;
        Ar = Ar+a;


    }
}

///求深度
int depth_ArTree_Order(ArTree *Ar)
{
    if(Ar==NULL)
        return 0;
    int firstchild_depth = depth_ArTree_Order(Ar->firstchild);
    int nextsibling_depth = depth_ArTree_Order(Ar->nextsibling);

    return (firstchild_depth+1 > nextsibling_depth ? firstchild_depth+1 : nextsibling_depth);
}
int depth_ArTree(ArTree *Ar)
{
    if(Ar==NULL)
        return 0;

    int fir = depth_ArTree(Ar->firstchild);
    int nex = depth_ArTree(Ar->nextsibling);
    if(fir>nex)
        return fir+1;
    else
        return nex+1;

}
int main()
{


    int input_num;
    while(scanf("%d",&input_num)!=EOF)
    {
        struct ArTree Ar[30010],*p;//,*pro;
        p = Ar;
        int flag = 0;
        int n = 1;
        for(int i = 0; i<input_num-1; i++)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            if(flag==0)
            {


                (p+a)->firstchild = p+b;
                n=a;
                (p+a)->exist_status = 1;

                (p+b)->exist_status = 1;
                flag=1;
                //printf("p+a = %p (p+a)->firstchild = %p p+b = %p\n",p+a,(p+a)->firstchild ,p+b);
            }
            else if(flag!=0)
            {
                Create_ArTree(p,a,b,n);

            }
        }
        printf("%d %d\n",depth_ArTree_Order(p+n),depth_ArTree(p+n));

    }

}

算法二如果使用 free(q) 会出现 RunTime Error,报错结果为:

Runtime Error:[ERROR] A Not allowed system call: runid:1508674
callid:146 *** glibc detected *** ./Main: free(): invalid pointer:
0xbfaaa550 *** Runtime Error:[ERROR] A Not allowed system call:
runid:1508674 callid:146 *** glibc detected *** ./Main: free():
invalid pointer: 0xbf9d4514 ***

算法三 : 二叉链表存储、结构体数组访问

核心:建立二叉链表与结构体数组间的联系

步骤:

    1. 建立结构体和二叉树,并进行结构体数组和树的初始化;

    2. 输入第一对节点,建立二叉链表,同时对相应的数组元素进行操作,令根结点的firstchild指向其在二叉链表中的孩子节点,而lastsibling则指向自己(也就是根结点);

    3. 接下来的输入按照以下步骤:

        判断输入的a是否在二叉链表里且a是否有孩子,如果没有的话,利用数组取得其地址,对其地址所在的节点加孩子,并且对相应的数组元素进行操作;

        如果有孩子的话,利用数组找到其孩子的最后一个兄弟,在最后一个兄弟后再加一个兄弟,并且对其原来的最后一个兄弟和现在的最后一个兄弟所在的数组元素进行操作;

结构体数组与二叉链表之间的关系

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
typedef int ElemType;
typedef int Status;
///孩子-兄弟表示法
typedef struct CSNode
{
    ElemType data;
    struct CSNode *firstchild,*nextsibling;
} CSNode,*CSTree;
///中间件

typedef struct ArTree
{
    CSNode *firstchild_Ar,*lastsibling;
    //struct ArTree *nextsibling;
    int  exist_status;
    ArTree()///默认构造函数
    {
        firstchild_Ar =NULL;///结构体成员指针需要初始化
        lastsibling = NULL;
        //nextsibling=NULL;
        exist_status=0;
    }
} ArTree;

///初始化树
int Init_CSTree(CSTree &t)
{
    t=NULL;
    //static int maxn = 0;
    return 0;
}
int Create_ArTree_BiTree(CSTree &T,struct ArTree *Ar,int a,int b)
{
    if(T==NULL)
    {
        ///create artree
        T = new CSNode;
        T->data = a;
        T->nextsibling=NULL;
        T->firstchild = new CSNode;
        T->firstchild->data =b;
        T->firstchild->firstchild = NULL;
        T->firstchild->nextsibling=NULL;
        ///中间件
        (Ar+a)->firstchild_Ar = T->firstchild;
        //n=a;
        (Ar+a)->lastsibling=T;
        (Ar+b)->lastsibling = T->firstchild;
        (Ar+a)->exist_status = 1;
        (Ar+b)->exist_status = 1;
    }
    else
    {
        ///判断输入的a 是否已经在二叉链表里了
        if((Ar+a)->exist_status==1 && (Ar+a)->firstchild_Ar == NULL)
        {

            if((Ar+a)->lastsibling->data == a)
            {
                CSNode *lq;
                lq= (Ar+a)->lastsibling;///zi shen
                lq->firstchild= new CSNode;
                lq->firstchild->data = b;
                lq->firstchild->firstchild=NULL;
                lq->firstchild->nextsibling=NULL;
                (Ar+a)->firstchild_Ar=lq->firstchild;
                (Ar+b)->lastsibling=lq->firstchild;
                (Ar+b)->exist_status =1;
            }
            else if((Ar+a)->lastsibling->data != a)
            {
                CSNode *haha;
                haha = (Ar+a)->lastsibling;
                int y;
                y = haha->data;
                (Ar+y)->lastsibling->firstchild=new CSNode;
                (Ar+y)->lastsibling->firstchild->data=b;
                (Ar+y)->lastsibling->firstchild->firstchild=NULL;
                (Ar+y)->lastsibling->firstchild->nextsibling=NULL;
                (Ar+a)->firstchild_Ar=(Ar+y)->lastsibling->firstchild;
                (Ar+b)->lastsibling=(Ar+y)->lastsibling->firstchild;
                (Ar+b)->exist_status=1;
            }
        }
        else if((Ar+a)->exist_status ==1 && (Ar+a)->firstchild_Ar != NULL)
        {
            //CSNode *lq;
            int n;
            n = (Ar+a)->firstchild_Ar->data;
            if((Ar+n)->lastsibling == (Ar+a)->firstchild_Ar)
            {
                CSNode *qll;
                qll=new CSNode;
                qll->data=b;
                qll->firstchild=NULL;
                qll->nextsibling=NULL;
                (Ar+n)->lastsibling->nextsibling=qll;
                (Ar+n)->lastsibling=qll;
                (Ar+b)->lastsibling= (Ar+a)->firstchild_Ar;///指回去
                (Ar+b)->exist_status=1;
            }
            else
            {
                CSNode *qll;
                qll = (Ar+n)->lastsibling;
                qll->nextsibling=new CSNode;
                qll->nextsibling->data=b;
                qll->nextsibling->firstchild=NULL;
                qll->nextsibling->nextsibling=NULL;
                (Ar+n)->lastsibling=qll->nextsibling;
                 int m;
                 m=qll->data;
                (Ar+m)->lastsibling=qll;

                (Ar+b)->lastsibling=(Ar+a)->firstchild_Ar;
                (Ar+b)->exist_status=1;
            }
        }
    }
    return 0;
}
///求深度
void freeTree(CSTree &root)
{
    if (root!=NULL)
    {

        if (root->firstchild)
        {
            freeTree(root->firstchild);
            root->firstchild= NULL;
        }
        if (root->nextsibling)
        {
            freeTree(root->nextsibling);
            root->nextsibling = NULL;
        }
        if (root!=NULL)
        {
            free(root);
            root=NULL;
        }
    }
}
void InOrder(CSTree T)
{
    if(T)
    {
        InOrder(T->firstchild);
        printf("%d ",T->data);
        InOrder(T->nextsibling);
    }
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        ///first create bitree
        CSTree T;
        Init_CSTree(T);
        struct ArTree Ar[31000];//,*p;
        //p=Ar;
        ///input data
        for(int i =0; i<n-1; i++)
        {
            int a,b;
            scanf("%d %d",&a,&b);
            Create_ArTree_BiTree(T,Ar,a,b);
            //InOrder(T);
            //printf("\n");
        }
        printf("%d %d\n",depth_Tree(T),depth_BiTree(T));
        //freeTree(T);
    }
    return 0;
}

还有一个现象:

    如果静态变量没有初始化,但在函数中出现了,codeblocks不会运行。

如上。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容

  • 树的概述 树是一种非常常用的数据结构,树与前面介绍的线性表,栈,队列等线性结构不同,树是一种非线性结构 1.树的定...
    Jack921阅读 4,373评论 1 31
  • 四、树与二叉树 1. 二叉树的顺序存储结构 二叉树的顺序存储就是用数组存储二叉树。二叉树的每个结点在顺序存储中都有...
    MinoyJet阅读 1,452评论 0 7
  • 1 序 2016年6月25日夜,帝都,天下着大雨,拖着行李箱和同学在校门口照了最后一张合照,搬离寝室打车去了提前租...
    RichardJieChen阅读 5,016评论 0 12
  • B树的定义 一棵m阶的B树满足下列条件: 树中每个结点至多有m个孩子。 除根结点和叶子结点外,其它每个结点至少有m...
    文档随手记阅读 13,010评论 0 25
  • 1、算法的概念 (1)概念:是指解题方案的准确而完整的描述。 【考题1】在计算机中,算法是指() A查询方法B加工...
    成都小菜阅读 1,484评论 0 15