一、二叉树api实现
#include <stdio.h>
#include <stdlib.h>
#include <vector>
using namespace std;
typedef struct node
{
struct node *pLChild;
struct node *pRChild;
}BiTNode, *PBiTree;
typedef void (*BITREE_FUNC)(BiTNode *pNode);
//初始化二叉树
PBiTree InitBiTree(BiTNode *pRoot)
{
PBiTree pTree = pRoot;
return pTree;
}
//生成树节点
BiTNode *MakeNode(BiTNode *pLChild, BiTNode *pRChild)
{
BiTNode *pNode = (BiTNode *)malloc(sizeof(BiTNode));
if(pNode)
{
pNode->pLChild = pLChild;
pNode->pRChild = pRChild;
}
return pNode;
}
//设置树节点的左右子树
BiTNode *SetNode(BiTNode *pNode, BiTNode *pLChild, BiTNode *pRChild)
{
if(pNode)
{
pNode->pLChild = pLChild;
pNode->pRChild = pRChild;
}
return pNode;
}
void ClearBiTree(PBiTree pTree)
{
if(pTree == NULL)
{
return;
}
BiTNode *pNode = pTree;
if(pNode->pLChild != NULL)
{
ClearBiTree(pNode->pLChild);
}
if(pNode->pRChild != NULL)
{
ClearBiTree(pNode->pRChild);
}
free(pNode);
return;
}
void DestroyBiTree(PBiTree pTree)
{
if(pTree)
{
ClearBiTree(pTree);
}
return;
}
bool IsEmpty(PBiTree pTree)
{
if(pTree == NULL)
{
return true;
}
else
{
return false;
}
}
int GetDepth(PBiTree pTree)
{
int iLDepth = 0;
int iRDepth = 0;
int iCDepth = 0;
if(pTree)
{
iLDepth = GetDepth(pTree->pLChild);
iRDepth = GetDepth(pTree->pRChild);
iCDepth = (iLDepth > iRDepth ? iLDepth : iRDepth);
return iCDepth + 1;
}
else
{
return 0;
}
}
PBiTree SetLChild(PBiTree pParent, PBiTree pLChild)
{
pParent->pLChild = pLChild;
return pLChild;
}
PBiTree SetRChild(PBiTree pParent, PBiTree pRChild)
{
pParent->pRChild = pRChild;
return pRChild;
}
int PreOrderTraverse(PBiTree pTree, BITREE_FUNC func)
{
BiTNode *pNode = pTree;
if(pNode)
{
func(pNode);
PreOrderTraverse(pNode->pLChild, func);
PreOrderTraverse(pNode->pRChild, func);
}
return 0;
}
int InOrderTraverse(PBiTree pTree, BITREE_FUNC func)
{
BiTNode *pNode = pTree;
if(pNode)
{
InOrderTraverse(pNode->pLChild, func);
func(pNode);
InOrderTraverse(pNode->pRChild, func);
}
return 0;
}
int PostOrderTraverse(PBiTree pTree, BITREE_FUNC func)
{
BiTNode *pNode = pTree;
if(pNode)
{
PostOrderTraverse(pNode->pLChild, func);
PostOrderTraverse(pNode->pRChild, func);
func(pNode);
}
return 0;
}
二、扩展实现和测试程序
#define OFFSET_OF(TYPE, MEM_NAME) ((unsigned long)&((TYPE *)0)->MEM_NAME)
#define MEM_TO_TYPE(pMem,TYPE, MEM_NAME) ((TYPE *)((unsigned long)(pMem)-OFFSET_OF(TYPE, MEM_NAME)))
typedef struct btree_data
{
BiTNode node;
int iValue;
}BTREE_DATA;
void PrintElem(BiTNode *pNode)
{
BTREE_DATA *pData = MEM_TO_TYPE(pNode, BTREE_DATA, node);
printf("iValue %d\n", pData->iValue);
return;
}
//查找iKey出现的路径
//1、把树由上到下的所有路径都存起来,如果发现
void FindPath(PBiTree pTree, int iKey, vector<int> &vec)
{
if(pTree)
{
BTREE_DATA *pData = MEM_TO_TYPE(pTree, BTREE_DATA, node);
vec.push_back(pData->iValue);
if((!pTree->pLChild)&&(!pTree->pRChild))
{
vector<int>::iterator iter = find(vec.begin(), vec.end(), iKey);
if(iter != vec.end())
{
for(int i=0; i<(int)vec.size(); i++)
{
printf(" %d ", vec[i]);
}
printf("\n");
}
return;
}
if(pTree->pLChild)
{
FindPath(pTree->pLChild, iKey, vec);
vec.pop_back();
}
if(pTree->pRChild != NULL)
{
FindPath(pTree->pRChild, iKey, vec);
vec.pop_back();
}
}
return;
}
BiTNode *CreatTreeFromArr(BiTNode *pNode, int arr[], int i, int iLength)
{
if(i >= iLength){return NULL;}
BTREE_DATA *pData = (BTREE_DATA *)malloc(sizeof(BTREE_DATA));
pData->iValue = arr[i];
pNode = &(pData->node);
pData->node.pLChild=CreatTreeFromArr(pData->node.pLChild, arr, 2*i + 1, iLength);
pData->node.pRChild=CreatTreeFromArr(pData->node.pRChild, arr, 2*i + 2, iLength);
return pNode;
}
int _tmain(int argc, _TCHAR* argv[])
{
BTREE_DATA elem0;
elem0.iValue = 0;
elem0.node.pLChild = NULL;
elem0.node.pRChild = NULL;
BTREE_DATA elem1;
elem1.iValue = 1;
elem1.node.pLChild = NULL;
elem1.node.pRChild = NULL;
BTREE_DATA elem2;
elem2.iValue = 2;
elem2.node.pLChild = NULL;
elem2.node.pRChild = NULL;
BTREE_DATA elem3;
elem3.iValue = 3;
elem3.node.pLChild = NULL;
elem3.node.pRChild = NULL;
BTREE_DATA elem4;
elem4.iValue = 4;
elem4.node.pLChild = NULL;
elem4.node.pRChild = NULL;
BTREE_DATA elem5;
elem5.iValue = 5;
elem5.node.pLChild = NULL;
elem5.node.pRChild = NULL;
BTREE_DATA elem6;
elem6.iValue = 6;
elem6.node.pLChild = NULL;
elem6.node.pRChild = NULL;
BTREE_DATA elem7;
elem7.iValue = 7;
elem7.node.pLChild = NULL;
elem7.node.pRChild = NULL;
BiTNode *pNode0 = SetNode(&elem0.node, NULL, NULL);
BiTNode *pNode1 = SetNode(&elem1.node, pNode0, NULL);
BiTNode *pNode2 = SetNode(&elem2.node, NULL, NULL);
BiTNode *pNode3 = SetNode(&elem3.node,pNode1, pNode2);
BiTNode *pNode4 = SetNode(&elem4.node,NULL, NULL);
BiTNode *pNode5 = SetNode(&elem5.node,NULL, NULL);
BiTNode *pNode6 = SetNode(&elem6.node,pNode4, pNode5);
BiTNode *pNode7 = SetNode(&elem7.node,NULL, NULL);
PBiTree pTree = InitBiTree(pNode7);
SetLChild(pTree, pNode3);
SetRChild(pTree, pNode6);
printf("tree depth : %d\n", GetDepth(pTree));
printf("PreOrderTraverse>>>>>>>>>>>>>>\n");
PreOrderTraverse(pTree, PrintElem);
printf("InOrderTraverse>>>>>>>>>>>>>>\n");
InOrderTraverse(pTree, PrintElem);
printf("PostOrderTraverse>>>>>>>>>>>>>>\n");
PostOrderTraverse(pTree, PrintElem);
printf("FindPath>>>>>>>>>>>>>>\n");
vector<int> vec;
vec.clear();
FindPath(pTree,7, vec);
BiTNode *pTree1 = NULL;
int arr[] = {1,2,3,4,5,6,7,8,9};
pTree1 = CreatTreeFromArr(pTree1, arr, 0, sizeof(arr)/sizeof(arr[0]));
printf("desc[after CreatTreeFromArr]\n");
PreOrderTraverse(pTree1, PrintElem);
getchar();
return 0;
}