二叉树遍历

一、代码

package digui;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.Stack;

public class shunxu {
    //递归遍历的方式与非递归遍历的方式:
    //一、递归的方式:
	//1、先序遍历:
	private static void preOrder1(Node root){
		if(root == null) return;
		System.out.print(root.value);
		preOrder1(root.left);
		preOrder1(root.right);
	}
	//2、中序遍历:
	private static void midOrder1(Node root){
		if(root == null) return;
		midOrder1(root.left);
		System.out.print(root.value);
		midOrder1(root.right);
	}
	//3、后序遍历:
	private static void postOrder1(Node root){
		if(root == null) return;
		postOrder1(root.left);
		postOrder1(root.right);
		System.out.print(root.value);
	}
    
    //二、非递归的方式:
	//1、先序遍历:
	private static void preOrder2(Node root){
		if(root == null) return;
		Stack<Node> stack = new Stack<>();//用栈来存储(先进后出)
		stack.push(root);
		while(!stack.isEmpty()){
			Node node = stack.pop();
			System.out.print(node.value);
			if(node.right != null) stack.push(node.right);
			if(node.left != null) stack.push(node.left);
		}
	}
	//2、中序遍历
	private static void midOrder2(Node root){
		if(root == null) return;
		Stack<Node> stack = new Stack<>();
		Node cur = root;//中序遍历需要时刻更新根节点
		while(!stack.isEmpty() || cur != null){
			while(cur != null){
				stack.push(cur);
				cur = cur.left;
			}
			Node node = stack.pop();
			System.out.print(node.value);
			if(node.right != null) cur= node.right;//中序遍历,左根右;更新最新的根节点指针,方便输出
		}
	}
	//3、后序遍历:
	/*和先序是逆的。可以在先序的基础上,把打印的节点单独拎出来放到另外一个栈中,之后弹出。
	直接改造先序遍历代码即可。*/
	private static void postOrder2(Node root){
		if(root == null) return;
		//两个栈
		Stack<Node> stack = new Stack<>();
		Stack<Node> stack2 = new Stack<>();
		stack.push(root);
		while(!stack.isEmpty()){
			Node node = stack.pop();
			stack2.push(node);//根节点拎出来单独放到另一个栈中
			if(node.left != null) stack.push(node.left);
			if(node.right != null) stack.push(node.right);
		}
		while(!stack2.isEmpty()){
			System.out.print(stack2.pop().value);//放有根节点的栈,倒序弹出节点
		}
	}
	
    //三、层序遍历
	/*层层打印,使用队列。*/
	private static void bfs(Node root){
		if(root == null) return;
		Queue<Node> queue = new LinkedList<>();//用队列来存储(先进先出)
		queue.add(root);
		while(!queue.isEmpty()){
			Node node = queue.poll();
			System.out.print(node.value);
			if(node.left != null) queue.add(node.left);
			if(node.right != null) queue.add(node.right);
		}
	}
	//按照一层一层打印,加层数
	private static List<List<String>> bfs2(Node root){
		List<List<String>> res = new ArrayList<>();//存储节点(List里面套List)
		Queue<Node> queue = new LinkedList<>();//用队列来存储(先进先出)
		queue.add(root);
		List<String> list;
		while(!queue.isEmpty()){
			int size = queue.size();//队列中的元素个数
			list = new ArrayList<>();//动态数组存结点元素
			while(size-- > 0){
				Node node = queue.poll();
				list.add(node.value);
				if(node.left != null) queue.add(node.left);
				if(node.right != null) queue.add(node.right);
			}
			res.add(list);//每次结束完将list加进去
		}
		System.out.print(res);
		return res;
	}
    
	public static void main(String[] args) {
		Node nodeA = new Node("A");
		Node nodeB = new Node("B");
		Node nodeC = new Node("C");
		Node nodeD = new Node("D");
		Node nodeE = new Node("E");
		Node nodeF = new Node("F");
		Node nodeG = new Node("G");
		
		//构建二叉树
		nodeA.left = nodeB;
		nodeA.right = nodeC;
		nodeB.left = nodeD;
		nodeB.right = nodeE;
		nodeC.right = nodeF;
		nodeE.left = nodeG;
		
		preOrder1(nodeA);
		System.out.println();
		midOrder1(nodeA);
		System.out.println();
		postOrder1(nodeA);
		System.out.println();
		preOrder2(nodeA);
		System.out.println();
		midOrder2(nodeA);
		System.out.println();
		postOrder2(nodeA);
		System.out.println();
		
		System.out.println("层序遍历:");
		bfs(nodeA);
		System.out.println();
		
        bfs2(nodeA);
	}
	
	public static class Node{ //节点
		public String value;
		public Node left;
		public Node right;
		
		public Node(String value){
			this.value = value;
		}
	}

}

代码视频讲解

[Queue的方法区别]

(https://blog.csdn.net/yemaozi1501/article/details/119761634)

二叉树的层次遍历和图的广度优先搜索的相同点和不同点:

相同点:两者都是从一个结点b出发一次访问其相邻结点,对于树来说,就是它的左右孩子结点,而图则是连通的结点。
不同点:对图来说,一个顶点的相邻结点有多个,而二叉树只有两个。另外,广度遍历图的时候,需要加上一个Visited[MAVX]数组,来记录已访问的结点,避免重复访问同个结点。比如:(a1,a2) (a1,a3)(a2,a3)访问a1后,广度遍历就会访问a2和a3,访问a2后,又会访问a3,这样就重复了。另外图还有不连通的情况,二叉树则没有。

List实现
作为一个Collection的子类型,Collection接口的所有方法在List接口里也适用。

因为List是一个接口,为了使用它,你必须实例化一个具体的实现,你可以在下列List的实现中选择:
java.util.ArrayList
java.util.LinkedList
java.util.Vector
java.util.Stack
在java.util.concurrent 包中,同样也有List 的实现,但是在本教程中,我将不考虑并发程序。

下面是创建List实例的一些例子
List listA = new ArrayList();
List listB = new LinkedList();
List listC = new Vector();
List listD = new Stack();

List 和 ArrayList的区别(转载)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值