简体   繁体   English

将二叉树转换为简单链表

[英]Convert binary tree into simple linked list

struct Monitor {
    int codMonitor;
    char* producator;
    float diagonala;
    int numarPorturi;
};

struct nodls {
    Monitor info;
    nodls* next;
};

nodls* creareNod(Monitor m) { --create node 
    nodls* nou = (nodls*)malloc(sizeof(nodls));
    nou->info.codMonitor = m.codMonitor;
    nou->info.producator = (char*)malloc(sizeof(char)*(strlen(m.producator) + 1));
    strcpy(nou->info.producator, m.producator);
    nou->info.diagonala = m.diagonala;
    nou->info.numarPorturi = m.numarPorturi;
    nou->next = nou;

    return nou;
}

nodls* inserare(nodls* cap, Monitor m) { -- insert 
    nodls* nou = creareNod(m);
    if (cap == NULL) {
        cap = nou;
        cap->next = cap;
    }
    else
    {
        nodls* temp = cap;
        while (temp->next != cap)
            temp = temp->next;
        temp->next = nou;
        nou->next = cap;
    }
    return cap;
}

void afisareMonitor(Monitor m) { -- display struct
    printf("\nMonitorul cu codul %d, producatorul %s, diagonala %f, numarul de porturi %d",
        m.codMonitor, m.producator, m.diagonala, m.numarPorturi);
}

void traversare(nodls** cap) { --display function
    nodls* temp = *cap;
    if (cap == NULL)
        printf("\nLista este goala");
    while (temp->next != *cap) {
        afisareMonitor(temp->info);
        temp = temp->next;
    }
    afisareMonitor(temp->info);

}

void stergereNod(nodls* cap) --delete node function
{
.......
}

void dezalocare(nodls* cap) { free allocate space
............
}

How I can convert using the following code, my binary tree into a simple linked list.如何使用以下代码将我的二叉树转换为简单的链表。 This can be done with recursion maybe.这可以通过递归来完成。

getLeavesList(root) {

    if root is NULL: return

    if root is leaf_node: add_to_leaves_list(root)

    getLeavesList(root -> left)

    getLeavesList(root -> right)
}

So, if the root is NULL, this is, if the function received no valid pointer, then return an error message.所以,如果根是 NULL,也就是说,如果 function 没有收到有效的指针,则返回错误信息。

If the root is a leaf, this is, if both left and right child nodes are NULL, the you have to add it to the list of leaf nodes.如果根是叶子,也就是说,如果左右子节点都是 NULL,则必须将其添加到叶子节点列表中。

Then you recursively call the function with the left and right child nodes.然后递归调用具有左右子节点的 function。

https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/ https://www.geeksforgeeks.org/tree-traversals-inorder-preorder-and-postorder/

This article covers 3 simple depth-first approaches(preorder, inorder, postorder) to traverse a binary tree.本文介绍了遍历二叉树的 3 种简单的深度优先方法(前序、中序、后序)。 It also has a nice compact example in C.它在 C 中也有一个很好的紧凑示例。

The pseudo-code algorithm you provided is actually the proper preorder approach.您提供的伪代码算法实际上是正确的预购方法。

The only modification you have to make in the您必须在

void printPreorder(struct node* node)

method is to replace the printing of the node's data:方法是替换节点数据的打印:

printf("%d ", node->data);

with checking if the node is a leaf and if yes adding it to a list:检查节点是否为叶子,如果是,则将其添加到列表中:

if((node->left == NULL) && (node->right == NULL))
{
     appendList(&node);   
}

Of course appendList is just my ad-hoc creation but based on the code that you've provided in the question you'll know how to add to a linked-list.当然appendList只是我的临时创建,但根据您在问题中提供的代码,您将知道如何添加到链接列表。 If not please feel free to ask.如果没有,请随时询问。

ps: https://www.geeksforgeeks.org/ is an amazing resource hats of to those guys for the amazing work! ps: https://www.geeksforgeeks.org/是对那些出色工作的人的惊人资源帽!

psps: If you'd like to return an error message when the function is called with NULL for the first time, that is if no valid tree is passed to the traversal, I'd suggest you to make a wrapper function that will call the recursive one. psps: If you'd like to return an error message when the function is called with NULL for the first time, that is if no valid tree is passed to the traversal, I'd suggest you to make a wrapper function that will call the递归的。 Then, in that wrapper you can check if the head passed to it is NULL before you even call the recursive method at all.然后,在该包装器中,您甚至可以在调用递归方法之前检查传递给它的头是否是 NULL。

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

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