在一定深度创建从二进制树的元素链表 [英] Create linked list from elements of a Binary tree at a certain depth

查看:58
本文介绍了在一定深度创建从二进制树的元素链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想建立一个链接列表,在一定深度的元素

我想出了这一点:

 无效NIVEL(阿斌一,INT K,SLIST * L,INT级){
    如果(一个!)回报;
    如果(K ==级){
     SLIST N =(SLIST)的malloc(sizeof的(结构SLIST));
     正>值= A->价值;
     正>接着=(* 1);
     (* 1)= N;
     返回;
    }其他{
        NIVEL(A->左,K,L,平+ 1);
        NIVEL(A->右,K,L,水平+ 1);
    }
}

它不工作

但练习要求使用这个标题:
SLIST NIVEL(阿斌一,INT N)

我曾尝试与一个空白练习。但无法弄清楚如何使返回链表之一。

从结构和二叉树数据:

  typedef结构SLIST
{
    int值;
    结构SLIST *接下来的;
} * SLIST;typedef结构arvbin *阿斌;
typedef结构arvbin
{
    int值;
    阿斌权;
    阿斌离开;
} arvb;

编辑:

< ---------------用头工作在运动----------->
//一个感谢你为 Politank-Z

  SLIST nivel_(阿斌一,诠释K){
    SLIST * L;    NIVEL(一个,K,L,1);    返回升;
}
无效NIVEL(阿斌一,INT K,SLIST * L,INT级){
    如果(一个!)回报;
    如果(K ==级){
     SLIST N =(SLIST)的malloc(sizeof的(结构SLIST));
     正>值= A->价值;
     正>接着=(* 1);
     (* 1)= N;
     返回;
    }其他{
        NIVEL(A->左,K,L,平+ 1);
        NIVEL(A->右,K,L,水平+ 1);
    }
}


解决方案

关于与原型的难度:它是相当普遍被限制在一个函数原型不符合您实现的需求。在这种情况下,往往容易从原型函数调用你的函数,然后到你的鞋拔子功能集成到原型。

I am trying to build a linked list, with the elements at a certain depth

I came up with this:

void nivel(ABin a, int k, SList *l, int level){
    if (!a) return;
    if(k == level){
     SList n = (SList)malloc(sizeof(struct slist));
     n->value = a->value;
     n->next=(*l);
     (*l) = n;
     return;
    }else{
        nivel(a->left, k, l, level+1);
        nivel(a->right, k, l, level+1);
    }
}

It does work

But the exercise asks to use this header: SList nivel (ABin a, int n)

I have tried with a void to practice. But can't figure out how to make the one that returns the linked lists.

Data from structure and binary tree:

typedef struct slist
{
    int value;
    struct slist* next;
} *SList;

typedef struct arvbin* ABin;
typedef struct arvbin
{
    int value;
    ABin right;
    ABin left;
} arvb;

EDIT:

<---------------working with header in the exercise-----------> // A thank you to Politank-Z

SList nivel_(ABin a, int k){
    SList *l; 

    nivel(a, k, l, 1);

    return l;
}


void nivel(ABin a, int k, SList *l, int level){
    if (!a) return;
    if(k == level){
     SList n = (SList)malloc(sizeof(struct slist));
     n->value = a->value;
     n->next=(*l);
     (*l) = n;
     return;
    }else{
        nivel(a->left, k, l, level+1);
        nivel(a->right, k, l, level+1);
    }
}

解决方案

Regarding your difficulty with the prototype: it is fairly common to be restricted to a function prototype which doesn't meet the needs of your implementation. In such cases, it is often easier to call your function from the prototyped function then to shoehorn your functionality into the prototype.

这篇关于在一定深度创建从二进制树的元素链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆