二叉树的基本操作

完成二叉树的创建,遍历,查找,返回叶子结点等操作
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");
}

其中非递归实现二叉树的遍历需要用到栈,对二叉树的层序遍历需要用到队列。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值