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

二叉樹基本操作

編輯:C++入門知識

//BiTree.h

#ifndef BITREE_H
#define BITREE_H

#include <stdio.h>
#include <stdlib.h>
#define ERROR -1 
#define OVERFLOW -2 
#define SUCCESS 0

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

struct _Node
{
	int iValue;
	struct _Node* pParent;
	struct _Node* pLChild;
	struct _Node* pRChild;
};

typedef struct _Node Node;

typedef struct
{
	Node* pRoot;//Root
	int iSize;
}BiTree;

#pragma pack(pop)


BiTree* InitTree();
Node* CreateNode( int iValue );
void ClearTree( BiTree** pTree );
void DestroyTree( BiTree** pTree );
int TreeEmpty( BiTree* pTree );
int GetTreeDepth( Node* pNode );
Node* GetRoot( BiTree* pTree );
void Assign( BiTree* pTree, Node* pNode, Node* pValue );
Node* GetParent( BiTree* pTree, Node* pNode );
Node* GetLeftChild( BiTree* pTree, Node* pNode );
Node* GetRightChild( BiTree* pTree, Node* pNode );
Node* GetLeftSibling( BiTree* pTree, Node* pNode );
Node* GetRightSibling( BiTree* pTree, Node* pNode );
void DeleteEntireNode( BiTree* pTree, Node** pNode );
void DeleteNode( BiTree* pTree, Node* pNode );
Node* AppendLeftChild( BiTree* pTree, Node* pNode, Node* pValue );
Node* AppendRightChild( BiTree* pTree, Node* pNode, Node* pValue );
void PrintNode( Node* pNode );
void PreTraverseTree( Node* pNode );
void MidTraverseTree( Node* pNode );
void PostTraverseTree( Node* pNode );
void Traverese( Node* pNode );
int GetTreeLeaves( Node* pNode );

#endif
//BitTree.cpp

#include "BiTree.h"


BiTree* InitTree()
{//初始化樹
	BiTree* pTree = (BiTree*)malloc( sizeof( BiTree ) );

	if( !pTree )
		return NULL;

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

	return pTree;
}

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

	if( !pNode )
		return NULL;

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

	pNode->iValue = iValue;

	return pNode;
}

void ClearTree( BiTree** pTree )
{//清空 樹
	if( !(*pTree) )
		return;

	DeleteEntireNode( *pTree, &((*pTree)->pRoot) );

	(*pTree)->iSize = 0;

	(*pTree)->pRoot = NULL;

}

void DestroyTree( BiTree** pTree )
{//銷毀樹
	ClearTree( pTree );

	free( *pTree );
	*pTree = NULL;

}

int TreeEmpty( BiTree* pTree )
{//測試樹是否為空
	if( !pTree || 0 == pTree->iSize )
		return 0;

	return pTree->iSize;
}

static int iMaxDepth = 0;
int GetTreeDepth( Node* pNode )
{//後序遍歷樹
	if( !pNode )
		return 0;

	GetTreeDepth( pNode->pLChild );
	GetTreeDepth( pNode->pRChild );
	
	Node* pCur = pNode;
	int iDepth = 0;
	while( pCur )
	{
		pCur = pCur->pParent;
		iDepth++;
	}

	if( iDepth > iMaxDepth )
		iMaxDepth = iDepth;

	return iMaxDepth;
}


Node* GetRoot( BiTree* pTree )
{//樹的根
	if( !pTree )
		return NULL;

	return pTree->pRoot;
}

void Assign( BiTree* pTree, Node* pNode, Node* pValue )
{//給結點賦值
	if( !pTree || 0 == pTree->iSize || !pNode || !pValue )
		return;

	pNode->iValue = pValue->iValue;
}

Node* GetParent( BiTree* pTree, Node* pNode )
{//取結點的父結點
	if( !pTree || 0 == pTree->iSize || !pNode )
		return NULL;

	return pNode->pParent;
}

Node* GetLeftChild( BiTree* pTree, Node* pNode )
{//取結點pNode的最左端孩子
	if( !pTree || 0 == pTree->iSize || !pNode || !pNode->pLChild )
		return NULL;

	return pNode->pLChild;
}

Node* GetRightChild( BiTree* pTree, Node* pNode )
{//取結點pNode的最右端孩子
	if( !pTree || pTree->iSize == 0 || !pNode->pRChild )
		return NULL;

	return pNode->pRChild;
}

Node* GetLeftSibling( BiTree* pTree, Node* pNode )
{//pNode的左側兄弟
	if( !pTree || 0 == pTree->iSize || !pNode )
		return NULL;

	Node* pParent = GetParent( pTree, pNode );
	if( !pParent )
		return NULL;

	return pParent->pLChild;
}

Node* GetRightSibling( BiTree* pTree, Node* pNode )
{//pNode的右側兄弟
	if( !pTree || pTree->iSize == 0 || !pNode )
		return NULL;

	Node* pParent = GetParent( pTree, pNode );
	if( !pParent )
		return NULL;

	return pNode->pRChild;
}


void DeleteEntireNode( BiTree* pTree, Node** pNode )
{//LRM 後序
	if( !pTree || !(*pNode) )
		return;

	if( (*pNode) && (*pNode)->pLChild )
	{
		DeleteEntireNode( pTree, &((*pNode)->pLChild) );
		(*pNode)->pLChild = NULL;
	}

	if( (*pNode) && (*pNode)->pRChild )
	{
		DeleteEntireNode( pTree, &((*pNode)->pRChild) );
		(*pNode)->pRChild = NULL;
	}

	Node* pParent = GetParent( pTree, *pNode );

	if( pParent )
	{
		if( pParent->pLChild == (*pNode) )
		{
			pParent->pLChild = NULL;
		}
		else if( pParent->pRChild == (*pNode) )
		{
			pParent->pRChild = NULL;
		}
	}
	delete (*pNode);
	(*pNode) = NULL;


	pTree->iSize--;
}

void DeleteNode( BiTree* pTree, Node* pNode )
{//刪除pNode結點
	if( !pTree || !pTree->iSize || !pNode )
		return;


	DeleteEntireNode( pTree, &pNode );
}

Node* AppendLeftChild( BiTree* pTree, Node* pNode, Node* pValue )
{//追加孩子結點
	if( !pTree || !pNode || !pValue )
		return NULL;

	if( pNode->pLChild )
		return NULL;

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

	pTree->iSize++;

	return pValue;
}

Node* AppendRightChild( BiTree* pTree, Node* pNode, Node* pValue )
{//追加孩子結點
	if( !pTree || !pNode || !pValue )
		return NULL;

	if( pNode->pRChild )
		return NULL;

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

	pTree->iSize++;

	return pValue;
}

void PrintNode( Node* pNode )
{//輸出結點
	if( pNode )
		printf( "%d ", pNode->iValue );
}

void PreTraverseTree( Node* pNode )
{//前序遍歷樹

	if( !pNode )
		return;

	PrintNode( pNode );//根
	PreTraverseTree( pNode->pLChild );
	PreTraverseTree( pNode->pRChild );
}

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

	MidTraverseTree( pNode->pLChild );
	PrintNode( pNode );
	MidTraverseTree( pNode->pRChild );
}

void PostTraverseTree( Node* pNode )
{//後序遍歷樹
	if( !pNode )
		return;

	PostTraverseTree( pNode->pLChild );
	PostTraverseTree( pNode->pRChild );
	PrintNode( pNode );
}

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

	puts( "Pre" );
	PreTraverseTree( pNode );
	puts( "" );

	puts( "Mid" );
	MidTraverseTree( pNode );
	puts( "" );

	puts( "Post" );
	PostTraverseTree( pNode );
	puts( "" );
}

int GetTreeLeaves( Node* pNode )
{
	if( !pNode )
		return 0;

	if( !pNode->pLChild && !pNode->pRChild )
		return 1;

	return GetTreeLeaves( pNode->pLChild ) + GetTreeLeaves( pNode->pRChild );
}

int main( int argc, char* argv[] )
{
	BiTree* pTree = InitTree();	
	pTree->pRoot = CreateNode( 0 );

	Node* pLeft = AppendLeftChild( pTree, pTree->pRoot, CreateNode( 1 ));
	Node* pRight = AppendRightChild( pTree, pTree->pRoot, CreateNode( 2 ));

	AppendLeftChild( pTree, pLeft, CreateNode( 3 ));
	AppendRightChild( pTree, pLeft, CreateNode( 4 ) );

	AppendLeftChild( pTree, pRight, CreateNode( 5 ));
	pRight = AppendRightChild( pTree, pRight, CreateNode( 6 ));

	puts( "Tree leaves:" );
	printf( "%d\n", GetTreeLeaves( pTree->pRoot ) );

	puts( "Tree Depth:" );
	printf( "%d\n", GetTreeDepth( pTree->pRoot ) );
	puts( "" );

	Traverese( pTree->pRoot );

	DeleteEntireNode( pTree, &pRight );
	puts( "Tree leaves:" );
	printf( "%d\n", GetTreeLeaves( pTree->pRoot ) );

	ClearTree( &pTree );
	DestroyTree( &pTree );

	if( pTree )
		Traverese( pTree->pRoot );

	return 0;
}

 

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