用一段C语言实现(1) 输入字符序列,建立二叉链表。 (2) 中序遍历二叉树:递归算法。 (3)中序遍历二叉树:非递归算法。 (4)求二叉树的高度 。 (5)为N个权值设计哈夫曼编码。及其输出示例
时间: 2024-01-16 17:03:26 浏览: 72
好的,我会尽力回答您的问题。下面是用C语言实现的代码。
1. 输入字符序列,建立二叉链表。
```
#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;
}
```
2. 中序遍历二叉树:递归算法。
```
void inorderTraversal(TreeNode *root) {
if (root != NULL) {
inorderTraversal(root->left);
printf("%c ", root->data);
inorderTraversal(root->right);
}
}
```
3. 中序遍历二叉树:非递归算法。
```
#include <stdbool.h>
#include <stdlib.h>
typedef struct StackNode {
TreeNode *data;
struct StackNode *next;
} StackNode;
typedef struct {
StackNode *top;
} Stack;
void push(Stack *stack, TreeNode *node) {
StackNode *newNode = (StackNode *) malloc(sizeof(StackNode));
newNode->data = node;
newNode->next = stack->top;
stack->top = newNode;
}
TreeNode *pop(Stack *stack) {
if (stack->top == NULL) {
return NULL;
}
StackNode *p = stack->top;
TreeNode *node = p->data;
stack->top = p->next;
free(p);
return node;
}
bool isEmpty(Stack *stack) {
return stack->top == NULL;
}
void inorderTraversal(TreeNode *root) {
Stack stack = {NULL};
TreeNode *p = root;
while (p != NULL || !isEmpty(&stack)) {
while (p != NULL) {
push(&stack, p);
p = p->left;
}
p = pop(&stack);
printf("%c ", p->data);
p = p->right;
}
}
```
4. 求二叉树的高度。
```
int getHeight(TreeNode *root) {
if (root == NULL) {
return 0;
}
int leftHeight = getHeight(root->left);
int rightHeight = getHeight(root->right);
return (leftHeight > rightHeight ? leftHeight : rightHeight) + 1;
}
```
5. 为N个权值设计哈夫曼编码,并输出示例。
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_N 100
typedef struct {
int weight;
int parent;
int leftChild;
int rightChild;
} HuffmanTreeNode;
typedef struct {
char ch;
char code[MAX_N];
} HuffmanCode;
void select(HuffmanTreeNode *nodes, int n, int *i, int *j) {
int min1 = 0x7fffffff, min2 = 0x7fffffff;
*i = *j = 0;
for (int k = 1; k <= n; k++) {
if (nodes[k].parent == 0 && nodes[k].weight < min1) {
min2 = min1;
*j = *i;
min1 = nodes[k].weight;
*i = k;
} else if (nodes[k].parent == 0 && nodes[k].weight < min2) {
min2 = nodes[k].weight;
*j = k;
}
}
}
void createHuffmanTree(HuffmanTreeNode *nodes, int n) {
for (int i = 1; i <= 2 * n - 1; i++) {
nodes[i].parent = nodes[i].leftChild = nodes[i].rightChild = 0;
}
for (int i = 1; i <= n; i++) {
scanf("%d", &nodes[i].weight);
}
for (int i = n + 1; i <= 2 * n - 1; i++) {
int x, y;
select(nodes, i - 1, &x, &y);
nodes[x].parent = nodes[y].parent = i;
nodes[i].leftChild = x;
nodes[i].rightChild = y;
nodes[i].weight = nodes[x].weight + nodes[y].weight;
}
}
void huffmanEncoding(HuffmanTreeNode *nodes, int n, HuffmanCode *codes) {
char code[MAX_N];
code[n - 1] = '\0';
for (int i = 1; i <= n; i++) {
int j = n - 1;
int k = i;
while (nodes[k].parent != 0) {
if (nodes[nodes[k].parent].leftChild == k) {
code[--j] = '0';
} else {
code[--j] = '1';
}
k = nodes[k].parent;
}
codes[i].ch = i + '0';
strcpy(codes[i].code, &code[j]);
}
}
void printHuffmanCode(HuffmanCode *codes, int n) {
printf("Huffman Codes:\n");
for (int i = 1; i <= n; i++) {
printf("%c: %s\n", codes[i].ch, codes[i].code);
}
}
int main() {
int n;
HuffmanTreeNode nodes[MAX_N * 2 - 1];
HuffmanCode codes[MAX_N];
scanf("%d", &n);
createHuffmanTree(nodes, n);
huffmanEncoding(nodes, n, codes);
printHuffmanCode(codes, n);
return 0;
}
```
以上就是题目的回答和代码实现,希望能对您有所帮助。
阅读全文