樹 【資料結構與演算法分析 c 語言描述】

jerrkill發表於2018-12-28

1. 樹的概念

樹就是一種非線性的資料結構,其平均複雜度為O(logN)。

2. 實現思路

  • 只需要一個指向第一個節點的指標跟右側兄弟節點的指標
    file

3. 實現過程

定義樹的結構體

typedef int element_type;
typedef struct node
{
    element_type element;
    struct node *first_child;
    struct node *next_sibling;
} *tree;

要實現的方法

tree create(element_type x);
void make_empty(tree t);
int is_empty(tree t);
void insert(element_type x, tree t, int is_child);
void print_tree(tree t);
void init(tree t); //初始化生成樹手動輸入

void rand_tree(tree t); //生成一個隨機樹

4. 全部程式碼

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

#define error(str) fatal_error(str)
#define fatal_error(str) fprintf(stderr, "%s\n", str),exit(1)

typedef int element_type;
typedef struct node
{
    element_type element;
    struct node *first_child;
    struct node *next_sibling;
} *tree;

tree create(element_type x);
void make_empty(tree t);
int is_empty(tree t);
void insert(element_type x, tree t, int is_child);
void print_tree(tree t);
void init(tree t); //初始化生成樹手動輸入

void rand_tree(tree t); //生成一個隨機樹
void test();

void init(tree t)
{
    tree temp_cell;
    element_type x;
    int has_child,  has_sibling, is_leaf;

    printf("has sibling node ?(1 or 0)\n");
    scanf("%d", &has_sibling);

    if (has_sibling == 1) {
        printf("please input a intger as a tree node\n");
        scanf("%d", &x);
        temp_cell = (tree)malloc(sizeof(struct node));
        temp_cell->element = x;
        temp_cell->first_child = NULL;
        temp_cell->next_sibling = NULL;

        t->next_sibling = temp_cell;

        init(temp_cell);
    }

    printf("has child node ?(1 or 0)\n");
    scanf("%d", &has_child);

    if (has_child == 1) {
        printf("please input a intger as a tree node\n");
        scanf("%d", &x);
        temp_cell = (tree)malloc(sizeof(struct node));
        temp_cell->element = x;
        temp_cell->first_child = NULL;
        temp_cell->next_sibling = NULL;

        t->first_child = temp_cell;

        init(temp_cell);
    }
}

tree create(element_type x)
{
    tree t;

    t = (tree)malloc(sizeof(struct node));
    if (NULL == t)
        fatal_error("out of space");

    t->first_child = NULL;
    t->next_sibling = NULL;
    t->element = x;

    // init(t);

    return t;
}

void make_empty(tree t)
{
    //置空一個 tree
    //遞迴呼叫每個節點銷燬
    if (NULL != t) {
        make_empty(t->first_child);
        make_empty(t->next_sibling);
        free(t);
    }
}

int is_empty(tree t)
{
    return t == NULL;
}

// 插入做得比較簡單
void insert(element_type x, tree t, int is_child)
{
    tree temp_cell;
    temp_cell = (tree)malloc(sizeof(struct node));
    temp_cell->first_child = NULL;
    temp_cell->next_sibling = NULL;
    temp_cell->element = x;
    //插那個節點,左插還是右插
    if (t->first_child == NULL)
        t->first_child = temp_cell;
    else
        t->next_sibling = temp_cell;
}

void rand_tree(tree t)
{
    tree temp_cell;
    static int hight = 0, width = 0, n = 1;

    if (n <= 10) {
        srand((unsigned)time(NULL));
        if ( (rand() % 2) == 1 && n != 1 ) {
            srand((unsigned)time(NULL) + hight + width);
            temp_cell = (tree)malloc(sizeof(struct node));
            temp_cell->first_child = NULL;
            temp_cell->next_sibling = NULL;
            temp_cell->element = rand() % 100 + 1;

            t->next_sibling = temp_cell;
            width++;
            n++;
            rand_tree(temp_cell);
        }

        if ( (rand() % 2) == 1 ) {
            srand((unsigned)time(NULL) + hight + width);
            temp_cell = (tree)malloc(sizeof(struct node));
            temp_cell->first_child = NULL;
            temp_cell->next_sibling = NULL;
            temp_cell->element = rand() % 100 + 1;

            t->first_child = temp_cell;
            hight++;
            width = 0;
            n++;
            rand_tree(temp_cell);
        }
    }

}

void print_tree(tree t)
{
    if (NULL != t) {
        printf("%d", t->element);   //先序遍歷 先節點

        if (NULL != t->next_sibling)
            printf("\t");
        print_tree(t->next_sibling);
        printf("%d", t->element);   //中序遍歷

        if (NULL != t->first_child)
            printf("\n");
        print_tree(t->first_child);
        printf("%d", t->element);   //後序遍歷
    }
}

void test()
{
    tree t;
    t = create(1);
    rand_tree(t);
    print_tree(t);
}

int main(int argc, char const *argv[])
{
    test();
    return 0;
}

高度自律,深度思考,以勤補拙

相關文章