二叉树的深度遍历以及广度遍历

1.二叉树的广度遍历(层序便利)

遍历后的结果为 A B D G F C Z E

层序遍历的实现:

申请一个队列 先将根入队列

将根弹出

A

将根的左右孩子入队

将B弹出 将B的左右孩子入栈

A  B

重复以上操作直到队列为空

实现代码:

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
   int nValue;
   struct node *pLeft;
   struct node *pRight;
}BinaryTree;

typedef struct node3
{
   BinaryTree* nValue;
   struct node3 *pNext;
}MyQueue;

typedef struct node4
{
   int nCount;
   MyQueue *pHead;
   MyQueue *pTail;
}Queue;

void q_Init(Queue **pQueue)
{
   *pQueue = (Queue*)malloc(sizeof(Queue));
   (*pQueue)->nCount = 0;
   (*pQueue)->pHead = NULL;
   (*pQueue)->pTail = NULL;
}

void q_Push(Queue *pQueue,BinaryTree* nNum)
{
   if(pQueue == NULL)return;

   MyQueue *pTemp = NULL;
   pTemp = (MyQueue*)malloc(sizeof(MyQueue));
   pTemp->nValue = nNum;
   pTemp->pNext = NULL;

   if(pQueue->pHead == NULL)
   {
      pQueue->pHead = pTemp;
   }
   else
   {
      pQueue->pTail->pNext = pTemp;
   }

   pQueue->pTail = pTemp;
   pQueue->nCount++;
}

BinaryTree* q_Pop(Queue *pQueue)
{
   if(pQueue == NULL || pQueue->nCount == 0)return NULL;


   MyQueue *pDel = NULL;
   BinaryTree* nNum;
   pDel = pQueue->pHead;
   nNum = pDel->nValue;

   pQueue->pHead = pQueue->pHead->pNext;

   free(pDel);
   pDel = NULL;
   pQueue->nCount--;

   if(pQueue->nCount == 0)
   {
      pQueue->pTail = NULL;
   }

   return nNum;
}

BinaryTree *CreateBinaryTree()
{
   BinaryTree *pRoot = NULL;

   //根
   pRoot = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->nValue = 1;
   

   //根的左
   pRoot->pLeft = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->pLeft->nValue = 2;

   //左的左
   pRoot->pLeft->pLeft = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->pLeft->pLeft->nValue = 4;
   pRoot->pLeft->pLeft->pLeft = NULL;
   pRoot->pLeft->pLeft->pRight = NULL;

   //左的右
   pRoot->pLeft->pRight = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->pLeft->pRight->nValue = 5;
   pRoot->pLeft->pRight->pLeft = NULL;
   pRoot->pLeft->pRight->pRight = NULL;

   //根的右
   pRoot->pRight = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->pRight->nValue = 3;

   //右的左
   pRoot->pRight->pLeft = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->pRight->pLeft->nValue = 6;
   pRoot->pRight->pLeft->pLeft = NULL;
   pRoot->pRight->pLeft->pRight = NULL;

   pRoot->pRight->pRight = NULL;

   return pRoot;
}

void LevelTraversal(BinaryTree *pTree)
{
   if(pTree == NULL)return;

   //申请辅助队列
   Queue *pQueue = NULL;
   q_Init(&pQueue);

   q_Push(pQueue,pTree);

   while(pQueue->nCount != 0)
   {
      //弹出 打印
      pTree = q_Pop(pQueue);
      printf("%d ",pTree->nValue);

      //非空左右入队
      if(pTree->pLeft != NULL)
      {
         q_Push(pQueue,pTree->pLeft);
      }

      if(pTree->pRight!= NULL)
      {
         q_Push(pQueue,pTree->pRight);
      }
   }


int main()
{
   BinaryTree *pTree = NULL;
   pTree = CreateBinaryTree();
   LevelTraversal(pTree);
   return 0;
}

如何实现每输出一层会输出一个换行

介绍一种方法  利用一个标记

在A后入栈一个标记

每次遇到标记就将\n输出  遇到标记后就将标记重新加入到队列的尾

2.二叉树的深度遍历

深度遍历时这个二叉树的输出为

A  B  G  F  Z   E  D  C

循环加栈的方式实现

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
   int nValue;
   struct node *pLeft;
   struct node *pRight;
}BinaryTree;

BinaryTree *CreateBinaryTree()
{
   BinaryTree *pRoot = NULL;

   //根
   pRoot = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->nValue = 1;
   

   //根的左
   pRoot->pLeft = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->pLeft->nValue = 2;

   //左的左
   pRoot->pLeft->pLeft = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->pLeft->pLeft->nValue = 4;
   pRoot->pLeft->pLeft->pLeft = NULL;
   pRoot->pLeft->pLeft->pRight = NULL;

   //左的右
   pRoot->pLeft->pRight = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->pLeft->pRight->nValue = 5;
   pRoot->pLeft->pRight->pLeft = NULL;
   pRoot->pLeft->pRight->pRight = NULL;

   //根的右
   pRoot->pRight = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->pRight->nValue = 3;

   //右的左
   pRoot->pRight->pLeft = (BinaryTree*)malloc(sizeof(BinaryTree));
   pRoot->pRight->pLeft->nValue = 6;
   pRoot->pRight->pLeft->pLeft = NULL;
   pRoot->pRight->pLeft->pRight = NULL;

   pRoot->pRight->pRight = NULL;

   return pRoot;

}
typedef struct node1
{
   BinaryTree* nValue;
   struct node1 *pNext;
}Mystack;

typedef struct node2
{
   int nCount;
   Mystack *pTop;
}Stack;

void s_Init(Stack **pStack)
{
   *pStack = (Stack*)malloc(sizeof(Stack));
   (*pStack)->nCount = 0;
   (*pStack)->pTop = NULL;
}

void s_Push(Stack *pStack,BinaryTree* nNum)
{
   //栈不存在
   if(pStack == NULL)return;

   Mystack *pTemp = NULL;
   pTemp = (Mystack*)malloc(sizeof(Mystack));
   pTemp->nValue = nNum;
   pTemp->pNext = pStack->pTop;
   pStack->pTop = pTemp;

   pStack->nCount++;
}

BinaryTree* s_Pop(Stack *pStack)
{
   if(pStack == NULL || pStack->nCount == 0)return NULL;

   Mystack *pDel = NULL;
   pDel = pStack->pTop;
   BinaryTree* nNum;
   nNum = pDel->nValue;

   pStack->pTop = pStack->pTop->pNext;
   free(pDel);
   pDel = NULL;

   pStack->nCount--;
   return nNum;
}

void UnRecPreTraversal(BinaryTree *pTree)
{
   if(pTree == NULL)return;

   Stack *pStack = NULL;
   s_Init(&pStack);

   while(1)
   {
      while(pTree)
      {
         printf("%d ",pTree->nValue);
         s_Push(pStack,pTree);
         pTree = pTree->pLeft;
      }

      pTree = s_Pop(pStack);
      
      if(pTree == NULL)break;

      pTree = pTree->pRight;
   }
}

int main()
{
   BinaryTree *pTree = NULL;
   pTree = CreateBinaryTree();
   UnRecPreTraversal(pTree);
   return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37163944/article/details/84400947