java实现二叉树的多种遍历

来源:互联网 发布:js的display 编辑:程序博客网 时间:2024/05/05 06:47

简述:

用Java实现二叉树的前序,中序,后序,层序遍历, S型层序遍历


算法简述:

前三个算法在于输出当前节点的位置,

1 前序: 在递归左右儿子之前,输出当前节点

[java] view plaincopy
  1. void PreOrderPrint(){  
  2.     System.out.print(value.toString() + " ");  
  3.     if(left != null)  
  4.         left.PreOrderPrint();  
  5.     if(right != null)  
  6.         right.PreOrderPrint();  
  7. }  

2 中序:在递归左右儿子中间,输出

[java] view plaincopy
  1. void InOrderPrint(){  
  2.     if(left != null)  
  3.         left.InOrderPrint();  
  4.     System.out.print(value.toString() + " ");  
  5.     if(right != null)  
  6.         right.InOrderPrint();  
  7. }  

后序:在递归左右儿子之后输出

[java] view plaincopy
  1. void PostOrderPrint(){  
  2.     if(left != null)  
  3.         left.PostOrderPrint();  
  4.     if(right != null)  
  5.         right.PostOrderPrint();  
  6.     System.out.print(value.toString() + " ");  
  7. }  

3 层序遍历,这里的实现方式类似于两个簸箕(queue1 和 queue2)之间互相倒,知道谁都没有后继节点位置,即两个簸箕都为空,此处是两个队列都为空

[java] view plaincopy
  1. void LevelOrderPrint(){  
  2.     if(this == null)  
  3.         throw new IllegalArgumentException("null node !");  
  4.     Queue<Node<E>> queue1 = new LinkedList<Node<E>>();  
  5.     Queue<Node<E>> queue2 = new LinkedList<Node<E>>();  
  6.     queue1.add(this);  
  7.     while(!queue1.isEmpty() || !queue2.isEmpty()){  
  8.         if(queue2.isEmpty()){  
  9.             while(!queue1.isEmpty()){  
  10.                 Node<E> currentNode = queue1.poll();  
  11.                 System.out.print(currentNode.value.toString() + " ");  
  12.                 if(currentNode.left != null){  
  13.                     queue2.add(currentNode.left);  
  14.                 }  
  15.                 if(currentNode.right != null){  
  16.                     queue2.add(currentNode.right);  
  17.                 }  
  18.             }  
  19.         }  
  20.         else{  
  21.             while(!queue2.isEmpty()){  
  22.                 Node<E> currentNode = queue2.poll();  
  23.                 System.out.print(currentNode.value.toString() + " ");  
  24.                 if(currentNode.left != null){  
  25.                     queue1.add(currentNode.left);  
  26.                 }  
  27.                 if(currentNode.right != null){  
  28.                     queue1.add(currentNode.right);  
  29.                 }  
  30.             }  
  31.         }  
  32.         System.out.println();  
  33.     }  
  34.       
  35. }  


4  S型层序遍历,就是把上面使用的queue换为stack,注意左右子节点添加顺序,就可以了

[java] view plaincopy
  1. //Print By Level S-style  
  2. public void S_LevelOrderPrint(){  
  3.     Stack<Node<E>> stack1 = new Stack<Node<E>>();  
  4.     Stack<Node<E>> stack2 = new Stack<Node<E>>();  
  5.     stack1.add(this);  
  6.     while(!stack1.isEmpty() || !stack2.isEmpty()){  
  7.         if(stack1.isEmpty()){  
  8.             while(!stack2.isEmpty()){  
  9.                 Node<E> currentNode = stack2.pop();  
  10.                 System.out.print(currentNode.value + " ");  
  11.                 if(currentNode.left != null)  
  12.                     stack1.push(currentNode.left);  
  13.                 if(currentNode.right != null)  
  14.                     stack1.push(currentNode.right);  
  15.             }  
  16.         }else{  
  17.             while(!stack1.isEmpty()){  
  18.                 Node<E> currentNode = stack1.pop();  
  19.                 System.out.print(currentNode.value + " ");  
  20.                 if(currentNode.right != null)  
  21.                     stack2.add(currentNode.right);  
  22.                 if(currentNode.left != null)  
  23.                     stack2.add(currentNode.left);  
  24.             }  
  25.         }  
  26.         System.out.println();  
  27.     }  
  28. }  

下面是主要代码,包括测试代码

代码:


[java] view plaincopy
  1. package offer;  
  2.   
  3. import java.util.LinkedList;  
  4. import java.util.Queue;  
  5. import java.util.Stack;  
  6.   
  7.   
  8. class Node<E extends Comparable<E>>{  
  9.     Node<E> left;  
  10.     Node<E> right;  
  11.     E value;  
  12.     Node(){  
  13.         left = null;  
  14.         right = null;  
  15.         value = null;  
  16.     }  
  17.     Node(E value){  
  18.         this.value = value;  
  19.         left = null;  
  20.         right = null;  
  21.     }  
  22.       
  23.     void PreOrderPrint(){  
  24.         System.out.print(value.toString() + " ");  
  25.         if(left != null)  
  26.             left.PreOrderPrint();  
  27.         if(right != null)  
  28.             right.PreOrderPrint();  
  29.     }  
  30.       
  31.     void InOrderPrint(){  
  32.         if(left != null)  
  33.             left.InOrderPrint();  
  34.         System.out.print(value.toString() + " ");  
  35.         if(right != null)  
  36.             right.InOrderPrint();  
  37.     }  
  38.       
  39.     void PostOrderPrint(){  
  40.         if(left != null)  
  41.             left.PostOrderPrint();  
  42.         if(right != null)  
  43.             right.PostOrderPrint();  
  44.         System.out.print(value.toString() + " ");  
  45.     }  
  46.       
  47.     //Print By Level  
  48.     void LevelOrderPrint(){  
  49.         if(this == null)  
  50.             throw new IllegalArgumentException("null node !");  
  51.         Queue<Node<E>> queue1 = new LinkedList<Node<E>>();  
  52.         Queue<Node<E>> queue2 = new LinkedList<Node<E>>();  
  53.         queue1.add(this);  
  54.         while(!queue1.isEmpty() || !queue2.isEmpty()){  
  55.             if(queue2.isEmpty()){  
  56.                 while(!queue1.isEmpty()){  
  57.                     Node<E> currentNode = queue1.poll();  
  58.                     System.out.print(currentNode.value.toString() + " ");  
  59.                     if(currentNode.left != null){  
  60.                         queue2.add(currentNode.left);  
  61.                     }  
  62.                     if(currentNode.right != null){  
  63.                         queue2.add(currentNode.right);  
  64.                     }  
  65.                 }  
  66.             }  
  67.             else{  
  68.                 while(!queue2.isEmpty()){  
  69.                     Node<E> currentNode = queue2.poll();  
  70.                     System.out.print(currentNode.value.toString() + " ");  
  71.                     if(currentNode.left != null){  
  72.                         queue1.add(currentNode.left);  
  73.                     }  
  74.                     if(currentNode.right != null){  
  75.                         queue1.add(currentNode.right);  
  76.                     }  
  77.                 }  
  78.             }  
  79.             System.out.println();  
  80.         }  
  81.     }  
  82.       
  83.     //Print By Level S-style  
  84.     public void S_LevelOrderPrint(){  
  85.         Stack<Node<E>> stack1 = new Stack<Node<E>>();  
  86.         Stack<Node<E>> stack2 = new Stack<Node<E>>();  
  87.         stack1.add(this);  
  88.         while(!stack1.isEmpty() || !stack2.isEmpty()){  
  89.             if(stack1.isEmpty()){  
  90.                 while(!stack2.isEmpty()){  
  91.                     Node<E> currentNode = stack2.pop();  
  92.                     System.out.print(currentNode.value + " ");  
  93.                     if(currentNode.left != null)  
  94.                         stack1.push(currentNode.left);  
  95.                     if(currentNode.right != null)  
  96.                         stack1.push(currentNode.right);  
  97.                 }  
  98.             }else{  
  99.                 while(!stack1.isEmpty()){  
  100.                     Node<E> currentNode = stack1.pop();  
  101.                     System.out.print(currentNode.value + " ");  
  102.                     if(currentNode.right != null)  
  103.                         stack2.add(currentNode.right);  
  104.                     if(currentNode.left != null)  
  105.                         stack2.add(currentNode.left);  
  106.                 }  
  107.             }  
  108.             System.out.println();  
  109.         }  
  110.     }  
  111. }  
  112.   
  113. class BinarySearchTree<E extends Comparable<E>>{  
  114.     private Node<E> root;  
  115.       
  116.     public Node<E> getRoot(){  
  117.         return root;  
  118.     }  
  119.       
  120.     BinarySearchTree(){  
  121.         root = null;  
  122.     }  
  123.       
  124.     public void InsertNode(E value){  
  125.         if(root == null){  
  126.             root = new Node<E>(value);  
  127.             return;  
  128.         }  
  129.         Node<E> currentNode = root;  
  130.         while(true){  
  131.             if(value.compareTo(currentNode.value) > 0){  
  132.                 if(currentNode.right == null){  
  133.                     currentNode.right = new Node<E>(value);  
  134.                     break;  
  135.                 }  
  136.                 currentNode = currentNode.right;  
  137.             }else{  
  138.                 if(currentNode.left == null){  
  139.                     currentNode.left = new Node<E>(value);  
  140.                     break;  
  141.                 }  
  142.                 currentNode = currentNode.left;  
  143.             }  
  144.         }  
  145.     }  
  146.       
  147. }  
  148.   
  149. public class LevelPrintOfBinaryTree<E extends Comparable<E>> {  
  150.     public static void main(String[] args) {  
  151.         BinarySearchTree<Integer> tree = new BinarySearchTree<Integer>();  
  152.         {  
  153.             tree.InsertNode(4);  
  154.             tree.InsertNode(2);  
  155.             tree.InsertNode(1);  
  156.             tree.InsertNode(3);  
  157.             tree.InsertNode(6);  
  158.             tree.InsertNode(5);  
  159.             tree.InsertNode(7);  
  160.             tree.InsertNode(8);  
  161.         }  
  162.         System.out.print("PreOrderPrint: ");  
  163.         tree.getRoot().PreOrderPrint();  
  164.         System.out.print("\nInOrderPrint: ");  
  165.         tree.getRoot().InOrderPrint();  
  166.         System.out.print("\nPostOrderPrint: ");  
  167.         tree.getRoot().PostOrderPrint();  
  168.         System.out.println("\nLevelOrderPrint: ");  
  169.         tree.getRoot().LevelOrderPrint();  
  170.         System.out.println("\nS_LevelOrderPrint: ");  
  171.         tree.getRoot().S_LevelOrderPrint();  
  172.     }  
  173. }  


输入测试的树:


输出:

0 0