1. package com.atguigu.tree;
    2. /**
    3. * 二叉树
    4. * @author Dxkstart
    5. * @create 2021-10-17-14:32
    6. */
    7. public class BinaryTreeDemo {
    8. public static void main(String[] args) {
    9. //先需要创建一棵二叉树
    10. BinaryTree binaryTree = new BinaryTree();
    11. //创建需要的节点
    12. HeroNode root = new HeroNode(1, "宋江");
    13. HeroNode node2 = new HeroNode(2, "吴用");
    14. HeroNode node3 = new HeroNode(3, "卢俊义");
    15. HeroNode node4 = new HeroNode(4, "林冲");
    16. HeroNode node5 = new HeroNode(5, "关胜");
    17. //说明:
    18. //我们先手动创建二叉树,后面我们学习递归的方式创建二叉树
    19. root.setLeft(node2);
    20. root.setRight(node3);
    21. node3.setRight(node4);
    22. node3.setLeft(node5);
    23. binaryTree.setRoot(root);
    24. //测试
    25. System.out.println("前序遍历");//12354
    26. binaryTree.preOrder1();
    27. System.out.println("中序遍历");//21534
    28. binaryTree.infixOrder1();
    29. System.out.println("后序遍历");//25431
    30. binaryTree.postOrder1();
    31. }
    32. }
    33. //定义BinaryTree 二叉树
    34. class BinaryTree{
    35. private HeroNode root;
    36. public void setRoot(HeroNode root) {
    37. this.root = root;
    38. }
    39. //前序遍历
    40. public void preOrder1(){
    41. if (this.root != null){
    42. this.root.preOrder();
    43. }else {
    44. System.out.println("二叉树为空,无法遍历");
    45. }
    46. }
    47. //中序遍历
    48. public void infixOrder1(){
    49. if (this.root != null){
    50. this.root.infixOrder();
    51. }else {
    52. System.out.println("二叉树为空,无法遍历");
    53. }
    54. }
    55. //后序遍历
    56. public void postOrder1(){
    57. if (this.root != null){
    58. this.root.postOrder();
    59. }else {
    60. System.out.println("二叉树为空,无法遍历");
    61. }
    62. }
    63. }
    64. //先创建HeroNode节点
    65. class HeroNode{
    66. private int no;
    67. private String name;
    68. private HeroNode left;//默认null
    69. private HeroNode right;//默认null
    70. public HeroNode(int no, String name) {
    71. this.no = no;
    72. this.name = name;
    73. }
    74. public int getNo() {
    75. return no;
    76. }
    77. public void setNo(int no) {
    78. this.no = no;
    79. }
    80. public String getName() {
    81. return name;
    82. }
    83. public void setName(String name) {
    84. this.name = name;
    85. }
    86. public HeroNode getLeft() {
    87. return left;
    88. }
    89. public void setLeft(HeroNode left) {
    90. this.left = left;
    91. }
    92. public HeroNode getRight() {
    93. return right;
    94. }
    95. public void setRight(HeroNode right) {
    96. this.right = right;
    97. }
    98. @Override
    99. public String toString() {
    100. return "HeroNode{" +
    101. "no=" + no +
    102. ", name='" + name + '\'' +
    103. '}';
    104. }
    105. //前序遍历方法
    106. public void preOrder(){
    107. System.out.println(this);//先输出父节点
    108. //递归向左子树遍历
    109. if (this.left != null){
    110. this.left.preOrder();
    111. }
    112. //递归向右子树遍历
    113. if (this.right != null){
    114. this.right.preOrder();
    115. }
    116. }
    117. //中序遍历方法
    118. public void infixOrder(){
    119. //递归向左子树中序遍历
    120. if (this.left != null){
    121. this.left.infixOrder();
    122. }
    123. //输出父节点
    124. System.out.println(this);
    125. //递归向右子树中序遍历
    126. if (this.right != null){
    127. this.right.infixOrder();
    128. }
    129. }
    130. //后序遍历方法
    131. public void postOrder(){
    132. if (this.left != null){
    133. this.left.postOrder();
    134. }
    135. if (this.right != null){
    136. this.right.postOrder();
    137. }
    138. System.out.println(this);
    139. }
    140. }