代码
#include<stdio.h>
#include<stdlib.h>
#include<Windows.h>
#include<assert.h>
typedef char DataType;
typedef enum { LINK, THREAD }PointFlag;
typedef struct BinTreeNode
{
struct BinTreeNode* _pLeft;
struct BinTreeNode* _pRight;
struct BinTreeNode* _pParent;
DataType _data;
PointFlag _leftThread;
PointFlag _rightThread;
}Node, *PNode;
void InitBinTree(PNode pRoot)
{
pRoot = 0;
}
PNode BuyNode(DataType data)
{
PNode pNewNode = (PNode)malloc(sizeof(Node));
assert(pNewNode);
pNewNode->_pLeft = NULL;
pNewNode->_pRight = NULL;
pNewNode->_pParent = NULL;
pNewNode->_data = data;
pNewNode->_leftThread = LINK;
pNewNode->_rightThread = LINK;
return pNewNode;
}
void _CreateBinTree(PNode* pRoot, DataType array[], int size, int* index, DataType invalid)
{
assert(index);
if (*index < size && array[*index] != invalid)
{
*pRoot = BuyNode(array[*index]);
(*index)++;
_CreateBinTree(&(*pRoot)->_pLeft, array, size, index, invalid);
if((*pRoot)->_pLeft)
(*pRoot)->_pLeft->_pParent = (*pRoot);
(*index)++;
_CreateBinTree(&(*pRoot)->_pRight, array, size, index, invalid);
if ((*pRoot)->_pRight)
(*pRoot)->_pRight->_pParent = (*pRoot);
}
}
void CreateBinTree(PNode* pRoot, DataType array[], int size, DataType invalid)
{
int index = 0;
_CreateBinTree(pRoot, array, size, &index, invalid);
}
void _PreOrderThd(PNode pRoot, PNode* prev)
{
if (pRoot)
{
if (NULL == pRoot->_pLeft)
{
pRoot->_pLeft = (*prev);
pRoot->_leftThread = THREAD;
}
if ((*prev) && NULL == (*prev)->_pRight)
{
(*prev)->_pRight = pRoot;
(*prev)->_rightThread = THREAD;
}
(*prev) = pRoot;
if (pRoot->_leftThread == LINK)
_PreOrderThd(pRoot->_pLeft, prev);
if (pRoot->_rightThread == LINK)
_PreOrderThd(pRoot->_pRight, prev);
}
}
void PreOrderThd(PNode pRoot)
{
PNode prev = NULL;
_PreOrderThd(pRoot, &prev);
}
void _InOrderThd(PNode pRoot, PNode* prev)
{
if (pRoot)
{
_InOrderThd(pRoot->_pLeft, prev);
if (NULL == pRoot->_pLeft)
{
pRoot->_pLeft = (*prev);
pRoot->_leftThread = THREAD;
}
if ((*prev) && NULL == (*prev)->_pRight)
{
(*prev)->_pRight = pRoot;
(*prev)->_rightThread = THREAD;
}
(*prev) = pRoot;
if(pRoot->_rightThread == LINK)
_InOrderThd(pRoot->_pRight, prev);
}
}
void InOrderThd(PNode pRoot)
{
PNode prev = NULL;
_InOrderThd(pRoot, &prev);
}
void _PostOrderThd(PNode pRoot, PNode* prev)
{
if (pRoot)
{
_PostOrderThd(pRoot->_pLeft, prev);
_PostOrderThd(pRoot->_pRight, prev);
if (NULL == pRoot->_pLeft)
{
pRoot->_pLeft = (*prev);
pRoot->_leftThread = THREAD;
}
if ((*prev) && NULL == (*prev)->_pRight)
{
(*prev)->_pRight = pRoot;
(*prev)->_rightThread = THREAD;
}
(*prev) = pRoot;
}
}
void PostOrderThd(PNode pRoot)
{
PNode prev = NULL;
_PostOrderThd(pRoot, &prev);
}
void PreOrderD(PNode pRoot)
{
if (pRoot)
{
printf("%c ", pRoot->_data);
PreOrderD(pRoot->_pLeft);
PreOrderD(pRoot->_pRight);
}
}
void PreOrder(PNode pRoot)
{
PNode pCur = pRoot;
while (pCur)
{
while (pCur->_leftThread == LINK)
{
printf("%c ", pCur->_data);
pCur = pCur->_pLeft;
}
printf("%c ", pCur->_data);
pCur = pCur->_pRight;
}
}
void InOrderD(PNode pRoot)
{
if (pRoot)
{
InOrderD(pRoot->_pLeft);
printf("%c ", pRoot->_data);
InOrderD(pRoot->_pRight);
}
}
void InOrder(PNode pRoot)
{
PNode pCur = pRoot;
while (pCur)
{
while (pCur->_leftThread == LINK)
{
pCur = pCur->_pLeft;
}
printf("%c ", pCur->_data);
while (pCur->_rightThread == THREAD)
{
pCur = pCur->_pRight;
printf("%c ", pCur->_data);
}
pCur = pCur->_pRight;
}
}
void PostOrderD(PNode pRoot)
{
if (pRoot)
{
PostOrderD(pRoot->_pLeft);
PostOrderD(pRoot->_pRight);
printf("%c ", pRoot->_data);
}
}
void PostOrder(PNode pRoot)
{
PNode pCur = pRoot;
PNode prev = NULL;
while (pCur)
{
while (pCur && pCur->_leftThread == LINK)
pCur = pCur->_pLeft;
while (pCur->_rightThread == THREAD)
{
printf("%c ", pCur->_data);
prev = pCur;
pCur = pCur->_pRight;
}
if (pCur == pRoot && (NULL == pCur->_pRight || prev == pCur->_pRight))
{
printf("%c ", pCur->_data);
return;
}
while (pCur && pCur->_pRight == prev)
{
printf("%c ", pCur->_data);
prev = pCur;
pCur = pCur->_pParent;
}
if (pCur && pCur->_rightThread == LINK)
{
pCur = pCur->_pRight;
}
}
}
int main()
{
char array[] = { 'A','B','D','#','#','#','C','E','#','#','F' };
PNode pRoot = NULL;
CreateBinTree(&pRoot, array, sizeof(array) / sizeof(array[0]), '#');
PreOrderD(pRoot);
printf("\n");
PreOrderThd(pRoot);
PreOrder(pRoot);
printf("\n");
system("pause");
return 0;
}