定义一个二叉树的二叉链表。 (2)编写二叉树的先序和中序遍历算法。 (3)编写构造哈夫曼树和求哈夫曼编码的算法。 (4)设计菜单,以选择菜单方式进行操作,并实现上述算法
时间: 2024-02-28 09:57:00 浏览: 70
好的,我来回答你的问题。
1. 二叉树的二叉链表定义:
二叉树的二叉链表是指每个节点都有两个指针,一个指向左子树,一个指向右子树。如果左子树或右子树为空,则对应指针为空。二叉树的二叉链表可以用如下结构体表示:
```
typedef struct BiTNode {
int data; // 节点数据
struct BiTNode *lchild; // 左子树指针
struct BiTNode *rchild; // 右子树指针
} BiTNode, *BiTree;
```
2. 二叉树的先序和中序遍历算法:
二叉树的先序遍历算法:
```
void PreOrderTraverse(BiTree T) {
if (T == NULL) {
return;
}
printf("%d ", T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
```
二叉树的中序遍历算法:
```
void InOrderTraverse(BiTree T) {
if (T == NULL) {
return;
}
InOrderTraverse(T->lchild);
printf("%d ", T->data);
InOrderTraverse(T->rchild);
}
```
3. 构造哈夫曼树和求哈夫曼编码的算法:
构造哈夫曼树的算法:
```
typedef struct {
int weight; // 权值
int parent; // 父节点
int lchild; // 左子节点
int rchild; // 右子节点
} HTNode, *HuffmanTree;
typedef char **HuffmanCode;
void CreateHuffmanTree(HuffmanTree &HT, int *w, int n) {
if (n <= 1) {
return;
}
int m = 2 * n - 1;
HT = (HuffmanTree)malloc((m + 1) * sizeof(HTNode));
for (int i = 1; i <= n; i++) {
HT[i].weight = w[i-1];
HT[i].parent = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
}
for (int i = n + 1; i <= m; i++) {
HT[i].weight = 0;
HT[i].parent = 0;
HT[i].lchild = 0;
HT[i].rchild = 0;
}
int s1, s2;
for (int i = n + 1; i <= m; i++) {
Select(HT, i - 1, s1, s2);
HT[s1].parent = i;
HT[s2].parent = i;
HT[i].lchild = s1;
HT[i].rchild = s2;
HT[i].weight = HT[s1].weight + HT[s2].weight;
}
}
void Select(HuffmanTree HT, int n, int &s1, int &s2) {
int min1, min2;
for (int i = 1; i <= n; i++) {
if (HT[i].parent == 0) {
if (min1 == 0 || HT[i].weight < HT[min1].weight) {
min2 = min1;
min1 = i;
} else if (min2 == 0 || HT[i].weight < HT[min2].weight) {
min2 = i;
}
}
}
s1 = min1;
s2 = min2;
}
```
求哈夫曼编码的算法:
```
void HuffmanCoding(HuffmanTree HT, HuffmanCode &HC, int n) {
HC = (HuffmanCode)malloc((n + 1) * sizeof(char *));
char *cd = (char *)malloc(n * sizeof(char));
cd[n-1] = '\0';
for (int i = 1; i <= n; i++) {
int start = n - 1;
int c = i;
int f = HT[i].parent;
while (f != 0) {
--start;
if (HT[f].lchild == c) {
cd[start] = '0';
} else {
cd[start] = '1';
}
c = f;
f = HT[f].parent;
}
HC[i] = (char *)malloc((n - start) * sizeof(char));
strcpy(HC[i], &cd[start]);
}
free(cd);
}
```
4. 菜单设计:
```
void Menu() {
printf("1. 构造哈夫曼树\n");
printf("2. 求哈夫曼编码\n");
printf("3. 二叉树的先序遍历\n");
printf("4. 二叉树的中序遍历\n");
printf("0. 退出\n");
}
int main() {
int w[] = {5, 6, 7, 8, 9};
HuffmanTree HT;
HuffmanCode HC;
int n = sizeof(w) / sizeof(w[0]);
int choice;
while (1) {
Menu();
printf("请输入选择:");
scanf("%d", &choice);
switch (choice) {
case 1:
CreateHuffmanTree(HT, w, n);
printf("哈夫曼树构造完成。\n");
break;
case 2:
HuffmanCoding(HT, HC, n);
printf("哈夫曼编码求解完成。\n");
for (int i = 1; i <= n; i++) {
printf("%d: %s\n", w[i-1], HC[i]);
}
break;
case 3:
printf("二叉树的先序遍历结果为:");
PreOrderTraverse(T);
printf("\n");
break;
case 4:
printf("二叉树的中序遍历结果为:");
InOrderTraverse(T);
printf("\n");
break;
case 0:
exit(0);
default:
printf("输入有误,请重新输入!\n");
break;
}
}
return 0;
}
```
阅读全文