【数据结构08】二叉树

二叉树,其实只需要一个root的Node节点就可以完成对二叉树的基本所有操作,所以下面额外封装的Tree结构其实没什么必要。

C语言代码实现

/*************************************************************************
    > File Name: tree.c
    > Author: jby
    > Mail: 
    > Created Time: Tue 18 Jul 2023 07:47:58 AM CST
 ************************************************************************/

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

typedef struct Node {
    int val;
    struct Node *left, *right;
} Node;

typedef struct Tree {
    Node *root;
    int len; // 树的节点数量
} Tree;

Node *initNode(int val) {
    Node *n = (Node *)malloc(sizeof(Node));
    n->val = val;
    n->left = n->right = NULL;
    return n;
}

void freeNode(Node *p) {
    if (!p) return ;
    free(p);
    return ;
}

Tree *initTree() {
    Tree *t = (Tree *)malloc(sizeof(Tree));
    t->root = NULL;
    t->len = 0;
    return t;
}

Node *insert(Node *root, int val) {
    if (!root) return initNode(val);
    if (val == root->val) return root;
    if (val > root->val) root->right = insert(root->right, val);
    else root->left = insert(root->left, val);
    return root;
}

void insertTree(Tree *t, int val) {
    if (!t) return ;
    t->root = insert(t->root, val);
    t->len++;
    return ;
}

void preorderTrav(Node *root) {
    if (!root) return ;
    printf("%d, ", root->val);
    preorderTrav(root->left);
    preorderTrav(root->right);
    return ;
}

void inorderTrav(Node *root) {
    if (!root) return ;
    inorderTrav(root->left);
    printf("%d, ", root->val);
    inorderTrav(root->right);
    return ;
}

void postorderTrav(Node *root) {
    if (!root) return ;
    postorderTrav(root->left);
    postorderTrav(root->right);
    printf("%d, ", root->val);
    return ;
}

void freeAll(Node *root) {
    if (!root) return ;
    freeAll(root->left);
    freeAll(root->right);
    freeNode(root);
    return ;
}

void preorderTree(Tree *t) {
    if (!t) return ;
    printf("Pre:[");
    preorderTrav(t->root);
    printf("]\n");
    return ;
}

void inorderTree(Tree *t) {
    if (!t) return ;
    printf("In :[");
    inorderTrav(t->root);
    printf("]\n");
    return ;
}

void postorderTree(Tree *t) {
    if (!t) return ;
    printf("In :[");
    postorderTrav(t->root);
    printf("]\n");
    return ;
}

void freeTree(Tree *t) {
    if (!t) return ;
    freeAll(t->root);
    free(t);
    return ;
}

void outputTable(Node *root) {
    if (!root) return ;
    printf("%d" , root->val);
    if (!root->left && !root->right) return ;
    printf("(");
    outputTable(root->left);
    printf(",");
    outputTable(root->right);
    printf(")");
    return ;
}

void outputTree(Tree *t) {
    if (!t) return ;
    printf("Table:[");
    outputTable(t->root);
    printf("]\n");
}

Node *findNode(Node *root, int val) {
    if (!root) return NULL;
    if (root->val == val) return root;
    if (root->val < val) return findNode(root->right, val);
    else return findNode(root->left, val);
}

Node *findTree(Tree *t, int val) {
    if (!t) return NULL;
    return findNode(t->root, val);
}

int main () {
    srand(time(0));
    Tree *t = initTree();
    int cnt = 10;
    int want = 0;
    while (cnt--) {
        int val = rand() % 10;
        insertTree(t, val);
        if (cnt == 5) want = val;
    }
    outputTree(t);
    preorderTree(t);
    inorderTree(t);
    postorderTree(t);
    Node *res = findTree(t, want);
    res ? printf("find %d at %#x, val = %d\n", want, res, res->val) : printf("not found~\n");
    freeTree(t);
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值