java中顺序遍历的二叉树

时间:2020-02-23 14:41:15  来源:igfitidea点击:

在本教程中,我们将在Java中看到InOrder二进制树遍历。

顺序遍历:

在Inorder遍历中,每个节点在子树之间处理。
在更简单的单词中,访问左子树,节点,然后右子树。

InOrder Traversal的步骤是:

  • 遍地 left subtree在InOrder。
  • Visit节点。
  • 遍地 right subtree在InOrder。

可以有两种方法可以实现它

  • 递归
  • 迭代

递归解决方案

递归解决方案非常直的转发。
相比之下,让我们更好地理解递归。

递归的代码将是:

//Recursive Solution
	public void inOrder(TreeNode root) {
		if(root !=  null) {
			inOrder(root.left);
			//Visit the node by Printing the node data  
			System.out.printf("%d ",root.data);
			inOrder(root.right);
		}
	}

迭代解决方案

对于递归,我们使用隐式堆栈。
所以这里要将递归解决方案转换为迭代,我们将使用显式堆栈。
迭代解决方案的步骤:

  • 创建一个空堆栈 s并初始化 currentNode作为root
  • currentNodes和集合 currentNode = currentNode.left直到 currentNode一片空白
  • 如果 currentNode是null和 s那时不是空的
  • 从堆栈中弹出顶部节点 s并打印它
  • currentNode = currentNode.right
  • 转到步骤2
  • 如果堆栈为空,则当前编码也为null,然后我们完成了
//Iterative solution
	public void inOrderIter(TreeNode root) {
 
		if(root == null)
			return;
 
		Stack<TreeNode> s = new Stack<TreeNode>();
		TreeNode currentNode=root;
 
		while(!s.empty() || currentNode!=null){
 
			if(currentNode!=null)
			{
				s.push(currentNode);
				currentNode=currentNode.left;
			}
			else
			{
				TreeNode n=s.pop();
				System.out.printf("%d ",n.data);
				currentNode=n.right;
			}
		}
	}

允许为InOrder Traversal创建Java程序:

package org.arpit.theitroad.binarytree;
 
import java.util.Stack;
 
public class BinaryTreeInOrder {
 
 
	public static class TreeNode
	{
		int data;
		TreeNode left;
		TreeNode right;
		TreeNode(int data)
		{
			this.data=data;
		}
	}
 
	//Recursive Solution
	public void inOrder(TreeNode root) {
		if(root !=  null) {
			inOrder(root.left);
			//Visit the node by Printing the node data  
			System.out.printf("%d ",root.data);
			inOrder(root.right);
		}
	}
 
	//Iterative solution
	public void inOrderIter(TreeNode root) {
 
		if(root == null)
			return;
 
		Stack<TreeNode> s = new Stack<TreeNode>();
		TreeNode currentNode=root;
 
		while(!s.empty() || currentNode!=null){
 
			if(currentNode!=null)
			{
				s.push(currentNode);
				currentNode=currentNode.left;
			}
			else
			{
				TreeNode n=s.pop();
				System.out.printf("%d ",n.data);
				currentNode=n.right;
			}
		}
	}
 
	public static void main(String[] args)
	{
		BinaryTreeInOrder bi=new BinaryTreeInOrder();
		//Creating a binary tree
		TreeNode rootNode=createBinaryTree();
		System.out.println("Using Recursive solution:");
 
		bi.inOrder(rootNode);
 
		System.out.println();
		System.out.println("-------------------------");
		System.out.println("Using Iterative solution:");
 
		bi.inOrderIter(rootNode);
	}
 
	public static TreeNode createBinaryTree()
	{
 
		TreeNode rootNode =new TreeNode(40);
		TreeNode node20=new TreeNode(20);
		TreeNode node10=new TreeNode(10);
		TreeNode node30=new TreeNode(30);
		TreeNode node60=new TreeNode(60);
		TreeNode node50=new TreeNode(50);
		TreeNode node70=new TreeNode(70);
 
		rootNode.left=node20;
		rootNode.right=node60;
 
		node20.left=node10;
		node20.right=node30;
 
		node60.left=node50;
		node60.right=node70;
 
		return rootNode;
	}
}

运行上面的程序,我们将获取以下输出:

Using Recursive solution:
10 20 30 40 50 60 70
————————
Using Iterative solution:
10 20 30 40 50 60 70