1. public class BinaryTree {
    2. private TreeNode root = null;
    3. public BinaryTree(){
    4. root = new TreeNode(1, "A");
    5. }
    6. public class TreeNode{
    7. private int index;
    8. private String data;
    9. private TreeNode leftChild;
    10. private TreeNode rightChild;
    11. public int getIndex() {
    12. return index;
    13. }
    14. public void setIndex(int index) {
    15. this.index = index;
    16. }
    17. public String getData() {
    18. return data;
    19. }
    20. public void setData(String data) {
    21. this.data = data;
    22. }
    23. public TreeNode(int index,String data){
    24. this.index = index;
    25. this.data = data;
    26. this.leftChild = null;
    27. this.rightChild = null;
    28. }
    29. }
    30. /**
    31. * 构建二叉树
    32. * A
    33. * B C
    34. * D E F
    35. */
    36. public void createBinaryTree(){
    37. TreeNode nodeB = new TreeNode(2, "B");
    38. TreeNode nodeC = new TreeNode(3, "C");
    39. TreeNode nodeD = new TreeNode(4, "D");
    40. TreeNode nodeE = new TreeNode(5, "E");
    41. TreeNode nodeF = new TreeNode(6, "F");
    42. root.leftChild = nodeB;
    43. root.rightChild = nodeC;
    44. nodeB.leftChild = nodeD;
    45. nodeB.rightChild = nodeE;
    46. nodeC.rightChild = nodeF;
    47. }
    48. /**
    49. * 求二叉树的高度
    50. * @author smallkong
    51. *
    52. */
    53. public int getHeight(){
    54. return getHeight(root);
    55. }
    56. private int getHeight(TreeNode node) {
    57. if(node == null){
    58. return 0;
    59. }else{
    60. int i = getHeight(node.leftChild);
    61. int j = getHeight(node.rightChild);
    62. return (i<j)?j+1:i+1;
    63. }
    64. }
    65. /**
    66. * 获取二叉树的结点数
    67. * @author smallkong
    68. *
    69. */
    70. public int getSize(){
    71. return getSize(root);
    72. }
    73. private int getSize(TreeNode node) {
    74. if(node == null){
    75. return 0;
    76. }else{
    77. return 1+getSize(node.leftChild)+getSize(node.rightChild);
    78. }
    79. }
    80. /**
    81. * 前序遍历——递归
    82. * @author smallkong
    83. *
    84. */
    85. public void preOrder(TreeNode node){
    86. if(node == null){
    87. return;
    88. }else{
    89. System.out.print(node.getData());
    90. preOrder(node.leftChild);
    91. preOrder(node.rightChild);
    92. }
    93. }
    94. /**
    95. * 前序遍历——非递归
    96. */
    97. public void nonRecOrder(TreeNode node){
    98. if(node == null){
    99. return;
    100. }
    101. Stack<TreeNode> stack = new Stack<TreeNode>();
    102. stack.push(node);
    103. while(!stack.isEmpty()){
    104. //出栈和进栈
    105. TreeNode n = stack.pop();//弹出根结点
    106. //压入子结点
    107. System.out.print(n.getData());
    108. if(n.rightChild!=null){
    109. stack.push(n.rightChild);
    110. }
    111. if(n.leftChild!=null){
    112. stack.push(n.leftChild);
    113. }
    114. }
    115. }
    116. /**
    117. * 中序遍历——递归
    118. * @author smallkong
    119. *
    120. */
    121. public void midOrder(TreeNode node){
    122. if(node == null){
    123. return;
    124. }else{
    125. midOrder(node.leftChild);
    126. System.out.print(node.getData());
    127. midOrder(node.rightChild);
    128. }
    129. }
    130. /**
    131. * 后序遍历——递归
    132. * @author smallkong
    133. *
    134. */
    135. public void postOrder(TreeNode node){
    136. if(node == null){
    137. return;
    138. }else{
    139. postOrder(node.leftChild);
    140. postOrder(node.rightChild);
    141. System.out.print(node.getData());
    142. }
    143. }
    144. public static void main(String[] args){
    145. BinaryTree binaryTree = new BinaryTree();
    146. binaryTree.createBinaryTree();
    147. int height = binaryTree.getHeight();
    148. System.out.println("树的高度treeHeihgt:"+height);
    149. int size = binaryTree.getSize();
    150. System.out.println("结点数treeSize:"+size);
    151. System.out.print("前序preOrder data:");
    152. binaryTree.preOrder(binaryTree.root);
    153. System.out.print("\n中序midOrder data:");
    154. binaryTree.midOrder(binaryTree.root);
    155. System.out.print("\n后序postOrder data:");
    156. binaryTree.postOrder(binaryTree.root);
    157. System.out.print("\n前序非递归preOrder data:");
    158. binaryTree.nonRecOrder(binaryTree.root);
    159. }
    160. }

    image.png