简体   繁体   English

从特定深度的二叉树元素创建链接列表

[英]Create linked list from elements of a Binary tree at a certain depth

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) 但是练习要求使用此标头: 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 <---------------在练习中使用标题-----------> //感谢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);
    }
}

Do you mean that you want to build a linked list using binary tree? 您是说要使用二叉树来构建链接列表吗? However, you may add a new item into your list when (k == level), then you call nivel(a->left, k, l, level+1). 但是,您可以在(k == level)时将新项目添加到列表中,然后调用nivel(a-> left,k,l,level + 1)。 It won't add any node here, because k != level+1 now, so your list will contain just one node actually... 它不会在此处添加任何节点,因为现在k!= level + 1,因此您的列表实际上仅包含一个节点...

BTW, you should ensure nivel add one node into your list everytime. 顺便说一句,您应该确保nivel每次都在列表中添加一个节点。

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. 在这种情况下,从原型函数中调用函数然后将函数插入原型中通常会更容易。

} *SList; 

will create a pointer type called SList. 将创建一个称为SList的指针类型。 This causes confusion within your code, because you use SList *l which is a pointer to a pointer! 这会在您的代码中引起混乱,因为您使用SList *l ,它是指向指针的指针! Change your struct typedef to just } SList . 将您的struct typedef更改为} SList This way you don't have to dereference a pointer of a pointer to reach the pointer value. 这样,您不必取消引用指针的指针即可达到该指针的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM