完成二叉树的创建,遍历,查找,返回叶子结点等操作
Tree.h
typedef char DataType;
typedef struct TreeNode
{
struct TreeNode *pLeft;
struct TreeNode *pRight;
DataType data;
}TreeNode;
TreeNode *CreateRoot(DataType data)
{
TreeNode *pRoot = (TreeNode *)malloc(sizeof(TreeNode));
assert(pRoot);
pRoot->data = data;
pRoot->pLeft = NULL;
pRoot->pRight = NULL;
return pRoot;
}
//创建二叉树
TreeNode *CreateTree(DataType preOrder[], int size, int *pIndex)
{
if (*pIndex >= size)
{
return NULL;
}
if (preOrder[*pIndex] == '#')
{
(*pIndex)++;
return NULL;
}
TreeNode *pRoot = CreateRoot(preOrder[*pIndex]);
(*pIndex)++;
pRoot->pLeft = CreateTree(preOrder, size, pIndex);
pRoot->pRight = CreateTree(preOrder, size, pIndex);
return pRoot;
}
//递归实现遍历
//前序
void PreOrder(TreeNode *pRoot)
{
if (pRoot == NULL)
{
return;
}
printf("%c ", pRoot->data);
PreOrder(pRoot->pLeft);
PreOrder(pRoot->pRight);
}
//中序
void InOrder(TreeNode *pRoot)
{
if (pRoot == NULL)
{
return;
}
InOrder(pRoot->pLeft);
printf("%c ", pRoot->data);
InOrder(pRoot->pRight);
}
//后序
void PostOrder(TreeNode *pRoot)
{
if (pRoot == NULL)
{
return;
}
PostOrder(pRoot->pLeft);
PostOrder(pRoot->pRight);
printf("%c ", pRoot->data);
}
//前序--非递归实现
//void PreOrderLoop(TreeNode *pRoot)
//{
// TreeNode *pCur = pRoot;
// TreeNode *pTop = NULL;
// Stack stack;
// StackInit(&stack);
// while (pCur != NULL || !StackIsEmpty(&stack))
// {
// while (pCur != NULL)
// {
// printf("%c ", pCur->data);
// StackPush(&stack, pCur);
// pCur = pCur->pLeft;
// }
//
// pTop = StackTop(&stack);
// StackPop(&stack);
//
// pCur = pTop->pRight;
// }
// printf("\n");
//}
中序--非递归
//void InOrderLoop(TreeNode *pRoot)
//{
// TreeNode *pCur = pRoot;
// TreeNode *pTop = NULL;
// Stack stack;
// StackInit(&stack);
// while (pCur != NULL || !StackIsEmpty(&stack))
// {
// while (pCur != NULL)
// {
// StackPush(&stack, pCur);
// pCur = pCur->pLeft;
// }
//
// pTop = StackTop(&stack);
// StackPop(&stack);
// printf("%c ", pTop->data);
//
// pCur = pTop->pRight;
// }
// printf("\n");
//}
后序
//void PostOrderLoop(TreeNode *pRoot)
//{
// TreeNode *pCur = pRoot;
// TreeNode *pTop = NULL;
// TreeNode *pLast = NULL;
// Stack stack;
// StackInit(&stack);
// while (pCur != NULL || !StackIsEmpty(&stack))
// {
// while (pCur != NULL)
// {
// StackPush(&stack,pCur);
// pCur = pCur->pLeft;
// }
// pTop = StackTop(&stack);
// if (pTop->pRight == NULL || pTop->pRight == pLast)
// {
// printf("%c ", pTop->data);
// StackPop(&stack);
// pLast = pTop;
// continue;
// }
// pCur = pTop->pRight;
// }
// printf("\n");
//
//}
//得到树的节点数
int GetSize(TreeNode *pRoot)
{
if (pRoot == NULL)
{
return 0;
}
return GetSize(pRoot->pLeft) + GetSize(pRoot->pRight) + 1;
}
//得到叶子结点的数目
int GetLeaveSize(TreeNode *pRoot)
{
if (pRoot == NULL)
{
return 0;
}
if (pRoot->pLeft == NULL && pRoot->pRight == NULL)
{
return 1;
}
return GetLeaveSize(pRoot->pLeft) + GetLeaveSize(pRoot->pRight);
}
//得到第K层结点的数目
int GetKLeavesSize(TreeNode *pRoot, int k)
{
if (pRoot == NULL)
{
return 0;
}
if (k == 1)
{
return 1;
}
return GetKLeavesSize(pRoot->pLeft, k - 1) + GetKLeavesSize(pRoot->pRight, k - 1);
}
//查找某一指定结点,若找到,返回地址,找不到返回NULL
TreeNode *Find(TreeNode *pRoot, DataType data)
{
if (pRoot == NULL)
{
return NULL;
}
if (pRoot == data)
{
return pRoot;
}
TreeNode *find = Find(pRoot->pLeft, data);
if (find != NULL)
{
return find;
}
return Find(pRoot->pRight, data);
}
//得到树的高度
int GetHigh(TreeNode *pRoot)
{
if (pRoot == NULL)
{
return 0;
}
int LeftHigh = GetHigh(pRoot->pLeft);
int RightHigh = GetHigh(pRoot->pRight);
return (LeftHigh > RightHigh) ? (LeftHigh + 1) : (RightHigh + 1);
}
//层序遍历
void LevelOrder(TreeNode *pRoot)
{
if (pRoot == NULL)
{
printf("\n");
return;
}
Queue queue;
TreeNode *pFront;
QueueInit(&queue);
QueuePush(&queue,pRoot);
while (!QueueIsEmpty(&queue))
{
pFront = QueueFront(&queue);
QueuePop(&queue);
printf("%c ", pFront->data);
if (pFront->pLeft!= NULL)
{
QueuePush(&queue, pFront->pLeft);
}
if (pFront->pRight != NULL)
{
QueuePush(&queue, pFront->pRight);
}
}
printf("\n");
}
其中非递归实现二叉树的遍历需要用到栈,对二叉树的层序遍历需要用到队列。