主函数main.c
#include"Huffman_tree.h"
#include<stdio.h>
#include<stdlib.h>
int main()
{
int datas[] = {3,6,4,5,7,8};
int temp1, temp2, flag = 0;
QUEUE queue = create_Queue();
Stack stack = create_stack();
for(int i = 0; i < sizeof(datas)/sizeof(int); i++)
{
push_queue(queue, datas[i]);
exchange_Queue(queue);
}
while(1)
{
temp1 = pop_queue(queue);
push_stack(stack, temp1);
temp2 = pop_queue(queue);
push_stack(stack, temp2);
if(isempty(queue))
{
break;
}
push_queue(queue, temp1+temp2);
}
push_queue(queue, temp1+temp2);
temp1 = pop_queue(queue);
push_stack(stack, temp1);
Snode s = stack->top;
int n = turn(s);
int keys[n];
while(stack->top != NULL)
{
keys[flag] = pop_stack(stack);
flag++;
}
pNode root = create_tree(keys, sizeof(keys)/sizeof(int));
pre_order(root);
return 0;
}
Huffman_tree.h
#ifndef _HUFFMAN_TREE_H_
#define _HUFFMAN_TREE_H_
typedef struct n
{
int data;
struct n *next;
}Node, *NODE;
typedef struct q
{
struct n *head;
struct n *tail;
}Queue, *QUEUE;
typedef struct Tree
{
int data;
struct Tree *left;
struct Tree *right;
}*pNode;
typedef struct s
{
int data;
struct s *next;
}*Snode;
typedef struct stack
{
struct s *top;
}*Stack;
pNode create_tree();
pNode create_node();
Stack create_stack();
Stack push_stack(Stack stack, int data);//入栈
int pop_stack(Stack stack);//出栈
void print_stack(Stack stack);//输出栈
QUEUE create_Queue();
NODE push_queue(QUEUE queue, int data);
int isempty(QUEUE queue);//判断队列是否为空
int pop_queue(QUEUE queue);//出队
void exchange_Queue(QUEUE queue);//队列排序
void print(QUEUE queue);//输出队列
void pre_order(pNode root);
int turn(Snode stack);
#endif
Huffman_tree.c
#include"Huffman_tree.h"
#include<stdio.h>
#include<stdlib.h>
#include<assert.h>
#include<unistd.h>
void pre_order(pNode root);
int depth_tree(pNode root, pNode node);
pNode create_node(int data)//创建结点
{
pNode node = (pNode)malloc(sizeof(pNode));
assert(node != NULL);
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
pNode insert_node(pNode root, pNode node)
{
if(root == NULL || node == NULL)
{
return NULL;
}
if(root->left == NULL && root->right == NULL)
{
root->right = node;
}
else if(root->left == NULL && root->right != NULL)
{
root->left = node;
}
else
{
if(depth_tree(root, root->right) <= depth_tree(root, root->left))
{
insert_node(root->right, node);
}
else
{
insert_node(root->left, node);
}
}
return root;
}
pNode create_tree(int *keys, int len)//创建树
{
pNode root = create_node(keys[0]);
for(int i = 1; i < len; i++)
{
pNode node = create_node(keys[i]);
insert_node(root, node);
}
return root;
}
Stack create_stack()//创建栈
{
Stack stack = (Stack)malloc(sizeof(Stack));
assert(stack != NULL);
stack->top = NULL;
return stack;
}
int isempty_stack(Stack stack)//判断栈是否为空
{
return stack->top == NULL;
}
Stack push_stack(Stack stack, int data)//入栈
{
Stack S = stack;
Snode pnew = (Snode)malloc(sizeof(Snode));
assert(pnew != NULL);
pnew->data = data;
pnew->next = NULL;
if(!isempty_stack(S))
{
pnew->next = S->top;
}
S->top = pnew;
}
void delete_stack(Stack stack)
{
Snode S = stack->top;
stack->top = stack->top->next;
free(S);
}
int pop_stack(Stack stack)//出栈
{
Snode S = stack->top;
int temp = S->data;
delete_stack(stack);
return temp;
}
QUEUE create_Queue()//创建队列
{
QUEUE queue = (QUEUE)malloc(sizeof(QUEUE));
assert(queue != NULL);
queue->head = NULL;
queue->tail = NULL;
return queue;
}
int isempty(QUEUE queue)//判断队列是否为空
{
return queue->head == NULL;
}
NODE push_queue(QUEUE queue, int data)//入队
{
QUEUE q = queue;
NODE pnew = (NODE)malloc(sizeof(NODE));
assert(pnew != NULL);
pnew->data = data;
pnew->next = NULL;
if(isempty(q))
{
q->head = pnew;
}
else
{
q->tail->next = pnew;
}
q->tail = pnew;
return pnew;
}
void delete_queue(QUEUE queue)
{
if(queue == NULL)
{
return;
}
NODE Q = queue->head;
queue->head = queue->head->next;
free(Q);
}
int pop_queue(QUEUE queue)//出队
{
NODE Q = queue->head;
int temp = Q->data;
delete_queue(queue);
return temp;
}
void exchange(int *a, int *b)//交换函数
{
int *temp;
temp = a;
a = b;
b = temp;
}
void exchange_Queue(QUEUE queue)//队列排序
{
NODE q = queue->head;
NODE p = queue->tail;
while(q != p)
{
NODE p = queue->tail;
if(p->data < q->data)
{
//exchange(&(p->data), &(q->data));
int temp;
temp = q->data;
q->data = p->data;
p->data = temp;
}
q = q->next;
}
return;
}
void print(QUEUE queue)//输出队列
{
NODE q = queue->head;
while(q)
{
printf("%d-", q->data);
q = q->next;
}
return;
}
void print_stack(Stack stack)//输出栈
{
Snode q = stack->top;
if(q == NULL)
{
printf("空栈\n");
return;
}
while(q)
{
printf("%d--", q->data);
q = q->next;
}
return;
}
void pre_order(pNode root)
{
if(root == NULL)
{
return;
}
printf("%d--", root->data);
pre_order(root->left);
pre_order(root->right);
}
int depth_tree(pNode root, pNode node)
{
int i = 1;
if(root == NULL)
{
return 0;
}
while(node != NULL)
{
i++;
node = node->left;
}
return i;
}
int turn(Snode stack)
{
int temp = 0;
while(stack != NULL)
{
temp++;
stack = stack->next;
}
return temp;
}