Emiyaa

一、二叉树的各种遍历方式

1.二叉树的建立

 1 public class BinaryTreeNode {
 2      private int data;
 3         private BinaryTreeNode left;
 4         private BinaryTreeNode right;
 5         
 6         public BinaryTreeNode() {}
 7 
 8         public BinaryTreeNode(int data, BinaryTreeNode left, BinaryTreeNode right) {
 9             super();
10             this.data = data;
11             this.left = left;
12             this.right = right;
13         }
14 
15         public int getData() {
16             return data;
17         }
18 
19         public void setData(int data) {
20             this.data = data;
21         }
22 
23         public BinaryTreeNode getLeft() {
24             return left;
25         }
26 
27         public void setLeft(BinaryTreeNode left) {
28             this.left = left;
29         }
30 
31         public BinaryTreeNode getRight() {
32             return right;
33         }
34 
35         public void setRight(BinaryTreeNode right) {
36             this.right = right;
37         }
38 }
二叉树的建立

 

2.前序遍历

①递归实现

1 public void preOrder(BinaryTreeNode root){
2         if(null!=root){
3             System.out.print(root.getData()+"\t");
4             preOrder(root.getLeft());
5             preOrder(root.getRight());
6         }

 ②非递归实现

 1 public void preOrderNonRecursive(BinaryTreeNode root){
 2         Stack<BinaryTreeNode> stack=new Stack<BinaryTreeNode>();
 3         while(true){
 4             while(root!=null){
 5                 System.out.print(root.getData()+"\t");
 6                 stack.push(root);
 7                 root=root.getLeft();
 8             }
 9             if(stack.isEmpty()) break;
10             root=stack.pop();
11             root=root.getRight();
12         }
13     }

 

3.中序遍历

①递归实现

1 public void inOrder(BinaryTreeNode root){
2         if(null!=root){
3             inOrder(root.getLeft());
4             System.out.print(root.getData()+"\t");
5             inOrder(root.getRight());
6         }
7     }

 

②非递归实现

 1 public void inOrderNonRecursive(BinaryTreeNode root){
 2         Stack<BinaryTreeNode> stack=new Stack<BinaryTreeNode>();
 3         while(true){
 4             while(root!=null){
 5                 stack.push(root);
 6                 root=root.getLeft();
 7             }
 8             if(stack.isEmpty())break;
 9             root=stack.pop();
10             System.out.print(root.getData()+"\t");
11             root=root.getRight();
12         }
13     }

 

4.后序遍历

①递归实现

1 public void postOrder(BinaryTreeNode root){
2         if(root!=null){
3             postOrder(root.getLeft());
4             postOrder(root.getRight());
5             System.out.print(root.getData()+"\t");
6         }
7     }

②非递归实现

 1 public void postOrderNonRecursive(BinaryTreeNode root){
 2         Stack<BinaryTreeNode> stack=new Stack<BinaryTreeNode>();
 3         while(true){
 4             if(root!=null){
 5                 stack.push(root);
 6                 root=root.getLeft();
 7             }else{
 8                 if(stack.isEmpty()) return;
 9                 
10                 if(null==stack.lastElement().getRight()){
11                     root=stack.pop();
12                     System.out.print(root.getData()+"\t");
13                     while(root==stack.lastElement().getRight()){
14                         System.out.print(stack.lastElement().getData()+"\t");
15                         root=stack.pop();
16                         if(stack.isEmpty()){
17                             break;
18                         }
19                     }
20                 }
21                 
22                 if(!stack.isEmpty())
23                     root=stack.lastElement().getRight();
24                 else
25                     root=null;
26             }
27         }
28     }

 

5.层序遍历

 1 public void levelOrder(BinaryTreeNode root){
 2         BinaryTreeNode temp;
 3         Queue<BinaryTreeNode> queue=new LinkedList<BinaryTreeNode>();
 4         queue.offer(root);
 5         while(!queue.isEmpty()){
 6             temp=queue.poll();
 7             System.out.print(temp.getData()+"\t");
 8             if(null!=temp.getLeft()) 
 9                 queue.offer(temp.getLeft());
10             if(null!=temp.getRight()){
11                 queue.offer(temp.getRight());
12             }
13         }
14     }

 

测试

 1 public static void main(String[] args) {
 2         BinaryTreeNode node10=new BinaryTreeNode(10,null,null);
 3         BinaryTreeNode node8=new BinaryTreeNode(8,null,null);
 4         BinaryTreeNode node9=new BinaryTreeNode(9,null,node10);
 5         BinaryTreeNode node4=new BinaryTreeNode(4,null,null);
 6         BinaryTreeNode node5=new BinaryTreeNode(5,node8,node9);
 7         BinaryTreeNode node6=new BinaryTreeNode(6,null,null);
 8         BinaryTreeNode node7=new BinaryTreeNode(7,null,null);
 9         BinaryTreeNode node2=new BinaryTreeNode(2,node4,node5);
10         BinaryTreeNode node3=new BinaryTreeNode(3,node6,node7);
11         BinaryTreeNode node1=new BinaryTreeNode(1,node2,node3);
12         
13         BinaryTree tree=new BinaryTree();
14         //采用递归的方式进行遍历
15         System.out.println("-----前序遍历------");
16         tree.preOrder(node1);
17         System.out.println();
18         //采用非递归的方式遍历
19         tree.preOrderNonRecursive(node1);
20         System.out.println();
21 
22         
23         //采用递归的方式进行遍历
24         System.out.println("-----中序遍历------");
25         tree.inOrder(node1);
26         System.out.println();
27         //采用非递归的方式遍历
28         tree.inOrderNonRecursive(node1);
29         System.out.println();
30         
31         //采用递归的方式进行遍历
32         System.out.println("-----后序遍历------");
33         tree.postOrder(node1);
34         System.out.println();
35         //采用非递归的方式遍历
36         tree.postOrderNonRecursive(node1);
37         System.out.println();
38         
39         //采用递归的方式进行遍历
40         System.out.println("-----层序遍历------");
41         tree.levelOrder(node1);
42         System.out.println();
43     }

 

转自:https://www.cnblogs.com/qiuyong/p/6675492.html

 

二、树的深度优先遍历以及广度优先遍历

  1 package Tree;
  2 
  3 import java.util.ArrayDeque;
  4 
  5 public class DandL {
  6     static class TreeNode{
  7         int value;
  8         TreeNode left;
  9         TreeNode right;
 10  
 11         public TreeNode(int value){
 12             this.value=value;
 13         }
 14     }
 15  
 16     TreeNode root;
 17  
 18     public DandL(int[] array){
 19         root=makeBinaryTreeByArray(array,1);
 20     }
 21  
 22     /**
 23      * 采用递归的方式创建一颗二叉树
 24      * 传入的是二叉树的数组表示法
 25      * 构造后是二叉树的二叉链表表示法
 26      */
 27     public static TreeNode makeBinaryTreeByArray(int[] array,int index){
 28         if(index<array.length){
 29             int value=array[index];
 30             if(value!=0){
 31                 TreeNode t=new TreeNode(value);
 32                 array[index]=0;
 33                 t.left=makeBinaryTreeByArray(array,index*2);
 34                 t.right=makeBinaryTreeByArray(array,index*2+1);
 35                 return t;
 36             }
 37         }
 38         return null;
 39     }
 40  
 41     /**
 42      * 深度优先遍历,相当于先根遍历
 43      * 采用非递归实现
 44      * 需要辅助数据结构:栈
 45      */
 46     public void depthOrderTraversal(){
 47         if(root==null){
 48             System.out.println("empty tree");
 49             return;
 50         }       
 51         ArrayDeque<TreeNode> stack=new ArrayDeque<TreeNode>();
 52         stack.push(root);       
 53         while(stack.isEmpty()==false){
 54             TreeNode node=stack.pop();
 55             System.out.print(node.value+"    ");
 56             if(node.right!=null){
 57                 stack.push(node.right);
 58             }
 59             if(node.left!=null){
 60                 stack.push(node.left);
 61             }           
 62         }
 63         System.out.print("\n");
 64     }
 65  
 66     /**
 67      * 广度优先遍历
 68      * 采用非递归实现
 69      * 需要辅助数据结构:队列
 70      */
 71     public void levelOrderTraversal(){
 72         if(root==null){
 73             System.out.println("empty tree");
 74             return;
 75         }
 76         ArrayDeque<TreeNode> queue=new ArrayDeque<TreeNode>();
 77         queue.add(root);
 78         while(queue.isEmpty()==false){
 79             TreeNode node=queue.remove();
 80             System.out.print(node.value+"    ");
 81             if(node.left!=null){
 82                 queue.add(node.left);
 83             }
 84             if(node.right!=null){
 85                 queue.add(node.right);
 86             }
 87         }
 88         System.out.print("\n");
 89     }
 90  
 91     /** 
 92      *                  13
 93      *                 /  \
 94      *               65    5
 95      *              /  \    \
 96      *             97  25   37
 97      *            /    /\   /
 98      *           22   4 28 32
 99      */
100     public static void main(String[] args) {
101         int[] arr={0,13,65,5,97,25,0,37,22,0,4,28,0,0,32,0};
102         DandL tree=new DandL(arr);
103         tree.depthOrderTraversal();
104         tree.levelOrderTraversal();
105     }
106 }

 转自:https://blog.csdn.net/varyall/article/details/72833757

分类:

技术点:

相关文章: