二叉树非递归遍历C语言实现

二叉树非递归遍历实现——C语言实现

二叉树非递归遍历:前、中、后序三种遍历需要用到栈,层序遍历需要用到队列。首先用c语言实现栈和队列,然后再实现二叉树的非递归遍历

编程环境:Visual Studio 2010

static void Visit(Position P)
{
 printf("%d ", P->Data);
}

void PreOrder(Tree T)//前序遍历
{
 Stack S;
 Position P;

 S = InitStack();//创建栈
 P = T;
 while(P || !StackEmpty(S))//StackEmpty栈是否为空
 {
  if(P)
  {
   Visit(P);
   if(P->Right)
    Push(P->Right, S);
   P = P->Left;
  }
  else
   P = Pop(S);
 }
 DestoryStack(S);//销毁栈
}

void InOrder(Tree T)//中序遍历
{
 Stack S;
 Position P;

 S = InitStack();//创建栈
 P = T;
 while(P || !StackEmpty(S))
 {
  if(P)
  {
   Push(P, S);
   P = P->Left;
  }
  else
  {
   P = Pop(S);
   Visit(P);
   P = P->Right;
  }
 }
 DestoryStack(S);//销毁栈
}

void PostOrder(Tree T)//后序遍历
{
 Position preNode, currNode;
 Stack S;

 S = InitStack();
 preNode = NULL;
 Push(T, S);

 while(!StackEmpty(S))//判断栈是否为空
 {
  currNode = Peek(S);
  if(preNode == NULL || preNode->Left == currNode || preNode->Right == currNode)
  {
   if(currNode->Left)
    Push(currNode->Left, S);
   else if(currNode->Right)
    Push(currNode->Right, S);
  }
  else if(currNode->Left == preNode)
  {
   if(currNode->Right)
    Push(currNode->Right, S);
  }
  else
  {
   Visit(currNode);
   Pop(S);
  }
  preNode = currNode;
 }
 DestoryStack(S);//销毁栈
}

void LevelOrder(Tree T)//层序遍历
{
 Queue Q;
 Position P;

 if(T == NULL)
  return;
 Q = InitQueue();
 Enqueue(T, Q);
 while(!QueueEmpty(Q))//判断队列是否为空
 {
  P = Dequeue(Q);
  Visit(P);
  if(P->Left)
   Enqueue(P->Left, Q);
  if(P->Right)
   Enqueue(P->Right, Q);
 }
 DestoryQueue(Q);//销毁队列
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是二叉树归中序遍的C语言实现: ```c // 定义二叉树结构体 typedef struct TreeNode { int val; struct TreeNode* left; struct TreeNode* right; } TreeNode; // 定义栈结构体 typedef struct Stack { TreeNode** arr; int top; int maxSize; } Stack; // 创建栈 Stack* createStack(int size) { Stack* stack = (Stack*)malloc(sizeof(Stack)); stack->arr = (TreeNode**)malloc(sizeof(TreeNode*) * size); stack->top = -1; stack->maxSize = size; return stack; } // 入栈 void push(Stack* stack, TreeNode* node) { if (stack->top == stack->maxSize - 1) { return; } stack->arr[++stack->top] = node; } // 出栈 TreeNode* pop(Stack* stack) { if (stack->top == -1) { return NULL; } return stack->arr[stack->top--]; } // 判断栈是否为空 int isEmpty(Stack* stack) { return stack->top == -1; } // 中序遍 int* inorderTraversal(TreeNode* root, int* returnSize) { *returnSize = 0; int* res = (int*)malloc(sizeof(int)); Stack* stack = createStack(100); // 创建栈 TreeNode* cur = root; while (cur != NULL || !isEmpty(stack)) { while (cur != NULL) { // 遍左子树并入栈 push(stack, cur); cur = cur->left; } cur = pop(stack); // 左子树遍完毕,出栈一个节点 res = (int*)realloc(res, sizeof(int) * (*returnSize + 1)); res[(*returnSize)++] = cur->val; // 将出栈节点的值加入结果数组 cur = cur->right; // 遍右子树 } return res; } ``` 该实现利用栈来模拟归的过程,将待遍的节点入栈,遍完左子树后出栈一个节点,将其值加入结果数组,然后遍其右子树。重复以上步骤直到栈为空。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值