用一段C语言实现(1) 输入字符序列,建立二叉链表。 (2) 中序遍历二叉树:递归算法。 (3)中序遍历二叉树:非递归算法。 (4)求二叉树的高度 。 (5)为N个权值设计哈夫曼编码。
时间: 2024-01-13 16:03:40 浏览: 72
这是基本数据结构及其算法的C++实现,涉及单链表、双链表、栈 、二叉树、并查集、哈夫曼树、平衡二叉树、二叉搜索树等.zip
好的,下面是 C 语言的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
// 定义二叉树节点结构体
typedef struct TreeNode {
char data;
struct TreeNode *left;
struct TreeNode *right;
} TreeNode;
// 建立二叉树的函数
TreeNode* createTree() {
char ch;
scanf("%c", &ch);
if (ch == '#') {
return NULL;
}
TreeNode *node = (TreeNode*)malloc(sizeof(TreeNode));
node->data = ch;
node->left = createTree();
node->right = createTree();
return node;
}
// 中序遍历二叉树的递归算法
void inorderRecursive(TreeNode *root) {
if (root == NULL) {
return;
}
inorderRecursive(root->left);
printf("%c ", root->data);
inorderRecursive(root->right);
}
// 中序遍历二叉树的非递归算法
void inorderNonrecursive(TreeNode *root) {
TreeNode *stack[100];
int top = -1;
TreeNode *curr = root;
while (top != -1 || curr != NULL) {
while (curr != NULL) {
stack[++top] = curr;
curr = curr->left;
}
curr = stack[top--];
printf("%c ", curr->data);
curr = curr->right;
}
}
// 求二叉树的高度
int getHeight(TreeNode *root) {
if (root == NULL) {
return 0;
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return leftHeight > rightHeight ? leftHeight + 1 : rightHeight + 1;
}
// 定义哈夫曼树节点结构体
typedef struct HuffmanNode {
int weight;
char data;
int parent;
int leftChild;
int rightChild;
} HuffmanNode;
// 找到两个权值最小的节点
void findMin(HuffmanNode *huffmanTree, int n, int *index1, int *index2) {
int min1 = 100000, min2 = 100000;
for (int i = 1; i <= n; i++) {
if (huffmanTree[i].parent == 0 && huffmanTree[i].weight < min1) {
min2 = min1;
*index2 = *index1;
min1 = huffmanTree[i].weight;
*index1 = i;
} else if (huffmanTree[i].parent == 0 && huffmanTree[i].weight < min2) {
min2 = huffmanTree[i].weight;
*index2 = i;
}
}
}
// 创建哈夫曼树
void createHuffmanTree(HuffmanNode *huffmanTree, int n) {
int m = 2 * n - 1;
for (int i = 1; i <= m; i++) {
huffmanTree[i].weight = 0;
huffmanTree[i].parent = 0;
huffmanTree[i].leftChild = 0;
huffmanTree[i].rightChild = 0;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &huffmanTree[i].weight);
getchar();
huffmanTree[i].data = getchar();
}
for (int i = n + 1; i <= m; i++) {
int index1, index2;
findMin(huffmanTree, i - 1, &index1, &index2);
huffmanTree[i].weight = huffmanTree[index1].weight + huffmanTree[index2].weight;
huffmanTree[i].leftChild = index1;
huffmanTree[i].rightChild = index2;
huffmanTree[index1].parent = i;
huffmanTree[index2].parent = i;
}
}
// 哈夫曼编码
void huffmanCoding(HuffmanNode *huffmanTree, int n) {
char *code[n];
char *temp = (char*)malloc(n*sizeof(char));
temp[n-1] = '\0';
for (int i = 1; i <= n; i++) {
int start = n - 1;
int curr = i;
int parent = huffmanTree[curr].parent;
while (parent != 0) {
if (huffmanTree[parent].leftChild == curr) {
temp[--start] = '0';
} else {
temp[--start] = '1';
}
curr = parent;
parent = huffmanTree[curr].parent;
}
code[i-1] = (char*)malloc((n-start)*sizeof(char));
for (int j = start; j < n; j++) {
code[i-1][j-start] = temp[j];
}
printf("%c: %s\n", huffmanTree[i].data, code[i-1]);
}
}
int main() {
// 1. 输入字符序列,建立二叉链表
TreeNode *root = createTree();
// 2. 中序遍历二叉树的递归算法
inorderRecursive(root);
printf("\n");
// 3. 中序遍历二叉树的非递归算法
inorderNonrecursive(root);
printf("\n");
// 4. 求二叉树的高度
printf("Height of the tree: %d\n", getHeight(root));
// 5. 为N个权值设计哈夫曼编码
int n;
printf("Enter the number of characters: ");
scanf("%d", &n);
getchar();
HuffmanNode huffmanTree[2*n-1];
createHuffmanTree(huffmanTree, n);
huffmanCoding(huffmanTree, n);
return 0;
}
```
阅读全文