程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> 構建Huffman樹

構建Huffman樹

編輯:C++入門知識

//HuffmanTree.h

#ifndef HUFFMANTREE_H
#define HUFFMANTREE_H
#include <stdio.h>
#include <stdlib.h>

enum BOOLEAN{ FALSE, TRUE };
enum STATE{ONLINK,ONTREE,VIRTUAL};

#pragma pack(push)
#pragma pack(4)

struct _Node
{
	int iValue;
	int iState;

	struct _Node* pNext;
	//struct _Node* pParent;
	struct _Node* pLChild;
	struct _Node* pRChild;};

typedef struct _Node Node;

typedef struct
{
	Node* pRoot;
	int iSize;
}HuffTree;

#pragma pack(pop)

#endif
//HuffmanTree.c

#include "HuffmanTree.h"

HuffTree* InitTree()
{
	HuffTree* pTree = (HuffTree*)malloc( sizeof( HuffTree ));

	if( !pTree )
		return (HuffTree*)NULL;

	pTree->iSize = 0;
	pTree->pRoot = NULL;

	return pTree;
}

Node* CreateNode( int iValue, int iState = ONLINK )
{
	Node* pNode = (Node*)malloc( sizeof( Node ));

	if( !pNode )
		return NULL;

	pNode->iState = iState;
	pNode->iValue = iValue;
	pNode->pNext  = NULL;

	pNode->pLChild = NULL;
	pNode->pRChild = NULL;
	//pNode->pParent = NULL;

	return pNode;
}

Node* CreateNodeList( int weightArr[], int iArrSize )
{
	if( iArrSize <= 0 )
		return NULL;

	Node* pHeader = CreateNode( weightArr[0] );
	Node* pNode = pHeader;

	int i = 1;
	for( ; i < iArrSize; i++ )
	{
		pNode->pNext = CreateNode( weightArr[i] );

		pNode = pNode->pNext;
	}

	return pHeader;
}

int isContinue( Node* pList )
{
	int iCount = 0;

	while( pList )
	{
		pList = pList->pNext;
		if( ++iCount >= 2 )
			return 1;
	}

	return 0;

}

Node* ResortList( Node* pList, Node* pNew )
{
	if( !pList && !pNew )
		return NULL;

	if( !pList && pNew )
		return pNew;

	if( pList->iValue > pNew->iValue )
	{//加至首部
		pNew->pNext = pList;

		return pNew;
	}

	Node* pHeader = pList;
	Node* pPrior = NULL;
	while( pList )
	{
		if( pNew->iValue < pList->iValue )
		{
			break;
		}

		pPrior = pList;
		pList = pList->pNext;
	}

	if( !pList )
	{//添加到末尾
		pPrior->pNext = pNew;
		pNew->pNext = NULL;
	}
	else
	{//插入中間
		pPrior->pNext = pNew;
		pNew->pNext = pList;
	}

	return pHeader;
}

void PrintNode( Node* pNode )
{
	if( pNode )
		printf( "%d ", pNode->iValue );
}

void InOrderTraverse(  Node* pNode )
{
	if( !pNode )
		return;

	InOrderTraverse( pNode->pLChild );

	PrintNode( pNode  );
	
	InOrderTraverse( pNode->pRChild );

}

void PreOrderTraverse( Node* pNode )
{
	if( !pNode )
		return;

	PrintNode( pNode );

	PreOrderTraverse( pNode->pLChild );

	PreOrderTraverse( pNode->pRChild );
}

Node* CreateHuffTree( Node* pList  )
{//根據權值創建Huffman Tree

	if( !pList )
		return NULL;

	Node* pNew = NULL;
	while( isContinue( pList ) )
	{
		pNew = CreateNode( pList->iValue + pList->pNext->iValue, VIRTUAL );
	
		if( !pNew ) 
			return NULL;

		pNew->pLChild = pList;
		pNew->pRChild = pList->pNext;
		pNew->pNext = NULL;
		
		pList = pList->pNext->pNext;

		pNew->pLChild->pNext = NULL;
		pNew->pRChild->pNext = NULL;

		//pList->pParent = pNew;
		//pList->pNext->pParent = pNew;

		pList = ResortList( pList, pNew );
	}

	return pList;
}

int main( int argc, char** argv )
{
	HuffTree* pTree = InitTree();

	//這裡確保有序 排序略過直接使用有序數據
	int WeightArr[9] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };

	pTree->pRoot = CreateHuffTree( CreateNodeList( WeightArr, 9 ) );

	puts( "InOrderTraverse" );
	InOrderTraverse( pTree->pRoot );

	puts( "\nPreOrderTraverse" );
	PreOrderTraverse( pTree->pRoot );
	puts( "" );

	return 0;
}

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved