java 二叉树的各种遍历

先序中序后序,递归与非递归,默认构造的排序二叉树

package Tree;

import java.util.Stack;

public class BinaryTree
{
	class Node
	{
		int value;
		Node LeftChild;
		Node RightChild;
		Node(int value) 
		{
			this.value = value;
			LeftChild = null;
			RightChild = null;
		}
	}
	public Node root;//根节点
	Stack<Node>s = new Stack<>();
	BinaryTree() //这是构造方法,不能修改和定义成 public
	{
		root = null;
	}
	public void Binary(int array[])//what the fuck?
	{
		for(int i: array)
		{
			insert(i);
		}
	}
	public void insert(int value)
	{
		root = insert(root,value);
	}
	public Node insert(Node node,int value)
	{
		if(node == null)
			node = new Node(value);
		else
		{
			if(value<=node.value)
				node.LeftChild = insert(node.LeftChild,value);
			else
				node.RightChild=insert(node.RightChild,value);
		}
		return node;
	}
	public void vist(Node node)
	{
		if(node==null)
			return ;
		System.out.println(node.value);
	}
	public void PreOrderTravel(Node node)//先序遍历
	{
		if(node == null)//二叉树遍历先中后序指的是  根位置在遍历过程中的 位置
			return ;//其中二叉树遍历 都是先左后右
		vist(node);//先根
		PreOrderTravel(node.LeftChild);//然后左子树
		PreOrderTravel(node.RightChild);//最后右子树
	}
	public void PreOrderTravel()//先序遍历
	{
		PreOrderTravel(root);
	}
	public void PrintfNextOrderTravel()
	{
		NextOrderTravel(root);
	}
	public void NextOrderTravel(Node node)//后序遍历
	{																																																																																																																											
		if(node==null)
			return ;//缩进
		
		//s.push(node);
		NextOrderTravel(node.LeftChild);//先左
		NextOrderTravel(node.RightChild);//然后右
		vist(node);//最后中
		
	}
	public void PrintfMidOrderTravel()
	{
		MidOrderTravel(root);
	}
	public void MidOrderTravel(Node node)//中序遍历
	{
		if(node==null)
			return ;//缩进
		MidOrderTravel(node.LeftChild);//先左
		vist(node);//然后中
		MidOrderTravel(node.RightChild);//最后右
	}
	public void PrintfFirstOrderTravel()
	{
		FirstOrderTravel(root);
	}
	public void FirstOrderTravel(Node node)//先序遍历的 非递归
	{
		Node p = node;
		while(p!=null||!s.isEmpty())
		{
			if(p!=null)
			{
				s.push(p);
				System.out.println(p.value);//先输出 节点的值
				p = p.LeftChild;//遍历左子树
			}
			else
			{
				Node q=s.peek();//取栈顶元素
				s.pop();
				//System.out.println(q.value);
				p = q.RightChild;//遍历右子树
			}
		}
	}
	public void PrintfCentreOrderTravel()//中序遍历
	{
		CentreOrderTravel(root);
	}
	public void CentreOrderTravel(Node node)
	{
		Node p = node;
		while(p!=null||!s.isEmpty())
		{
			if(p!=null)
			{
				s.push(p);
			//	System.out.println(p.value);
				p = p.LeftChild;//遍历左子树
			}
			else
			{
				Node q=s.peek();
				s.pop();
				System.out.println(q.value);
				p = q.RightChild;//遍历右子树
			}
		}
	}
	public void PrintfLastOrderTravel()//后序遍历
	{
		LastOrderTravel(root);
	}
	public void LastOrderTravel(Node node)
	{
		Node p = node;//为根节点
		Stack<Node> stack = new Stack<>();
		while(p!=null||!s.isEmpty())
		{//自己想的算法,空间复杂度 会大一点,待验证正确性,目前来说测试数据 正确
			if(p!=null)
			{//牺牲s栈用来当辅助栈,只是用s来储存入栈的节点
				s.push(p);//如果  我们先把当前节点入栈,再把当前节点的右子树入栈,再把当前节点的左子树入栈
				stack.push(p);//那么 出栈顺序即代表了 我们想要的 后序遍历,即左->右->根
				//p = p.LeftChild;//遍历左子树
				p = p.RightChild;//遍历右子树//
			}
			else
			{
				Node q=s.peek();//取栈顶
				s.pop();
				p = q.LeftChild;//遍历左子树
				//System.out.println(q.value);
			}
		}
		while(!stack.isEmpty())
		{
			System.out.println(stack.peek().value);
			stack.pop();
		}
	}
}

=========================================================================================

测试类:

package Tree;
import java.util.Scanner;

public class BinaryTreeSearch {
	public static void main(String args[])
	{
		
		@SuppressWarnings("resource")
		Scanner cin = new Scanner(System.in);
		while(true)
		{
			BinaryTree Tree = new BinaryTree();
			int n =cin.nextInt();
			int array[] = new int [n];
		//	Random random = new Random();
			for(int i=0;i<n;i++)
				array[i] = cin.nextInt();
			//array[i]=i+1;
			for(int i=0;i<n;i++)
				System.out.print(array[i]+"*");
			System.out.println();
			Tree.Binary(array);
			System.out.println("先序遍历");
			Tree.PreOrderTravel();
			System.out.println("***********");
			System.out.println("中序遍历");
			Tree.PrintfMidOrderTravel();
			System.out.println("***********");
			System.out.println("后序遍历");
			Tree.PrintfNextOrderTravel();
			System.out.println("***********");
			System.out.println("中序遍历,非递归");
			Tree.PrintfCentreOrderTravel();
			System.out.println("***********");
			System.out.println("先序遍历,非递归");
			Tree.PrintfFirstOrderTravel();
			System.out.println("***********");
			System.out.println("后序遍历,非递归");
			Tree.PrintfLastOrderTravel();
		}
		//cin.close();
		
	}

}


猜你喜欢

转载自blog.csdn.net/z8110/article/details/52815615