二叉树基本操作
//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; }