1. package com.atguigu.tree;
    2. /**
    3. * 二叉树
    4. *
    5. * @author Dxkstart
    6. * @create 2021-10-17-14:32
    7. */
    8. public class BinaryTreeDemo {
    9. public static void main(String[] args) {
    10. //先需要创建一棵二叉树
    11. BinaryTree binaryTree = new BinaryTree();
    12. //创建需要的节点
    13. HeroNode root = new HeroNode(1, "宋江");
    14. HeroNode node2 = new HeroNode(2, "吴用");
    15. HeroNode node3 = new HeroNode(3, "卢俊义");
    16. HeroNode node4 = new HeroNode(4, "林冲");
    17. HeroNode node5 = new HeroNode(5, "关胜");
    18. //说明:
    19. //我们先手动创建二叉树,后面我们学习递归的方式创建二叉树
    20. root.setLeft(node2);
    21. root.setRight(node3);
    22. node3.setRight(node4);
    23. node3.setLeft(node5);
    24. binaryTree.setRoot(root);
    25. //测试
    26. System.out.println("前序遍历");//12354
    27. binaryTree.preOrder1();
    28. System.out.println("中序遍历");//21534
    29. binaryTree.infixOrder1();
    30. System.out.println("后序遍历");//25431
    31. binaryTree.postOrder1();
    32. //测试查找
    33. System.out.println("前序遍历查找~");//比较了4次
    34. HeroNode resNode = binaryTree.preOrderSearch(5);
    35. if (resNode != null) {
    36. System.out.printf("找到了,信息为:no=%d name=%s \n", resNode.getNo(), resNode.getName());
    37. } else {
    38. System.out.println("没有找到");
    39. }
    40. System.out.println("中序遍历查找~");//比较了3次
    41. HeroNode resNode2 = binaryTree.infixOrderSearch(5);
    42. if (resNode2 != null) {
    43. System.out.printf("找到了,信息为:no=%d name=%s \n", resNode2.getNo(), resNode2.getName());
    44. } else {
    45. System.out.println("没有找到");
    46. }
    47. System.out.println("后序遍历查找~");//比较了2次
    48. HeroNode resNode3 = binaryTree.postOrderSearch(5);
    49. if (resNode3 != null) {
    50. System.out.printf("找到了,信息为:no=%d name=%s \n", resNode3.getNo(), resNode3.getName());
    51. } else {
    52. System.out.println("没有找到");
    53. }
    54. }
    55. }
    56. //定义BinaryTree 二叉树
    57. class BinaryTree {
    58. private HeroNode root;
    59. public void setRoot(HeroNode root) {
    60. this.root = root;
    61. }
    62. //前序遍历
    63. public void preOrder1() {
    64. if (this.root != null) {
    65. this.root.preOrder();
    66. } else {
    67. System.out.println("二叉树为空,无法遍历");
    68. }
    69. }
    70. //中序遍历
    71. public void infixOrder1() {
    72. if (this.root != null) {
    73. this.root.infixOrder();
    74. } else {
    75. System.out.println("二叉树为空,无法遍历");
    76. }
    77. }
    78. //后序遍历
    79. public void postOrder1() {
    80. if (this.root != null) {
    81. this.root.postOrder();
    82. } else {
    83. System.out.println("二叉树为空,无法遍历");
    84. }
    85. }
    86. //前序遍历查找
    87. public HeroNode preOrderSearch(int no) {
    88. if (root != null) {
    89. return root.postOrderSearch(no);
    90. } else {
    91. return null;
    92. }
    93. }
    94. //中序遍历查找
    95. public HeroNode infixOrderSearch(int no) {
    96. if (root != null) {
    97. return root.infixOrderSearch(no);
    98. } else {
    99. return null;
    100. }
    101. }
    102. //后序遍历查找
    103. public HeroNode postOrderSearch(int no) {
    104. if (root != null) {
    105. return root.postOrderSearch(no);
    106. } else {
    107. return null;
    108. }
    109. }
    110. }
    111. //先创建HeroNode节点
    112. class HeroNode {
    113. private int no;
    114. private String name;
    115. private HeroNode left;//默认null
    116. private HeroNode right;//默认null
    117. public HeroNode(int no, String name) {
    118. this.no = no;
    119. this.name = name;
    120. }
    121. public int getNo() {
    122. return no;
    123. }
    124. public void setNo(int no) {
    125. this.no = no;
    126. }
    127. public String getName() {
    128. return name;
    129. }
    130. public void setName(String name) {
    131. this.name = name;
    132. }
    133. public HeroNode getLeft() {
    134. return left;
    135. }
    136. public void setLeft(HeroNode left) {
    137. this.left = left;
    138. }
    139. public HeroNode getRight() {
    140. return right;
    141. }
    142. public void setRight(HeroNode right) {
    143. this.right = right;
    144. }
    145. @Override
    146. public String toString() {
    147. return "HeroNode{" +
    148. "no=" + no +
    149. ", name='" + name + '\'' +
    150. '}';
    151. }
    152. //前序遍历方法
    153. public void preOrder() {
    154. System.out.println(this);//先输出父节点
    155. //递归向左子树遍历
    156. if (this.left != null) {
    157. this.left.preOrder();
    158. }
    159. //递归向右子树遍历
    160. if (this.right != null) {
    161. this.right.preOrder();
    162. }
    163. }
    164. //中序遍历方法
    165. public void infixOrder() {
    166. //递归向左子树中序遍历
    167. if (this.left != null) {
    168. this.left.infixOrder();
    169. }
    170. //输出父节点
    171. System.out.println(this);
    172. //递归向右子树中序遍历
    173. if (this.right != null) {
    174. this.right.infixOrder();
    175. }
    176. }
    177. //后序遍历方法
    178. public void postOrder() {
    179. if (this.left != null) {
    180. this.left.postOrder();
    181. }
    182. if (this.right != null) {
    183. this.right.postOrder();
    184. }
    185. System.out.println(this);
    186. }
    187. //前序遍历查找
    188. /**
    189. * @param no 查找no
    190. * @return 如果找到就返回该Node,如果没有找到,就返回null
    191. */
    192. public HeroNode preOrderSearch(int no) {
    193. //比较当前节点是不是
    194. if (this.no == no) {
    195. return this;
    196. }
    197. //1.则判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
    198. //2.如果左递归前序查找,找到节点,则返回
    199. HeroNode resNode = null;
    200. if (this.left != null) {
    201. resNode = this.left.preOrderSearch(no);
    202. }
    203. if (resNode != null) {//说明我们左子树找到
    204. return resNode;
    205. }
    206. //1.左递归前序查找,找到节点,则返回,否则继续判断
    207. //2.当前的节点的右子节点是否为空,如果不为空,则继续向右递归前序查找
    208. if (this.right != null) {
    209. resNode = this.right.preOrderSearch(no);
    210. }
    211. return resNode;
    212. }
    213. //中序遍历查找
    214. public HeroNode infixOrderSearch(int no) {
    215. HeroNode resNode = null;
    216. //判断当前节点的左子节点是否为空,如果不为空,则递归中序查找
    217. if (this.left != null) {
    218. resNode = this.left.infixOrderSearch(no);
    219. }
    220. if (resNode != null) {
    221. return resNode;
    222. }
    223. //如果没找到,就和当前节点比较,如果是则返回当前节点
    224. if (this.no == no) {
    225. return this;
    226. }
    227. //否则继续进行右递归的中序查找
    228. if (this.right != null) {
    229. resNode = this.right.infixOrderSearch(no);
    230. }
    231. return resNode;
    232. }
    233. //后序遍历查找
    234. public HeroNode postOrderSearch(int no) {
    235. HeroNode resNode = null;
    236. //判断当前节点的左子节点是否为空,如果不为空,则递归后序查找
    237. if (this.left != null) {
    238. resNode = this.left.infixOrderSearch(no);
    239. }
    240. if (resNode != null) {
    241. return resNode;
    242. }
    243. //如果左子树没有找到,则向柚子树递归进行后序遍历查找
    244. if (this.right != null) {
    245. resNode = this.right.infixOrderSearch(no);
    246. }
    247. if (resNode != null) {
    248. return resNode;
    249. }
    250. //如果左右字数都没有找到,就比较当前节点是不是
    251. if (this.no == no) {
    252. resNode = this;
    253. }
    254. return resNode;
    255. }
    256. }