C++实现二叉树前、中、后序递归与非递归遍历

1. 前序遍历

根-----左-----右

1.1 递归前序遍历

void preOrder1(BinTree *root)     //递归前序遍历 
{
    if(root!=NULL)
    {
        cout<<root->data<<" ";
        preOrder1(root->lchild);
        preOrder1(root->rchild);
    }
}

1.2 非递归前序遍历

根据前序遍历访问的顺序,优先访问根结点,然后再分别访问左孩子和右孩子。即对于任一结点,其可看做是根结点,因此可以直接访问,访问完之后,若其左孩子不为空,按相同规则访问它的左子树;当访问其左子树时,再访问它的右子树。因此其处理过程如下:

对于任一结点P:

  1. 访问结点P,并将结点P入栈;

  2. 判断结点P的左孩子是否为空,若为空,则取栈顶结点并进行出栈操作,并将栈顶结点的右孩子置为当前的结点P,循环至1);若不为空,则将P的左孩子置为当前的结点P;

  3. 直到P为NULL并且栈为空,则遍历结束。

void preOrder2(BinTree *root)     //非递归前序遍历 
{
    stack<BinTree*> s;
    BinTree *p=root;
    while(p!=NULL||!s.empty())
    {
        while(p!=NULL)
        {
            cout<<p->data<<" ";
            s.push(p);
            p=p->lchild;
        }
        if(!s.empty())
        {
            p=s.top();
            s.pop();
            p=p->rchild;
        }
    }
}

Java版本

public void preOrder(TreeNode root) {
		Stack<TreeNode> s = new Stack<>();
		TreeNode p = root;
		while (!s.isEmpty() || p != null) {
			while (p != null) {
				System.out.println(p.val);
				s.push(p);
				p = p.left;
			}
			if (!s.isEmpty()) {
				p = s.pop();
				p = p.right;
			}
		}
	}

2. 中序遍历

左-----根-----右

2.1 递归中序遍历

void inOrder1(BinTree *root)      //递归中序遍历
{
    if(root!=NULL)
    {
        inOrder1(root->lchild);
        cout<<root->data<<" ";
        inOrder1(root->rchild);
    }
}

2.2 非递归中序遍历

根据中序遍历的顺序,对于任一结点,优先访问其左孩子,而左孩子结点又可以看做一根结点,然后继续访问其左孩子结点,直到遇到左孩子结点为空的结点才进行访问,然后按相同的规则访问其右子树。因此其处理过程如下:

对于任一结点P

  1. 若其左孩子不为空,则将P入栈并将P的左孩子置为当前的P,然后对当前结点P再进行相同的处理;

  2. 若其左孩子为空,则取栈顶元素并进行出栈操作,访问该栈顶结点,然后将当前的P置为栈顶结点的右孩子;

  3. 直到P为NULL并且栈为空则遍历结束。

void inOrder2(BinTree *root)      //非递归中序遍历
{
    stack<BinTree*> s;
    BinTree *p=root;
    while(p!=NULL||!s.empty())
    {
        while(p!=NULL)
        {
            s.push(p);
            p=p->lchild;
        }
        if(!s.empty())
        {
            p=s.top();
            cout<<p->data<<" ";
            s.pop();
            p=p->rchild;
        }
    }    
}

Java版本

public void inOrder(TreeNode root) {
		Stack<TreeNode> s = new Stack<>();
		TreeNode p = root;
		while (!s.isEmpty() || p != null) {
			while (p != null) {
				s.push(p);
				p = p.left;
			}
			if (!s.isEmpty()) {
				p = s.pop();
				System.out.println(p.val);
				p = p.right;
			}
		}
	}

3. 后序遍历

左-----右-----根

3.1 递归后序遍历

void postOrder1(BinTree *root)    //递归后序遍历
{
    if(root!=NULL)
    {
        postOrder1(root->lchild);
        postOrder1(root->rchild);
        cout<<root->data<<" ";
    }    
}

3.2 非递归后序遍历

要保证根结点在左孩子和右孩子访问之后才能访问,因此对于任一结点P,先将其入栈。
如果P不存在左孩子和右孩子,则可以直接访问它;或者P存 在左孩子或者右孩子,但是其左孩子和右孩子都已被访问过了,则同样可以直接访问该结点。

若非上述两种情况,则将P的右孩子和左孩子依次入栈,这样就保证了 每次取栈顶元素的时候,左孩子在右孩子前面被访问,左孩子和右孩子都在根结点前面被访问。

void postOrder3(BinTree *root)     //非递归后序遍历
{
    stack<BinTree*> s;
    BinTree *cur;                      //当前结点 
    BinTree *pre=NULL;                 //前一次访问的结点 
    s.push(root);
    while(!s.empty())
    {
        cur=s.top();
        if((cur->lchild==NULL&&cur->rchild==NULL)||
           (pre!=NULL&&(pre==cur->lchild||pre==cur->rchild)))
        {
            cout<<cur->data<<" ";  //如果当前结点没有孩子结点或者孩子节点都已被访问过 
              s.pop();
            pre=cur; 
        }
        else
        {
            if(cur->rchild!=NULL)
                s.push(cur->rchild);
            if(cur->lchild!=NULL)    
                s.push(cur->lchild);
        }
    }    
}

Java版本

public void postOrder(TreeNode root) {
		Stack<TreeNode> s = new Stack<>();
		TreeNode cur;
		TreeNode pre = null;
		s.push(root);
		while (!s.isEmpty()) {
			cur = s.peek();
			if (cur.left == null && cur.right == null || 
					(pre != null && (pre == cur.left || pre == cur.right))) {
				System.out.println(cur.val);
				s.pop();
				pre = cur;
			} else {
				if (cur.right != null) {
					s.push(cur.right);
				}
				if (cur.left != null) {
					s.push(cur.left);
				}
			}
		}
	}

文章来源
https://www.cnblogs.com/SHERO-Vae/p/5800363.html

猜你喜欢

转载自blog.csdn.net/fxjzzyo/article/details/81320377