树
一对多
节点:集合 节点的数量=边数+1
边:关系,集合与集合的关系。可以通过指针域完成。
根:全集,子集取并集
子节点:子集
树的深度、高度:最长路径节点的个数。
节点的深度:从上往下数节点。向下看
节点的高度:从下往上数节点。向上看
节点的度:边向外指向的数量。
二叉树
任何N叉树都可转成二叉树(左孩子,右兄弟,十字链表)。从非确定性问题道确定性问题的转变。
- 每一节点度最多为2。
- 度为0的节点比度为2的节点多一个。节点的数量=边数+1
二叉树的遍历(递归)
- 前序遍历:根 左 右
- 中序遍历:左 根 右(二叉排序树)
- 后序遍历:左 右 根
完全二叉树
- 编号为i的子节点:
左边号:2 * i
右编号:2 * i + 1
- 可以用连续空间储存(数组)
结构定义
typedef struct Node {
int data;
struct Node *lchild, *rchild;
} Node;
typedef struct Tree {
Node *root;
int length;
} Tree;
初始化
Node *getNewNode(int val) {
Node *p = (Node *)malloc(sizeof(Node));
p->data = val;
p->lchild = p->rchild = NULL;
return p;
}
Tree *getNewTree() {
Tree *tree = (Tree *)malloc(sizeof(Tree));
tree->root = NULL;
tree->length = 0;
return tree;
}
插入
Node *insert_node(Node *root, int val, int *flag) {
if (root == NULL) {
*flag = 1;
return getNewNode(val);
}
if (root->data == val) return root;
if (root->data > val) root->lchild = insert_node(root->lchild, val, flag);
else root->rchild = insert_node(root->rchild, val, flag);
return root;
}
void insert(Tree *tree, int val) {
if (tree == NULL) return ;
int flag = 0;
tree->root = insert_node(tree->root, val, &flag);
tree->length += flag;
return ;
}
清除
void clear_node(Node *root){
if (root = NULL) return;
clear_node(root->lchild);
clear_node(root->rchild);
free(root);
return ;
}
void clear(Tree *tree) {
if (tree == NULL) return ;
clear_node(tree->root);
free(tree);
return ;
}
插入
Node *insert_node(Node *root, int val, int *flag) {
if (root == NULL) {
*flag = 1;
return getNewNode(val);
}
if (root->data == val) return root;
if (root->data > val) root->lchild = insert_node(root->lchild, val, flag);
else root->rchild = insert_node(root->rchild, val, flag);
return root;
}
void insert(Tree *tree, int val) {
if (tree == NULL) return ;
int flag = 0;
tree->root = insert_node(tree->root, val, &flag);
tree->length += flag;
return ;
}
遍历
void pre_order_node(Node *root) {
if (root == NULL ) return ;
printf(" %d", root->data);
pre_order_node(root->lchild);
pre_order_node(root->rchild);
return ;
}
void pre_order(Tree *tree) {
if (tree == NULL) return ;
printf("pre_order : ");
pre_order_node(tree->root);
printf("\n");
return ;
}
void in_order_node(Node *root) {
if (root == NULL ) return ;
in_order_node(root->lchild);
printf(" %d", root->data);
in_order_node(root->rchild);
return ;
}
void in_order(Tree *tree) {
if (tree == NULL) return ;
printf("in_order : ");
in_order_node(tree->root);
printf("\n");
return ;
}
void post_order_node(Node *root) {
if (root == NULL ) return ;
post_order_node(root->lchild);
post_order_node(root->rchild);
printf(" %d", root->data);
return ;
}
void post_order(Tree *tree) {
if (tree == NULL) return ;
printf("post_order : ");
post_order_node(tree->root);
printf("\n");
return ;
}
输出
void output_node(Node *root) {
if (root == NULL) return ;
printf(" %d", root->data);
if (root->lchild == NULL && root->rchild == NULL) return ;
printf("(");
output_node(root->lchild);
printf(",");
output_node(root->rchild);
printf(")");
return ;
}
void output(Tree *tree) {
if (tree == NULL) return ;
printf("tree (%d): ", tree->length);
output_node(tree->root);
printf("\n");
return ;
}
完整代码
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
typedef struct Node {
int data;
struct Node *lchild, *rchild;
} Node;
typedef struct Tree {
Node *root;
int length;
} Tree;
Node *getNewNode(int val) {
Node *p = (Node *)malloc(sizeof(Node));
p->data = val;
p->lchild = p->rchild = NULL;
return p;
}
Tree *getNewTree() {
Tree *tree = (Tree *)malloc(sizeof(Tree));
tree->root = NULL;
tree->length = 0;
return tree;
}
Node *insert_node(Node *root, int val, int *flag) {
if (root == NULL) {
*flag = 1;
return getNewNode(val);
}
if (root->data == val) return root;
if (root->data > val) root->lchild = insert_node(root->lchild, val, flag);
else root->rchild = insert_node(root->rchild, val, flag);
return root;
}
void insert(Tree *tree, int val) {
if (tree == NULL) return ;
int flag = 0;
tree->root = insert_node(tree->root, val, &flag);
tree->length += flag;
return ;
}
void clear_node(Node *root){
if (root = NULL) return;
clear_node(root->lchild);
clear_node(root->rchild);
free(root);
return ;
}
void clear(Tree *tree) {
if (tree == NULL) return ;
clear_node(tree->root);
free(tree);
return ;
}
void pre_order_node(Node *root) {
if (root == NULL ) return ;
printf(" %d", root->data);
pre_order_node(root->lchild);
pre_order_node(root->rchild);
return ;
}
void pre_order(Tree *tree) {
if (tree == NULL) return ;
printf("pre_order : ");
pre_order_node(tree->root);
printf("\n");
return ;
}
void in_order_node(Node *root) {
if (root == NULL ) return ;
in_order_node(root->lchild);
printf(" %d", root->data);
in_order_node(root->rchild);
return ;
}
void in_order(Tree *tree) {
if (tree == NULL) return ;
printf("in_order : ");
in_order_node(tree->root);
printf("\n");
return ;
}
void post_order_node(Node *root) {
if (root == NULL ) return ;
post_order_node(root->lchild);
post_order_node(root->rchild);
printf(" %d", root->data);
return ;
}
void post_order(Tree *tree) {
if (tree == NULL) return ;
printf("post_order : ");
post_order_node(tree->root);
printf("\n");
return ;
}
void output_node(Node *root) {
if (root == NULL) return ;
printf(" %d", root->data);
if (root->lchild == NULL && root->rchild == NULL) return ;
printf("(");
output_node(root->lchild);
printf(",");
output_node(root->rchild);
printf(")");
return ;
}
void output(Tree *tree) {
if (tree == NULL) return ;
printf("tree (%d): ", tree->length);
output_node(tree->root);
printf("\n");
return ;
}
int main() {
srand(time(0));
Tree *tree = getNewTree();
#define MAX_OP 20
for (int i = 0; i < MAX_OP; i++) {
int val = rand() % 100;
insert(tree, val);
output(tree);
}
pre_order(tree);
in_order(tree);
post_order(tree);
clear(tree);
#undef MAX_OP
return 0;
}