提交 194e67b8 创建 作者: 宋海霞's avatar 宋海霞

modify

上级 a5eed84a
#ifndef HUFFMANTREE_H_ #ifndef HUFFMANTREE_H_
#define HUFFMANTREE_H_ #define HUFFMANTREE_H_
// //
// Include the C standard library header file here // Include the C standard library header file here
// //
#include <stdio.h> #include <stdio.h>
// //
// Other header files are included here // Other header files are included here
// //
// //
// Define the data structure here // Define the data structure here
// //
...@@ -21,16 +18,20 @@ ...@@ -21,16 +18,20 @@
#define MAXVALUE 1000 // Define the maximum value #define MAXVALUE 1000 // Define the maximum value
// Node structure // Node structure
typedef struct HtNode{ typedef struct HtNode
{
int Weight; // The node weight int Weight; // The node weight
int parent, lchild, rchild; // The parent and the left and right child are subscripts in the array // The parent and the left and right child are subscripts in the array
int parent, lchild, rchild;
}HtNode; }HtNode;
// The structure of a Huffman tree // The structure of a Huffman tree
typedef struct _HtTree{ typedef struct _HtTree
int Count; // Number of leaf nodes {
int Root; // The index of the Huffman root node in the array int Count; // Number of leaf nodes
struct HtNode *HtArray; // The subscript of the Huffman root in an array of 2*Count-1 nodes int Root; // The index of the Huffman root node in the array
// The subscript of the Huffman root in an array of 2*Count-1 nodes
struct HtNode *HtArray;
}HtTree, *PHtTree; }HtTree, *PHtTree;
// //
......
#include "HuffmanTree.h" #include "HuffmanTree.h"
#include <stdlib.h>
int main(int argc, char* argv[]) int main(int argc, char* argv[])
{ {
...@@ -17,7 +18,7 @@ int main(int argc, char* argv[]) ...@@ -17,7 +18,7 @@ int main(int argc, char* argv[])
PHtTree pTree = (PHtTree)malloc(sizeof(HtTree)); PHtTree pTree = (PHtTree)malloc(sizeof(HtTree));
pTree->HtArray = (HtNode*)malloc(sizeof(HtNode) * HuffmanLength); pTree->HtArray = (HtNode*)malloc(sizeof(HtNode) * HuffmanLength);
for(i = 0; i < HuffmanLength; i++) for (i = 0; i < HuffmanLength; i++)
{ {
pTree->HtArray[i].lchild = -1; pTree->HtArray[i].lchild = -1;
pTree->HtArray[i].rchild = -1; pTree->HtArray[i].rchild = -1;
...@@ -54,7 +55,8 @@ returned value: ...@@ -54,7 +55,8 @@ returned value:
*/ */
PHtTree HuffmanTree(PHtTree pTree, int Count) PHtTree HuffmanTree(PHtTree pTree, int Count)
{ {
int i, j; // Cursor, which is mainly used to find the index of the smallest node and the second smallest node // Cursor, which is mainly used to find the index of the smallest node and the second smallest node
int i, j;
int Index1, Index2; // Holds the subscript of the smallest and subsmallest nodes int Index1, Index2; // Holds the subscript of the smallest and subsmallest nodes
int Number1, Number2; // Store minimum and sub - small node weights int Number1, Number2; // Store minimum and sub - small node weights
...@@ -69,17 +71,12 @@ void OutputResult(PHtTree pTree, int Length) ...@@ -69,17 +71,12 @@ void OutputResult(PHtTree pTree, int Length)
{ {
int i; int i;
printf("subscript\tweight\tparent\tlchild\trchild\n"); printf("subscript\tweight\tparent\tlchild\trchild\n");
for(i = 0; i < Length; i++) for (i = 0; i < Length; i++)
{ {
printf("%d", i); printf("%d", i);
printf("\t\t%d",pTree->HtArray[i].Weight); printf("\t\t%d", pTree->HtArray[i].Weight);
printf("\t\t%d",pTree->HtArray[i].parent); printf("\t\t%d", pTree->HtArray[i].parent);
printf("\t\t%d",pTree->HtArray[i].lchild); printf("\t\t%d", pTree->HtArray[i].lchild);
printf("\t\t%d\n",pTree->HtArray[i].rchild); printf("\t\t%d\n", pTree->HtArray[i].rchild);
} }
} }
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论