递归和非递归俩种方法实现二叉树的前序、中序、后续遍历

/*
copyright@nciaebupt 转载请注明出处
题目:递归和非递归俩种方法实现二叉树的前序遍历
分析:
按照二叉树前序遍历的定义,无论是访问整棵树还是其子树,均应该遵循先访问根结点,
然后访问根结点的左子树,最后访问根结点的右子树的。
因为对于一棵树(子树)t,如果t 非空,访问完t 的根结点值后,就应该进入t 的左子树,
但此时必须将t 保存起来,以便访问完其左子树后,进入其右子树的访问。
yeah,就是这个意思。
即在t 处设置一个回溯点,并将该回溯点进栈保存。
在整个二叉树前序遍历的过程中,程序始终要做的工作分成俩个部分:
1.当前正在处理的树(子树)
2.保存在栈中等待处理的部分。
注:当栈中元素位于栈顶即将出栈时,意味着其根结点和左子树已访问完成,
出栈后,进入其右子树进行访问。
*/

#include <iostream>
#include <cstdlib>
#include <stack>

struct BSTreeNode{
  int value;
  int flag;
  BSTreeNode * lchild;
  BSTreeNode * rchild;
};

void insertBSTree(BSTreeNode ** root, int value){
  BSTreeNode * newNode = new BSTreeNode();
  newNode->value = value;
  newNode->flag = 0;
  newNode->lchild = NULL;
  newNode->rchild = NULL;

  if((*root) == NULL){
    *root = newNode;
  }
  else{
    BSTreeNode * par = NULL;
    BSTreeNode * cur = *root;
    while(cur != NULL){
      par = cur;
      if(value >= cur->value){
        cur = cur->rchild;
      }
      else{
        cur = cur->lchild;
      }
    }
    if(value >= par->value){
      par->rchild = newNode;
    }
    else{
      par->lchild = newNode;
    }
  }
  return;
}

void createBSTree(BSTreeNode ** root, int * arr, int len){
  if(arr == NULL || len < 1) return;
  for(int i = 0; i < len; ++i){
    insertBSTree(root, arr[i]);
  }
  return;
}

void preOrderRecursion(BSTreeNode *root){
  if(root != NULL){
    std::cout<<root->value<<std::endl;
    preOrderRecursion(root->lchild);
    preOrderRecursion(root->rchild);
  }
  return ;
}

void preOrderNoRecursion(BSTreeNode * root){
  if(root == NULL) return;
  std::stack<BSTreeNode *> BSTreeNodeStack;
  while(root != NULL || !BSTreeNodeStack.empty()){
    while(root != NULL){
      std::cout<<root->value<<std::endl;
      BSTreeNodeStack.push(root);
      root = root->lchild;
    }
    if(!BSTreeNodeStack.empty()){
      root = BSTreeNodeStack.top();
      root = root->rchild;
      BSTreeNodeStack.pop();
    }
  }
  return ;
}

void inOrderRecursion(BSTreeNode * root){
  if(root != NULL){
    inOrderRecursion(root->lchild);
    std::cout<<"*"<<root->value<<std::endl;
    inOrderRecursion(root->rchild);
  }
  return;
}

void inOrderNoRecursion(BSTreeNode * root){
  if(root == NULL) return;
  BSTreeNode * cur = root;
  std::stack<BSTreeNode *> BSTreeNodeStack;
  while(cur != NULL || !BSTreeNodeStack.empty()){
    while(cur != NULL){
      BSTreeNodeStack.push(cur);
      cur = cur->lchild;
    }
    if(!BSTreeNodeStack.empty()){
      cur = BSTreeNodeStack.top();
      BSTreeNodeStack.pop();
      std::cout<<"*"<<cur->value<<std::endl;
      cur = cur->rchild;
    }
  }
  return;
}

void lastOrderRecursion(BSTreeNode * root){
  if(root != NULL){
    lastOrderRecursion(root->lchild);
    lastOrderRecursion(root->rchild);
    std::cout<<"**"<<root->value<<std::endl;
  }
}

void lastOrderNoRecursion(BSTreeNode * root){
  if(root == NULL) return;
  std::stack<BSTreeNode *> BSTreeNodeStack;
  BSTreeNodeStack.push(root);
  BSTreeNode * cur = NULL;
  while(!BSTreeNodeStack.empty()){
    cur = BSTreeNodeStack.top();
    if(cur->flag == 0){
      if(cur->rchild != NULL)
        BSTreeNodeStack.push(cur->rchild);
      if(cur->lchild != NULL)
        BSTreeNodeStack.push(cur->lchild);
      cur->flag = 1;
    }
    else{
      std::cout<<"**"<<cur->value<<std::endl;
      cur->flag = 0;
      BSTreeNodeStack.pop();
    }
  }
  return;
}

int main(int argc, char ** argv){
  int arr[] = {2,4,1,6,9,5,7};
  int len = sizeof(arr)/sizeof(int);
  BSTreeNode * root = NULL;
  createBSTree(&root, arr, len);

  preOrderRecursion(root);
  preOrderNoRecursion(root);

  inOrderRecursion(root);
  inOrderNoRecursion(root);

  lastOrderRecursion(root);
  lastOrderNoRecursion(root);


  system("pause");
  return 0;
}



















  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值