代码总结
二叉树
结点的子树个数为结点的度,度为0即没有孩子结点的结点称为叶子节点,规定根结点为树的第一层,节点的最大层数就是树的深度,在树上,从一个结点出发可以访问到其余的结点,并且一个结点到另一个结点的路径有且仅有一条
二叉树的第 i 层最多有 2^(i-1) 个结点
深度为 k 的二叉树最多有 2^k - 1 个结点
记一颗二叉树结点数为 n ,度为 0,1,2的结点分别为 n0,n1,n2,则 n=n0+n1+n2,可知一颗二叉树有 n-1条边,度为0,1,2的结点分别可以衍生出0,1,2条边,则 n-1==n1+2n2,合并可得:n0==n2+1;
满二叉树:若一颗深度为k的二叉树有2^k-1个结点,也就是说,该深度已经无法再添加结点了,就称为满二叉树
完全二叉树:若一颗深度为k的二叉树,第1至(k-1)层为满二叉树,第k层的结点都集中在左边,我们就称之为完全二叉树
二叉树的建立
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *lchild, *rchild;
} Node;
Node* init(int data)
{
Node *node =(Node *)malloc(sizeof(Node));
node->data = data;
node->lchild = NULL;
node->rchild = NULL;
return node;
}
Node *build_demo()//建立二叉树
{
Node *node=init(1);
node->lchild=init(2);
node->rchild=init(3);
node->lchild->lchild=init(4);
node->lchild->rchild=init(5);
node->rchild->rchild=init(6);
return node;
}
void clear(Node *node)
{
if (node->lchild != NULL)
{
clear(node->lchild);
}
if (node->rchild != NULL)
{
clear(node->rchild);
}
free(node);
}
int main()
{
Node *root=build_demo();
clear(root);
return 0;
}
二叉树的遍历
先序遍历:根结点 -> 左子树 -> 右子树
中序遍历:左子树 -> 根结点 -> 右子树
后序遍历:左子树 -> 右子树 -> 根结点
代码:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node
{
int data;
struct Node *lchild, *rchild;
} Node;
Node* init(int data)
{
Node *node =(Node *)malloc(sizeof(Node));
node->data = data;
node->lchild = NULL;
node->rchild = NULL;
return node;
}
Node* build_demo()
{
Node *node = init(1);
node->lchild = init(2);
node->rchild = init(3);
node->lchild->lchild = init(4);
node->lchild->rchild = init(5);
node->rchild->rchild = init(6);
return node;
}
void preorder(Node *node)//先序遍历
{
printf("%d ",node->data);
if(node->lchild!=NULL)
{
preorder(node->lchild);
}
if(node->rchild!=NULL)
{
preorder(node->rchild);
}
}
void inorder(Node *node)//中序遍历
{
if(node->lchild!=NULL)
{
inorder(node->lchild);
}
printf("%d ",node->data);
if(node->rchild!=NULL)
{
inorder(node->rchild);
}
}
void postorder(Node *node)//后序遍历
{
if(node->lchild!=NULL)
{
postorder(node->lchild);
}
if(node->rchild!=NULL)
{
postorder(node->rchild);
}
printf("%d ",node->data);
}
void clear(Node *node)
{
if (node->lchild != NULL)
{
clear(node->lchild);
}
if (node->rchild != NULL)
{
clear(node->rchild);
}
free(node);
}
int main()
{
Node *root = build_demo();
preorder(root);
printf("\n");
inorder(root);
printf("\n");
postorder(root);
printf("\n");
clear(root);
return 0;
}
已知先序中序求后序
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node
{
int data;
struct Node *lchild, *rchild;
} Node;
Node* init(int data)
{
Node *node =(Node *)malloc(sizeof(Node));
node->data = data;
node->lchild = NULL;
node->rchild = NULL;
return node;
}
void postorder(Node *node)
{
if (node->lchild != NULL)
{
postorder(node->lchild);
}
if (node->rchild != NULL)
{
postorder(node->rchild);
}
printf("%d ", node->data);
}
// 根据先序、中序建立二叉树
Node *build(char pre_str[], char in_str[], int len)
{
Node *p=init(pre_str[0] - '0');
int pos=strchr(in_str,pre_str[0])-in_str;
//char *strchr(const char *str, int c)在参数str所指向的字符串中搜索第一次出现字符c(一个无符号字符)的位置。
//返回一个指向该字符串中第一次出现的字符的指针,如果字符串中不包含该字符则返回NULL空指针。
if(pos>0)
{
p->lchild=build(pre_str+1,in_str,pos);
}
if(len-pos-1>0)
{
p->rchild=build(pre_str+pos+1,in_str+pos+1,len-pos-1);
}
return p;
}
void clear(Node *node)
{
if (node->lchild != NULL)
{
clear(node->lchild);
}
if (node->rchild != NULL)
{
clear(node->rchild);
}
free(node);
}
int main() {
char pre_str[] = "136945827";
char in_str[] = "963548127";
Node *root=build(pre_str,in_str,strlen(pre_str));
postorder(root);
printf("\n");
clear(root);
return 0;
}
哈夫曼编码(一种无损数据压缩的编码算法)
哈夫曼编码先统计每种字母在字符串中出现的频率,根据频率建立一棵路径带权的二叉树,即哈夫曼树,将每个字母看出一个结点,结点的权值就是字母出现的频率,根结点到结点的路径就是字母的编码,频率高的字母使用短编码,频率低的字母使用长编码,这样编码后的字符串所占用空间最小
哈夫曼树的建立:
- 将每个字母结点都看作一颗只有根结点的二叉树,结点权值是字母的频率,放入一个集合中
- 取出根权值最小的两棵树a、b,构造为一棵新二叉树c,根权值为a、b之和,a、b分别为左右子树
- 删除集合中a、b,加入新二叉树c
- 重复操作直到集合中只剩下一棵二叉树,这棵树就是哈夫曼树
- 规定到左孩子结点路径权值为0,到右孩子结点路径权值为1,则每个字母的编码就是根结点到字母对应结点的路径
哈夫曼树的带权路径长度WPL==每个叶子节点到根结点的距离*叶子节点权值 之和
例:
假设这是一棵已经建立好的哈夫曼树,那么对应的各字母编码如下:
直接储存字符串的话,一字符占一字节,一字节8个比特位,那么需要 (5+5+4+3+2+2+2+1+1+1)×8==208个比特
而哈夫曼编码:WPL =5×2+5×3+4×3+3×3+2×4+2×4+2×4+1×4+1×4+1×4=82,原字符串所占比特总长度为82,显然哈夫曼编码将原数据压缩了且没有损失