二叉树,其实只需要一个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;
}