首页 > 代码库 > 二叉树

二叉树

Bean:

 1 package com.learning.algorithm.binaryTree; 2  3 public class BinaryNode { 4      5     private String name; 6     private BinaryNode leftNode; 7     private BinaryNode rightNode; 8      9 10     public String getName() {11         return name;12     }13 14 15 16     public void setName(String name) {17         this.name = name;18     }19 20 21 22     public BinaryNode getLeftNode() {23         return leftNode;24     }25 26 27 28     public void setLeftNode(BinaryNode leftNode) {29         this.leftNode = leftNode;30     }31 32 33 34     public BinaryNode getRightNode() {35         return rightNode;36     }37 38 39 40     public void setRightNode(BinaryNode rightNode) {41         this.rightNode = rightNode;42     }43 44     45 46 }

tree and traversal

  1 package com.learning.algorithm.binaryTree;  2   3 public class BinaryTree {  4       5     public void preOrder(BinaryNode node){  6         if(node!=null){  7             System.out.print(node.getName()+" -- ");  8             preOrder(node.getLeftNode());  9             preOrder(node.getRightNode()); 10         } 11     } 12      13     public void inOrder(BinaryNode node){ 14         if(node!=null){ 15             inOrder(node.getLeftNode()); 16             System.out.print(node.getName()+" -- "); 17             inOrder(node.getRightNode()); 18         } 19     } 20      21     public void postOrder(BinaryNode node){ 22         if(node!=null){ 23             postOrder(node.getLeftNode()); 24             postOrder(node.getRightNode()); 25             System.out.print(node.getName()+" -- "); 26         } 27     } 28      29     public void preOrderNonRecursive(BinaryNode node){ 30         BinaryNode tempNode = null; 31         BinaryNode[] arrNodes = new BinaryNode[6]; 32         int count = -1; 33         if(node!=null){ 34             count++; 35             arrNodes[count]=node; 36             while(count>-1){ 37                 tempNode = arrNodes[count]; 38                 System.out.print(tempNode.getName()+" -- "); 39                 count--; 40                 if(tempNode.getRightNode()!=null){ 41                     count++; 42                     arrNodes[count] = tempNode.getRightNode();                     43                 } 44                 if(tempNode.getLeftNode()!=null){ 45                     count++; 46                     arrNodes[count] = tempNode.getLeftNode(); 47                      48                 } 49             } 50              51         } 52     } 53  54  55     /** 56      * @param args 57      */ 58     public static void main(String[] args) { 59         BinaryNode node1 = new BinaryNode(); 60         node1.setName("node1"); 61  62         BinaryNode node2 = new BinaryNode(); 63         node2.setName("node2"); 64          65         BinaryNode node3 = new BinaryNode(); 66         node3.setName("node3"); 67          68         BinaryNode node4 = new BinaryNode(); 69         node4.setName("node4"); 70          71         BinaryNode node5 = new BinaryNode(); 72         node5.setName("node5"); 73          74         BinaryNode node6 = new BinaryNode(); 75         node6.setName("node6"); 76          77         node1.setLeftNode(node2); 78         node1.setRightNode(node3); 79          80         node2.setLeftNode(node4); 81          82         node3.setRightNode(node5); 83          84         node4.setRightNode(node6); 85          86         BinaryTree tree = new BinaryTree(); 87         tree.preOrder(node1); 88         System.out.println(); 89          90         tree.inOrder(node1); 91         System.out.println(); 92          93         tree.postOrder(node1); 94         System.out.println(); 95          96         tree.preOrderNonRecursive(node1); 97  98     } 99 100 }

 

二叉树