main.c
#include<stdio.h>
#include<stdlib.h>
#include"tree.h"
#include"linkedList.h"
Tree * createHuffmanTree(List * l);
int main()
{
List * l = create();
List * l1 = create();
int value;
char data;
TNode * p = NULL;
while(1)
{
scanf("%c%d",&data,&value);
getchar();
if(data == '#')
break;
p = (TNode *)malloc(sizeof(TNode));
p->data = data;
p->value = value;
p->lchild = NULL;
p->rchild = NULL;
p->parent = NULL;
insert(l,p);
insert(l1,p);
}
Tree *t = createHuffmanTree(l);
destroyLkList(l);
Midorder(t->root);
}
hafuma.h
```c
#ifndef __TREE_H__
#define __TREE_H__
struct tNode
{
char data;
int value;
struct tNode * lchild;
struct tNode * rchild;
struct tNode * parent;
};
typedef struct tNode TNode;
struct tree
{
TNode * root;
int n;
};
typedef struct tree Tree;
void Preorder(TNode * root);
void Midorder(TNode * root);
void Postorder(TNode * root);
#endif
list.h
```c
#ifndef __LINKEDLIST_H__
#define __LINKEDLIST_H__
#include "tree.h"
typedef TNode * ElemType;//给int类型取一个别名 ElemType
//一个元素的数据(data)部分的类型
struct linkedListNode
{
ElemType data;//数据域 用来保存数据
struct linkedListNode *next;//存储下一个元素的地址 ,指针域
};
typedef struct linkedListNode lNode;
struct linkedList//头结点
{
lNode* first;//用来保存链表中第一个节点的地址
lNode* last;//用来保存链表中最后一个节点的地址
int n;//用来保存链表节点的个数
};//头结点
typedef struct linkedList List;
/*
功能:创建一个空链表
返回值:
返回空链表的头结点指针
*/
List * create();
void insert(List * l,ElemType x);
void printfLkList(List * l);
void destroyLkList(List * l);
void delete(List * l,ElemType x);
#endif
list.c
#include<stdio.h>
#include <stdlib.h>
#include"linkedList.h"
List * create()
{
List * l = (List *)malloc(sizeof(List));
l->first = NULL;
l->last =NULL;
l->n = 0;
return l;
}
void insert(List * l,ElemType x)
{
lNode * p = (lNode *)malloc(sizeof(lNode));
p->data = x;
p->next = NULL;
if(l->n == 0)
{
l->last = l->first = p;
l->n ++;
return ;
}
lNode * q = l->first;
lNode * r = NULL;
while(q)
{
if(q->data->value > x->value)
{
break;
}
r = q;
q = q->next;
}
if(q == l->first)
{
p->next = l->first;
l->first = p;
}
else if(q == NULL)
{
l->last->next = p;
l->last = p;
}
else
{
r->next = p;
p->next = q;
}
l->n ++;
}
void printfLkList(List * l)
{
lNode * p = l->first;
while(p)
{
printf("%c(%d) ",p->data->data,p->data->value);
p = p->next;
}
printf("\n");
}
void destroyLkList(List * l)
{
lNode *p = l->first;
while(p)
{
l->first = p->next;
p->next = NULL;
free(p);
p = l->first;
}
l->first = l->last = NULL;
l->n = 0;
free(l);
}
void delete(List * l,ElemType x)
{
lNode * p = l->first;
lNode * r = NULL;
while(p)
{
if(p->data == x)
{
break;
}
r = p;
p = p->next;
}
if(p == NULL)
{
return ;
}
else
{
if(p == l->first)
{
l->first = l->first->next;
p->next = NULL;
free(p);
}
else if(p->next == NULL)
{
r->next = NULL;
free(p);
l->last = r;
}
else
{
r->next = p->next;
p->next = NULL;
free(p);
}
l->n --;
}
}
hanfuman.c
#include <stdio.h>
#include <stdlib.h>
#include "tree.h"
#include "linkedList.h"
Tree * createHuffmanTree(List * l)
{
Tree * t = (Tree *)malloc(sizeof(Tree));
t->root = NULL;
t->n = 0;
TNode *p = NULL;
TNode *p1 = NULL;
TNode *p2 = NULL;
while(l->n > 1)
{
p1 = l->first->data;
delete(l,l->first->data);
p2 = l->first->data;
delete(l,l->first->data);
p = (TNode *)malloc(sizeof(TNode));
p->data = '#';
p->value = p1->value + p2->value;
p->lchild = p1;
p->rchild = p2;
p1->parent = p;
p2->parent = p;
insert(l, p);
}
t->root = l->first->data;
return t;
}
void Preorder(TNode * root)
{
if(root == NULL)
{
return ;
}
printf("%d ",root->data);
Preorder(root->lchild);
Preorder(root->rchild);
}
void Midorder(TNode * root)
{
if(root == NULL)
{
return ;
}
Midorder(root->lchild);
printf("%c(%d) ",root->data,root->value);
Midorder(root->rchild);
}
void Postorder(TNode * root)
{
if(root == NULL)
{
return ;
}
Postorder(root->lchild);
Postorder(root->rchild);
printf("%d ",root->data);
}
|