哈夫曼树的构造与遍历----树-二叉树-扩充二叉树-哈夫曼树

//树-二叉树-扩充二叉树-哈夫曼树
#include<stdio.h>
#include<stdlib.h>
typedef struct btnode {
    
    
	int element;
	struct btnode* Lchild, * Rchild;
}BTNode;
typedef struct btree {
    
    
	struct btnode* Root;
}BTree;
BTNode* NewNode() {
    
    
	BTNode* p = (BTNode*)malloc(sizeof(BTNode));
	return p;
}
void CreateBT(BTree* bt) {
    
    
	bt->Root = NULL;
}
void MakeBT(BTree* bt, int x, BTree* lt, BTree* rt) {
    
    
	BTNode* p = NewNode();
	p->element = x;
	p->Rchild = rt->Root;
	p->Lchild = lt->Root;
	lt->Root = rt->Root = NULL;
	bt->Root = p;
}
void BreakBT(BTree* bt, BTree* lt, BTree* rt) {
    
    
	BTNode* p = bt->Root;
	if (p) {
    
    
		lt->Root = p->Lchild;
		rt->Root = p->Rchild;
		bt->Root = NULL;
		free(p);
	}
}
void Visit(BTNode* p) {
    
    
	printf("%d ", p->element);
}
void PreOrd(void (*Visit)(BTNode* u), BTNode* t) {
    
    
	if (t) {
    
    
		(*Visit)(t);
		PreOrd(Visit, t->Lchild);
		PreOrd(Visit, t->Rchild);
	}
}
void InOrd(void (*Visit)(BTNode* u), BTNode* t) {
    
    
	if (t) {
    
    
		InOrd(Visit, t->Lchild);
		(*Visit)(t);
		InOrd(Visit, t->Rchild);
	}
}
void PostOrd(void (*Visit)(BTNode* u), BTNode* t) {
    
    
	if (t) {
    
    
		PostOrd(Visit, t->Lchild);
		PostOrd(Visit, t->Rchild);
		(*Visit)(t);
	}
}
void PreOrder(BTree* bt, void  (*Visit)(BTNode* u)) {
    
    
	PreOrd(Visit, bt->Root);
}
void InOrder(BTree* bt, void  (*Visit)(BTNode* u)) {
    
    
	InOrd(Visit, bt->Root);
}
void PostOrder(BTree* bt, void  (*Visit)(BTNode* u)) {
    
    
	PostOrd(Visit, bt->Root);
}
void LevelOrd(BTree bt) {
    
    
	BTNode* p;
	BTNode* queue[256];
	int front = -1, rear = -1;
	p = bt.Root;
	rear++;
	queue[rear] = p;
	while (front != rear) {
    
    
		front++;
		p = queue[front];
		printf("%d ", p->element);
		if (p->Lchild != NULL) {
    
    
			rear++;
			queue[rear] = p->Lchild;
		}
		if (p->Rchild != NULL) {
    
    
			rear++;
			queue[rear] = p->Rchild;
		}
	}
	printf("\n");
}
void Fmin(BTree ht[], int**minIndx1, int** minIndex2, int k) {
    
    
	int i, j, k1, k2;
	int temp, temp2;
	temp = ht[0].Root->element;
	k1 = 0;
	for (i = 0; i <= k; i++) {
    
    
		if (temp > ht[i].Root->element) {
    
    
			temp = ht[i].Root->element;
			k1 = i;
		}
	}
	*minIndx1 = &k1;
	if (k1 != 0) {
    
    
		temp2 = ht[0].Root->element;
		k2 = 0;
		for (j = 0; j <= k; j++) {
    
    
			if (k1 == j) {
    
    
				continue;
			}
			if (temp2 > ht[j].Root->element) {
    
    
				temp2 = ht[j].Root->element;
				k2 = j;
			}
		}
	}
	else {
    
    
		temp2 = ht[1].Root->element;
		k2 = 1;
		for (j = 1; j <= k; j++) {
    
    
			if (temp2 > ht[j].Root->element) {
    
    
				temp2 = ht[j].Root->element;
				k2 = j;
			}
		}
	}
	*minIndex2 = &k2;
}
BTree CreateHFMTree(int w[],int n) {
    
    
	BTree ht[256];
	int i, k, *k1 = 0, *k2 = 0, k2Temp;
	BTree* zero;
	zero = (BTree*)malloc(sizeof(BTree));
	CreateBT(zero);
	for (i = 0; i < n; i++) {
    
    
		MakeBT(&ht[i], w[i], zero, zero);  //初始化森林F,其元素都是一个个二叉树,其左右孩子都为NULL
	}
	for (k = n - 1; k > 0; k--) {
    
    
		Fmin(ht,&k1,&k2, k);   //返回根节点值最小的二叉树在BTree ht[]中的索引值
		k2Temp = *k2; //指针K1, K2会在下一步运算中被清空,这里要保存 k2 所指向的索引值
		MakeBT(&ht[*k1], ht[*k1].Root->element + ht[*k2].Root->element, &ht[*k1], &ht[*k2]);
		ht[k2Temp] = ht[k];
	}
	return ht[0];
}

void main() {
    
    
	BTree bt;
	int w[] = {
    
    9,11,13,3,5,12};
	
	bt = CreateHFMTree(w,sizeof(w)/sizeof(int));
	PreOrder(&bt, Visit);
	printf("\n");
	LevelOrd(bt);
	printf("\n");
}

おすすめ

転載: blog.csdn.net/m0_47472749/article/details/121865425