java版 二叉树 所有递归和非递归遍历算法


  1. import java.util.LinkedList;  
  2.   
  3.   
  4. public class BinaryTree {  
  5.     //根节点  
  6.     private Node<Integer> root;  
  7.           
  8.     //二叉树中节点数量  
  9.     private int size;  
  10.       
  11.     //无参构造器  
  12.     public BinaryTree() {  
  13.         root = new Node<Integer>();  
  14.     }   
  15.           
  16.     //数组构造器  
  17.     public BinaryTree(int[] values) {  
  18.         System.out.print("新建binaryTree:");  
  19.         for (int i : values) {  
  20.             System.out.print(i);  
  21.         }  
  22.         System.out.println();  
  23.         boolean isLeft = true;  
  24.         int len = values.length;  
  25.         if (len == 0)  
  26.             return ;  
  27.         LinkedList<Node<Integer>> queue = new LinkedList<Node<Integer>>();  
  28.         root = new Node<Integer>(values[0]);  
  29.         queue.addLast(root);  
  30.         Node parent = null;  
  31.         Node current = null;  
  32.         for (int i=1; i<len; i++) {  
  33.             current = new Node<Integer>(values[i]);  
  34.             queue.addLast(current);  
  35.             if (isLeft)  
  36.                 parent = queue.getFirst();  
  37.             else  
  38.                 parent = queue.removeFirst();  
  39.             if (isLeft) {  
  40.                 parent.setLeftChild(current);  
  41.                 isLeft = false;  
  42.             }else {  
  43.                 parent.setRightChild(current);  
  44.                 isLeft = true;  
  45.             }  
  46.         }  
  47.     }   
  48.   
  49.   
  50.       
  51.     //递归中序遍历  
  52.     public void inorder() {  
  53.         System.out.print("binaryTree递归中序遍历:");  
  54.         inorderTraverseRecursion(root);  
  55.         System.out.println();  
  56.     }  
  57.       
  58.     //层次遍历  
  59.     public void layerorder() {  
  60.         System.out.print("binaryTree层次遍历:");  
  61.         LinkedList<Node<Integer>> queue = new LinkedList<Node<Integer>>();  
  62.         queue.addLast(root);  
  63.         Node<Integer> current = null;  
  64.         while(!queue.isEmpty()) {  
  65.             current = queue.removeFirst();  
  66.             if (current.getLeftChild() != null)  
  67.                 queue.addLast(current.getLeftChild());  
  68.             if (current.getRightChild() != null)  
  69.                 queue.addLast(current.getRightChild());  
  70.             System.out.print(current.getValue());  
  71.         }  
  72.         System.out.println();  
  73.     }  
  74.   
  75.       
  76.     //获得二叉树深度     
  77.     public int getDepth() {  
  78.         return getDepthRecursion(root);  
  79.     }  
  80.       
  81.     private int getDepthRecursion(Node<Integer> node){  
  82.         if (node == null)  
  83.             return 0;  
  84.         int llen = getDepthRecursion(node.getLeftChild());  
  85.         int rlen = getDepthRecursion(node.getRightChild());  
  86.         int maxlen = Math.max(llen, rlen);  
  87.         return maxlen + 1;  
  88.     }  
  89.     //递归先序遍历      
  90.     public void preorder() {  
  91.         System.out.print("binaryTree递归先序遍历:");  
  92.         preorderTraverseRecursion(root);  
  93.         System.out.println();  
  94.     }  
  95.       
  96.       
  97.   
  98.     private void inorderTraverseRecursion(Node<Integer> node) {  
  99.         // TODO Auto-generated method stub  
  100.         if (node.getLeftChild() != null)  
  101.             inorderTraverseRecursion(node.getLeftChild());  
  102.         System.out.print(node.getValue());  
  103.         if (node.getRightChild() != null)  
  104.             inorderTraverseRecursion(node.getRightChild());  
  105.     }  
  106.       
  107.   
  108.     private void preorderTraverseRecursion(Node<Integer> node){  
  109.         System.out.print(node.getValue());  
  110.         if (node.getLeftChild() != null)  
  111.             preorderTraverseRecursion (node.getLeftChild());  
  112.         if (node.getRightChild() != null)  
  113.             preorderTraverseRecursion (node.getRightChild());  
  114.     }  
  115.       
  116.     //非递归先序遍历     
  117.     public void preorderNoRecursion() {  
  118.         System.out.print("binaryTree非递归先序遍历:");  
  119.         LinkedList<Node<Integer>> stack = new LinkedList<Node<Integer>>();  
  120.         stack.push(root);  
  121.         Node<Integer> current = null;  
  122.         while (!stack.isEmpty()) {  
  123.             current = stack.pop();  
  124.             System.out.print(current.getValue());  
  125.             if (current.getRightChild() != null)  
  126.                 stack.push(current.getRightChild());  
  127.             if (current.getLeftChild() != null)  
  128.                 stack.push(current.getLeftChild());  
  129.         }  
  130.         System.out.println();  
  131.     }  
  132.       
  133.     /** 
  134.      * 非递归中序遍历 
  135.      * 栈内保存将要访问的元素 
  136.      */  
  137.     public void inorderNoRecursion() {  
  138.         System.out.print("binaryTree非递归中序遍历:");  
  139.         LinkedList<Node<Integer>> stack = new LinkedList<Node<Integer>>();  
  140.         Node<Integer> current = root;  
  141.         while (current != null || !stack.isEmpty()) {  
  142.             while(current != null) {  
  143.                 stack.push(current);  
  144.                 current = current.getLeftChild();  
  145.             }  
  146.             if (!stack.isEmpty()) {  
  147.                 current = stack.pop();  
  148.                 System.out.print(current.getValue());  
  149.                 current = current.getRightChild();  
  150.             }  
  151.         }  
  152.         System.out.println();  
  153.     }  
  154.       
  155.     /** 
  156.      * 非递归后序遍历 
  157.  
  158.      * 当上一个访问的结点是右孩子或者当前结点没有右孩子则访问当前结点 
  159.      */  
  160.     public void postorderNoRecursion() {  
  161.         System.out.print("binaryTree非递归后序遍历:");  
  162.         Node<Integer> rNode = null;  
  163.         Node<Integer> current = root;  
  164.         LinkedList<Node<Integer>> stack = new LinkedList<Node<Integer>>();  
  165.         while(current != null || !stack.isEmpty()) {  
  166.             while(current != null) {  
  167.                 stack.push(current);  
  168.                 current = current.getLeftChild();  
  169.             }  
  170.             current = stack.pop();  
  171.             while (current != null && (current.getRightChild() == null ||current.getRightChild() == rNode)) {  
  172.                 System.out.print(current.getValue());  
  173.                 rNode = current;  
  174.                 if (stack.isEmpty()){  
  175.                     System.out.println();  
  176.                     return;  
  177.                 }  
  178.                 current = stack.pop();  
  179.             }  
  180.             stack.push(current);  
  181.             current = current.getRightChild();  
  182.         }  
  183.           
  184.     }  
  185.       
  186.     public static void main(String[] args) {  
  187.         BinaryTree bt = new BinaryTree(new int[]{1,2,3,4,5,6,7,8});  
  188.         bt.inorder();  
  189.         bt.preorder();  
  190.         bt.layerorder();  
  191.         bt.preorderNoRecursion();  
  192.         bt.inorderNoRecursion();  
  193.         bt.postorderNoRecursion();  
  194.         System.out.println("深度为:" + bt.getDepth());  
  195.     }  
  196. }  
  197.   
  198. class Node<V>{  
  199.     private V  value;  
  200.     private Node<V> leftChild;  
  201.     private Node<V> rightChild;  
  202.       
  203.     public Node(){  
  204.     };  
  205.       
  206.     public Node(V value) {  
  207.         this.value = value;  
  208.         leftChild = null;  
  209.         rightChild = null;  
  210.     }  
  211.       
  212.     public void setLeftChild(Node<V> lNode) {  
  213.         this.leftChild = lNode;  
  214.     }  
  215.       
  216.     public void setRightChild(Node<V> rNode) {  
  217.         this.rightChild = rNode;  
  218.     }  
  219.   
  220.     public V getValue() {  
  221.         return value;  
  222.     }  
  223.   
  224.     public void setValue(V value) {  
  225.         this.value = value;  
  226.     }  
  227.   
  228.     public Node<V> getLeftChild() {  
  229.         return leftChild;  
  230.     }  
  231.   
  232.     public Node<V> getRightChild() {  
  233.         return rightChild;  
  234.     }  
  235.       
  236.       
  237. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值