C語言之一般樹

1、一般樹

將這種一般的樹轉化成我們熟悉的單鏈表形式,這有三層,每一層都可以看成單鏈表或者多個分散的單鏈表

數據節點如下:

struct tree {
        int elem;
        struct tree *FirstChild;
        struct tree *NextBro;
};

每個節點和第一個孩子還有下一個兄弟鏈接

複製代碼

#include <stdio.h>
#include <stdlib.h>

struct tree {
    int elem;
    struct tree *FirstChild;
    struct tree *NextBro;
};

struct tree *root_ptr = NULL;

/* 知道第一個孩子的位置,將要添加的節點放到鏈尾 */
int register_child(struct tree **first, struct tree *tree_ptr)
{
    struct tree *ptr = *first;
    while (ptr->NextBro)
        ptr = ptr->NextBro;

    ptr->NextBro = tree_ptr;
    return 0;
}

/* 3層樹
* floor: 要添加的鏈表位於第幾層
* FirstFa: 是第一層第幾個節點的孩子
* num:節點的值
*/
int add_tree(int floor, int FirstFa, int num)
{
    struct tree *tree_ptr = (struct tree *)calloc(1, sizeof(struct tree));
    if (!tree_ptr) {
        printf("calloc error\n");
        return -1;
    }
    if (!root_ptr) {
        if (floor == 0)
            root_ptr = tree_ptr;
    }
    else {
        if (floor == 0) {
            printf("root really exist\n");
            goto error;
        }
        else if (floor == 1) {
            if (!(root_ptr->FirstChild))
                root_ptr->FirstChild = tree_ptr;
            else
                register_child(&(root_ptr->FirstChild), tree_ptr);
        }
        else if (floor == 2) {
            int i;
            struct tree *last_fa = root_ptr->FirstChild;
            if (!last_fa) {
                printf("no first floor\n");   //第1層沒有
                goto error;
            }
            for (i = 0; i < FirstFa; i++)
                last_fa = last_fa->NextBro;
            if (!last_fa) {
                printf("your father No exist\n");  //對應的父節點沒有
                goto error;
            }
            if (!(last_fa->FirstChild))
                last_fa->FirstChild = tree_ptr;
            else
                register_child(&(last_fa->FirstChild), tree_ptr);
        }
    }
    tree_ptr->elem = num;
    tree_ptr->FirstChild = NULL;
    tree_ptr->NextBro = NULL;
    return 0;
error:
    free(tree_ptr);
    return -1;
}

/* 輸出該節點和節點下的所以數據 */
int output_fa_and_child(struct tree *fa)
{
    static int cnt = 0;
    printf("data %d : %d\n", cnt++, fa->elem);
    struct tree *vy = fa->FirstChild;
    tree *tmp =NULL;
    while (vy) {
    tmp = vy;
        output_fa_and_child(vy);   //遞歸調用
        vy = vy->NextBro;
        free(tmp);
    }
    return 0;
}

/* 輸出樹中的所有數據 */
int output_tree_data(void)
{
    if (!root_ptr) {
        printf("no data\n");
        return -1;
    }
    output_fa_and_child(root_ptr);
    free(root_ptr);
    return 0;
}

int main()
{
    int i;
    int ret;
    /* 向樹中添加10個節點 */
    int num[20] = { 1,2,3,4,5,6,7,8,9,0,10,11 };
    ret = add_tree(0, 0, num[0]);
    if (ret < 0) {
        printf("add_tree error\n");
    }
    for (i = 1; i < 5; i++) {
        ret = add_tree(1, 0, num[i]);
        if (ret < 0) {
            printf("add_tree error\n");
        }
    }
    for (i = 1; i < 6; i++) {
        ret = add_tree(2, 1, num[4 + i]);
        if (ret < 0) {
            printf("add_tree error\n");
        }
    }
    /* 輸出所有節點中的數據 */
    ret = output_tree_data();
    if (ret < 0)
        printf("output_tree_data error\n");
   return 0;
}

複製代碼

填充樹後的圖如下:

輸出數據順序是1、2、3、6、7、8、9、0、4、5

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章