头文件
typedef char BTDataType;
typedef struct BinTreeNode
{
struct BinTreeNode* _pLeft;
struct BinTreeNode* _pRight;
BTDataType _data;
}BTNode, *PBTNode;
void CreateBinTree(PBTNode* pRoot, BTDataType* array, int size, BTDataType invalid);
void _CreateBinTree(PBTNode* pRoot, BTDataType* array, int size, int* index, BTDataType invalid);
PBTNode CopyBinTree(PBTNode pRoot);
void PreOrder(PBTNode pRoot);
void PreOrderNor(PBTNode pRoot);
void InOrder(PBTNode pRoot);
void InOrderNor(PBTNode pRoot);
void PostOrder(PBTNode pRoot);
void PostOrder(PBTNode pRoot);
int Size(PBTNode pRoot);
int GetLeafCount(PBTNode pRoot);
int GetKLevel(PBTNode pRoot, int K);
int Height(PBTNode pRoot);
void LevelOrder(PBTNode pRoot);
void Mirror(PBTNode pRoot);
void Mirror_Nor(PBTNode pRoot);
int IsCompleteBinTree(PBTNode pRoot);
PBTNode Find(PBTNode pRoot, BTDataType data);
int IsNodeInBinTree(PBTNode pRoot, PBTNode pNode);
PBTNode LeftChild(PBTNode pNode);
PBTNode RightChild(PBTNode pNode);
void DestroyBinTree(PBTNode* pRoot);
PBTNode BuyBinTreeNode(BTDataType data);
函数实现
#include <assert.h>
#include <malloc.h>
#include <string.h>
#include <stdio.h>
#include "BinTree.h"
#include "Queue.h"
#include "Stack.h"
PBTNode BuyBinTreeNode(BTDataType data)
{
PBTNode pNewNode = (PBTNode)malloc(sizeof(BTNode));
assert(pNewNode);
pNewNode->_data = data;
pNewNode->_pLeft = NULL;
pNewNode->_pRight = NULL;
return pNewNode;
}
void _CreateBinTree(PBTNode* pRoot, BTDataType* array, int size, int* index, BTDataType invalid)
{
assert(index);
if (*index < size&&invalid != array[*index])
{
*pRoot = BuyBinTreeNode(array[*index]);
++(*index);
_CreateBinTree(&(*pRoot)->_pLeft, array, size, index, invalid);
++(*index);
_CreateBinTree(&(*pRoot)->_pRight, array, size, index, invalid);
}
}
void CreateBinTree(PBTNode* pRoot, BTDataType* array, int size, BTDataType invalid)
{
int index = 0;
_CreateBinTree(pRoot, array, size, &index, invalid);
}
PBTNode CopyBinTree(PBTNode pRoot)
{
PBTNode pNewRoot = NULL;
if(pRoot)
{
pNewRoot = BuyBinTreeNode(pRoot->_data);
if(pRoot->_pLeft)
pNewRoot->_pLeft = CopyBinTree(pRoot->_pLeft);
if(pRoot->_pRight)
pNewRoot->_pRight = CopyBinTree(pRoot->_pRight);
}
return pNewRoot;
}
void PreOrder(PBTNode pRoot)
{
if(pRoot)
{
printf("%c ", pRoot->_data);
PreOrder(pRoot->_pLeft);
PreOrder(pRoot->_pRight);
}
}
void InOrder(PBTNode pRoot)
{
if(pRoot)
{
InOrder(pRoot->_pLeft);
printf("%c ", pRoot->_data);
InOrder(pRoot->_pRight);
}
}
void PostOrder(PBTNode pRoot)
{
if(pRoot)
{
PostOrder(pRoot->_pLeft);
PostOrder(pRoot->_pRight);
printf("%c ", pRoot->_data);
}
}
void DestroyBinTree(PBTNode* pRoot)
{
assert(pRoot);
if(*pRoot)
{
DestroyBinTree(&(*pRoot)->_pLeft);
DestroyBinTree(&(*pRoot)->_pRight);
free(*pRoot);
*pRoot = NULL;
}
}
int Size(PBTNode pRoot)
{
int leftSize = 0;
int rightSize = 0;
if(NULL == pRoot)
return 0;
leftSize = Size(pRoot->_pLeft);
rightSize = Size(pRoot->_pRight);
return leftSize+rightSize+1;
}
int GetLeafCount(PBTNode pRoot)
{
if(NULL == pRoot)
return 0;
if(NULL == pRoot->_pLeft && NULL == pRoot->_pRight)
return 1;
return GetLeafCount(pRoot->_pLeft)+GetLeafCount(pRoot->_pRight);
}
int Height(PBTNode pRoot)
{
int leftHeight = 0;
int rightHeight = 0;
if (NULL == pRoot)
return 0;
leftHeight = Height(pRoot->_pLeft);
rightHeight = Height(pRoot->_pRight);
return leftHeight>rightHeight? leftHeight+1:rightHeight+1;
}
int GetKLevel(PBTNode pRoot, int K)
{
if(NULL == pRoot || K <= 0)
return 0;
if(1 == K)
return 1;
return GetKLevel(pRoot->_pLeft, K-1)+GetKLevel(pRoot->_pRight, K-1);
}
void LevelOrder(PBTNode pRoot)
{
Queue q;
PBTNode pCur = NULL;
if(NULL == pRoot)
return;
QueueInit(&q);
QueuePush(&q, pRoot);
while(!QueueEmpty(&q))
{
pCur = QueueFront(&q);
printf("%c ", pCur->_data);
if(pCur->_pLeft)
QueuePush(&q, pCur->_pLeft);
if(pCur->_pRight)
QueuePush(&q, pCur->_pRight);
QueuePop(&q);
}
printf("\n");
QueueDestroy(&q);
}
void Mirror(PBTNode pRoot)
{
if(pRoot)
{
PBTNode p = pRoot->_pLeft;
pRoot->_pLeft = pRoot->_pRight;
pRoot->_pRight = p;
Mirror(pRoot->_pLeft);
Mirror(pRoot->_pRight);
}
}
void Mirror_Nor(PBTNode pRoot)
{
Queue q;
if(NULL == pRoot)
return;
QueueInit(&q);
QueuePush(&q, pRoot);
while(!QueueEmpty(&q))
{
PBTNode pCur = QueueFront(&q);
PBTNode p = pCur->_pLeft;
pCur->_pLeft = pCur->_pRight;
pCur->_pRight = p;
if(pCur->_pLeft)
QueuePush(&q, pCur->_pLeft);
if(pCur->_pRight)
QueuePush(&q, pCur->_pRight);
QueuePop(&q);
}
QueueDestroy(&q);
}
int IsCompleteBinTree(PBTNode pRoot)
{
Queue q;
int flag = 0;
if(NULL == pRoot)
return 1;
QueueInit(&q);
QueuePush(&q, pRoot);
while(!QueueEmpty(&q))
{
PBTNode pCur = QueueFront(&q);
if(flag)
{
if(pCur->_pLeft || pCur->_pRight)
return 0;
}
else
{
if(pCur->_pLeft && pCur->_pRight)
{
QueuePush(&q, pCur->_pLeft);
QueuePush(&q, pCur->_pRight);
flag = 0;
}
else if(pCur->_pLeft)
{
QueuePush(&q, pCur->_pLeft);
flag = 1;
}
else if(pCur->_pRight)
return 0;
else
flag = 1;
}
QueuePop(&q);
}
return 1;
}
int IsNodeInBinTree(PBTNode pRoot, PBTNode pNode)
{
int flag = 0;
if(NULL == pRoot || NULL == pNode)
return 0;
if(pRoot == pNode)
return 1;
if(flag = IsNodeInBinTree(pRoot->_pLeft, pNode))
return flag;
return IsNodeInBinTree(pRoot->_pRight, pNode);
}
PBTNode Find(PBTNode pRoot, BTDataType data)
{
PBTNode pNode = NULL;
if(NULL == pRoot)
return NULL;
if(pRoot->_data == data)
return pRoot;
if(pNode = Find(pRoot->_pLeft, data))/
return pNode;
return Find(pRoot->_pRight, data);
}
void PreOrderNor(PBTNode pRoot)
{
#if 0
Stack s;
if(NULL == pRoot)
return;
StackInit(&s);
StackPush(&s, pRoot);
while(!StackEmpty(&s))
{
PBTNode pCur = StackTop(&s);
printf("%c ", pCur->_data);
StackPop(&s);
if(pCur->_pRight)
StackPush(&s, pCur->_pRight);
if(pCur->_pLeft)
StackPush(&s, pCur->_pLeft);
}
printf("\n");
#endif
Stack s;
if(NULL == pRoot)
return;
StackInit(&s);
StackPush(&s, pRoot);
while(!StackEmpty(&s))
{
PBTNode pCur = StackTop(&s);
StackPop(&s);
while(pCur)
{
printf("%c ", pCur->_data);
if(pCur->_pRight)
StackPush(&s, pCur->_pRight);
pCur = pCur->_pLeft;
}
}
printf("\n");
}
void InOrderNor(PBTNode pRoot)
{
Stack s;
PBTNode pCur = pRoot;
if(NULL == pRoot)
return;
StackInit(&s);
while(pCur || !StackEmpty(&s))
{
while(pCur)
{
StackPush(&s, pCur);
pCur = pCur->_pLeft;
}
pCur = StackTop(&s);
printf("%c ", pCur->_data);
StackPop(&s);
pCur = pCur->_pRight;
}
printf("\n");
}
void PostOrderNor(PBTNode pRoot)
{
Stack s;
PBTNode pCur = pRoot;
PBTNode pTop = NULL;
PBTNode pPrev = NULL;
if(NULL == pRoot)
return;
StackInit(&s);
while(pCur || !StackEmpty(&s))
{
while(pCur)
{
StackPush(&s, pCur);
pCur = pCur->_pLeft;
}
pTop = StackTop(&s);
if(NULL == pTop->_pRight || pTop->_pRight == pPrev)
{
printf("%c ", pTop->_data);
pPrev = pTop;
StackPop(&s);
}
else
pCur = pTop->_pRight;
}
}
PBTNode LeftChild(PBTNode pNode)
{
return (NULL == pNode)? NULL:pNode->_pLeft;
}
PBTNode RightChild(PBTNode pNode)
{
return (NULL == pNode)? NULL:pNode->_pRight;
}