//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;//Rootint 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 () {// initialization tree 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) {// clear the tree if (! (* PTree) return; DeleteEntireNode (* pTree, & (* pTree)-> pRoot); (* pTree)-> iSize = 0; (* pTree) -> pRoot = NULL;} void DestroyTree (BiTree ** pTree) {// destroy the ClearTree (pTree); free (* pTree); * pTree = NULL ;} int TreeEmpty (BiTree * pTree) {// test whether the tree is empty if (! PTree | 0 = pTree-> iSize) return 0; return pTree-> iSize;} static int iMaxDepth = 0; int GetTreeDepth (Node * pNode) {// post-order traversal tree 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) {// tree root if (! PTree) return NULL; return pTree-> pRoot;} void Assign (BiTree * pTree, Node * pNode, Node * pValue) {// Assign if (! PTree | 0 = pTree-> iSize |! PNode |! PValue) return; pNode-> iValue = pValue-> iValue;} Node * GetParent (BiTree * pTree, Node * pNode) {// obtain the Node's parent Node if (! PTree | 0 = pTree-> iSize |! PNode) return NULL; return pNode-> pParent;} Node * GetLeftChild (BiTree * pTree, Node * pNode) {// obtain the leftmost child of the Node pNode if (! PTree | 0 = pTree-> iSize |! PNode |! PNode-> pLChild) return NULL; return pNode-> pLChild;} Node * GetRightChild (BiTree * pTree, Node * pNode) {// obtain the rightmost child of the Node pNode if (! PTree | pTree-> iSize = 0 |! PNode-> pRChild) return NULL; return pNode-> pRChild;} Node * GetLeftSibling (BiTree * pTree, Node * 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) {// The right brother of 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) {// post-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) {// Delete the pNode node if (! PTree |! PTree-> iSize |! PNode) return; DeleteEntireNode (pTree, & pNode);} Node * AppendLeftChild (BiTree * pTree, Node * pNode, Node * pValue) {// append the child Node 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) {// append the child node 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) {// output Node if (pNode) printf ("% d", pNode-> iValue);} void PreTraverseTree (Node * pNode) {// pre-order traversal tree if (! PNode) return; PrintNode (pNode); // root 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) {// post-order traversal tree 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 ;}