二叉树的深度优先、广度优先遍历实现

递归深度优先:

  1 package xh_algorithm.chapter03;
  2 
  3 import java.util.Arrays;
  4 import java.util.LinkedList;
  5 
  6 /**
  7  * @author zsh
  8  * @site qqzsh.top
  9  * @create 2019-08-15 10:28
 10  * @description 二叉树的遍历
 11  * 递归实现
 12  */
 13 public class Main1 {
 14 
 15     /**
 16      * 创建二叉树
 17      * @param inputList 输入序列
 18      * @return
 19      */
 20     public static TreeNode createBinaryTree(LinkedList<Integer> inputList){
 21         TreeNode node = null;
 22         if (inputList == null || inputList.isEmpty()){
 23             return null;
 24         }
 25         Integer date = inputList.removeFirst();
 26         if (date != null){
 27             node = new TreeNode(date);
 28             node.leftChild = createBinaryTree(inputList);
 29             node.rightChild = createBinaryTree(inputList);
 30         }
 31         return node;
 32     }
 33 
 34     /**
 35      * 二叉树的前序遍历
 36      * @param node 二叉树节点
 37      */
 38     static void preOrderTraveral(TreeNode node){
 39         if (node == null){
 40             return;
 41         }
 42         //根左右
 43         System.out.println(node.data);
 44         preOrderTraveral(node.leftChild);
 45         preOrderTraveral(node.rightChild);
 46     }
 47 
 48     /**
 49      * 二叉树的中序遍历
 50      * @param node 二叉树节点
 51      */
 52     static void inOrderTreveral(TreeNode node){
 53         if (node == null){
 54             return;
 55         }
 56         //左根右
 57         inOrderTreveral(node.leftChild);
 58         System.out.println(node.data);
 59         inOrderTreveral(node.rightChild);
 60     }
 61 
 62     /**
 63      * 二叉树的后序遍历
 64      * @param node 二叉树节点
 65      */
 66     static void postOrderTreveral(TreeNode node){
 67         if (node == null){
 68             return;
 69         }
 70         //左右根
 71         postOrderTreveral(node.leftChild);
 72         postOrderTreveral(node.rightChild);
 73         System.out.println(node.data);
 74     }
 75 
 76     public static void main(String[] args) {
 77         LinkedList<Integer> inputList = new LinkedList<>(Arrays.asList(new Integer[]{
 78             3,2,9,null,null,10,null,null,8,null,4
 79         }));
 80         TreeNode treeNode = createBinaryTree(inputList);
 81         System.out.println("前序遍历");
 82         preOrderTraveral(treeNode);
 83         System.out.println("中序遍历");
 84         inOrderTreveral(treeNode);
 85         System.out.println("后序遍历");
 86         postOrderTreveral(treeNode);
 87     }
 88 }
 89 
 90 /**
 91  * 二叉树节点
 92  */
 93 class TreeNode{
 94     int data;
 95     TreeNode leftChild;
 96     TreeNode rightChild;
 97 
 98     public TreeNode(int data) {
 99         this.data = data;
100     }
101 }
View Code

非递归深度优先:

  1 package xh_algorithm.chapter03;
  2 
  3 import java.util.Arrays;
  4 import java.util.LinkedList;
  5 import java.util.Stack;
  6 
  7 import static xh_algorithm.chapter03.Main1.createBinaryTree;
  8 
  9 /**
 10  * @author zsh
 11  * @site qqzsh.top
 12  * @create 2019-08-17 10:01
 13  * @description 二叉树的遍历
 14  * 非递归实现
 15  */
 16 public class Main2 {
 17 
 18     /**
 19      * 二叉树非递归前序遍历
 20      * @param root 二叉树根节点
 21      */
 22     static void preOrderTraveralWithStack(TreeNode root){
 23         Stack<TreeNode> stack = new Stack<>();
 24         TreeNode treeNode = root;
 25         while (treeNode != null || !stack.isEmpty()){
 26             //迭代访问左孩子
 27             while (treeNode != null){
 28                 System.out.println(treeNode.data);
 29                 stack.push(treeNode);
 30                 treeNode = treeNode.leftChild;
 31             }
 32             //如果节点没有左孩子,则弹出栈顶节点,访问节点右孩子
 33             if (!stack.isEmpty()){
 34                 treeNode = stack.pop();
 35                 treeNode = treeNode.rightChild;
 36             }
 37 
 38         }
 39     }
 40 
 41     /**
 42      * 二叉树非递归中序遍历
 43      * @param root 二叉树根节点
 44      */
 45     static void inOrderTraveralWithStack(TreeNode root){
 46         Stack<TreeNode> stack = new Stack<>();
 47         TreeNode treeNode = root;
 48         while (treeNode != null || !stack.isEmpty()){
 49             //迭代访问左孩子
 50             if (treeNode != null){
 51                 stack.push(treeNode);
 52                 treeNode = treeNode.leftChild;
 53             }else {
 54                 treeNode = stack.pop();
 55                 System.out.println(treeNode.data);
 56                 treeNode = treeNode.rightChild;
 57             }
 58         }
 59     }
 60 
 61     /**
 62      * 二叉树非递归后序遍历
 63      * @param root 二叉树根节点
 64      * 两个栈实现
 65      */
 66     static void postOrderTraveralWithStack(TreeNode root){
 67         Stack<TreeNode> stack = new Stack<>();
 68         Stack<TreeNode> stack2 = new Stack<>();
 69         stack.push(root);
 70         while (!stack.isEmpty()){
 71             TreeNode pop = stack.pop();
 72             stack2.push(pop);
 73             if (pop.leftChild != null){
 74                 stack.push(pop.leftChild);
 75             }
 76             if (pop.rightChild != null){
 77                 stack.push(pop.rightChild);
 78             }
 79         }
 80         while (!stack2.isEmpty()){
 81             TreeNode pop = stack2.pop();
 82             System.out.println(pop.data);
 83         }
 84     }
 85 
 86     /**
 87      * 二叉树非递归后序遍历
 88      * @param root 二叉树根节点
 89      * 一个栈实现
 90      */
 91     static void postOrderTraveralWithStack2(TreeNode root){
 92         Stack<TreeNode> stack = new Stack<>();
 93         //当前访问的结点
 94         TreeNode curNode;
 95         //上次访问的结点
 96         TreeNode lastVisitNode;
 97         curNode = root;
 98         lastVisitNode = null;
 99 
100         //把currentNode移到左子树的最下边
101         while(curNode != null){
102             stack.push(curNode);
103             curNode = curNode.leftChild;
104         }
105         while(!stack.empty()){
106             curNode = stack.pop();  //弹出栈顶元素
107             //一个根节点被访问的前提是:无右子树或右子树已被访问过
108             if(curNode.rightChild!= null && curNode.rightChild !=lastVisitNode){
109                 //根节点再次入栈
110                 stack.push(curNode);
111                 //进入右子树,且可肯定右子树一定不为空
112                 curNode = curNode.rightChild;
113                 while(curNode!= null){
114                     //再走到右子树的最左边
115                     stack.push(curNode);
116                     curNode = curNode.leftChild;
117                 }
118             }else{
119                 //访问
120                 System.out.println(curNode.data);
121                 //修改最近被访问的节点
122                 lastVisitNode = curNode;
123             }
124         }
125     }
126 
127     public static void main(String[] args) {
128         LinkedList<Integer> inputList = new LinkedList<>(Arrays.asList(new Integer[]{
129                 3,2,9,null,null,10,null,null,8,null,4
130         }));
131         TreeNode treeNode = createBinaryTree(inputList);
132         System.out.println("前序遍历");
133         preOrderTraveralWithStack(treeNode);
134         System.out.println("中序遍历");
135         inOrderTraveralWithStack(treeNode);
136         System.out.println("后序遍历");
137         postOrderTraveralWithStack(treeNode);
138         System.out.println("后序遍历2");
139         postOrderTraveralWithStack2(treeNode);
140     }
141 
142 
143 }
View Code

 广度优先:

 1 package xh_algorithm.chapter03;
 2 
 3 import java.util.Arrays;
 4 import java.util.LinkedList;
 5 import java.util.Queue;
 6 
 7 import static xh_algorithm.chapter03.Main1.createBinaryTree;
 8 
 9 /**
10  * @author zsh
11  * @site qqzsh.top
12  * @create 2019-08-17 11:04
13  * @description 二叉树的层序遍历
14  */
15 public class Main3 {
16 
17     /**
18      * 二叉树的层序遍历
19      * @param root 二叉树根节点
20      */
21     static void levelOrderTraversal(TreeNode root){
22         Queue<TreeNode> queue = new LinkedList<>();
23         queue.offer(root);
24         while (!queue.isEmpty()){
25             TreeNode node = queue.poll();
26             System.out.println(node.data);
27             if (node.leftChild != null){
28                 queue.offer(node.leftChild);
29             }
30             if (node.rightChild != null){
31                 queue.offer(node.rightChild);
32             }
33         }
34     }
35 
36     public static void main(String[] args) {
37         LinkedList<Integer> inputList = new LinkedList<>(Arrays.asList(new Integer[]{
38                 3,2,9,null,null,10,null,null,8,null,4
39         }));
40         TreeNode treeNode = createBinaryTree(inputList);
41         System.out.println("层序遍历");
42         levelOrderTraversal(treeNode);
43     }
44 }
View Code

猜你喜欢

转载自www.cnblogs.com/zsh-blogs/p/11367859.html
今日推荐