一、通常樹spa
將這種通常的樹轉化成咱們熟悉的單鏈表形式,這有三層,每一層均可以當作單鏈表或者多個分散的單鏈表code
數據節點以下:blog
struct tree {
int elem;
struct tree *FirstChild;
struct tree *NextBro;
};遞歸
每一個節點和第一個孩子還有下一個兄弟連接io
#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; while (vy) { output_fa_and_child(vy); //遞歸調用 vy = vy->NextBro; } return 0; } /* 輸出樹中的全部數據 */ int output_tree_data(void) { if (!root_ptr) { printf("no data\n"); return -1; } output_fa_and_child(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; }
填充樹後的圖以下:ast
輸出數據順序是一、二、三、六、七、八、九、0、四、5class