1. /**
  2. * 前序遍历查找
  3. * @param no 查找no
  4. * @return 如果找到返回节点,没有找到返回null
  5. */
  6. public HeroNode preOrderSearch(int no) {
  7. // 比较当前节点是不是
  8. if (this.no == no) {
  9. return this;
  10. }
  11. // 1. 则判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
  12. // 2. 如果左递归前序查找,找到节点,则返回
  13. HeroNode resNode = null;
  14. if (this.left != null) {
  15. resNode = this.left.preOrderSearch(no);
  16. }
  17. if (resNode != null) {
  18. return resNode;
  19. }
  20. // 1. 左递归前序查找,找到节点,则返回,否则继续判断
  21. // 2. 当前的节点的右子节点是否为空,如果不空,则继续向右递归前序查找
  22. if (this.right != null) {
  23. resNode = this.right.preOrderSearch(no);
  24. }
  25. return resNode;
  26. }
  27. /**
  28. * 中序遍历查找
  29. * @param no
  30. * @return
  31. */
  32. public HeroNode infixOrderSearch(int no) {
  33. // 判断当前节点的左子节点是否为空
  34. HeroNode resNode = null;
  35. if (this.left != null) {
  36. resNode = this.left.infixOrderSearch(no);
  37. }
  38. if (resNode != null) {
  39. return resNode;
  40. }
  41. // 如果找到则返回,如果没有找到,就和当前节点比较,如果是则返回当前节点
  42. if (this.no == no) {
  43. return this;
  44. }
  45. // 否则继续记性右递归的中序查找
  46. if (this.right != null) {
  47. resNode = this.right.infixOrderSearch(no);
  48. }
  49. return resNode;
  50. }
  51. /**
  52. * 后续查找
  53. * @param no 序号
  54. * @return 查找到了返回 那啥 ,否则返回null
  55. */
  56. public HeroNode postOrderSearch(int no) {
  57. // 先判断当前节点的左子节点是否为空,如果不为空,
  58. // 则递归后需查找
  59. HeroNode resNode = null;
  60. if (this.left != null) {
  61. resNode = this.left.postOrderSearch(no);
  62. }
  63. if (resNode != null) {
  64. return resNode;
  65. }
  66. // 如果左子树没有找到,则向右子树递归进行后序变量查找
  67. if (this.right != null) {
  68. resNode = this.right.postOrderSearch(no);
  69. }
  70. if (resNode != null) {
  71. return resNode;
  72. }
  73. // 如果 左右子树 都没有找到,就比较当前节点是不是
  74. if (this.no == no) {
  75. return this;
  76. }
  77. return resNode;
  78. }

调用

  1. /**
  2. * 前序查找
  3. * @param no 查找no
  4. * @return 如果找到返回节点,没有找到返回null
  5. */
  6. public HeroNode preOrderSearch(int no) {
  7. // 比较当前节点是不是
  8. if (this.no == no) {
  9. return this;
  10. }
  11. // 1. 则判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
  12. // 2. 如果左递归前序查找,找到节点,则返回
  13. HeroNode resNode = null;
  14. if (this.left != null) {
  15. resNode = this.left.preOrderSearch(no);
  16. }
  17. if (resNode != null) {
  18. return resNode;
  19. }
  20. // 1. 左递归前序查找,找到节点,则返回,否则继续判断
  21. // 2. 当前的节点的右子节点是否为空,如果不空,则继续向右递归前序查找
  22. if (this.right != null) {
  23. resNode = this.right.preOrderSearch(no);
  24. }
  25. return resNode;
  26. }
  27. /**
  28. * 中序查找
  29. * @param no
  30. * @return
  31. */
  32. public HeroNode infixOrderSearch(int no) {
  33. // 判断当前节点的左子节点是否为空
  34. HeroNode resNode = null;
  35. if (this.left != null) {
  36. resNode = this.left.infixOrderSearch(no);
  37. }
  38. if (resNode != null) {
  39. return resNode;
  40. }
  41. // 如果找到则返回,如果没有找到,就和当前节点比较,如果是则返回当前节点
  42. if (this.no == no) {
  43. return this;
  44. }
  45. // 否则继续记性右递归的中序查找
  46. if (this.right != null) {
  47. resNode = this.right.infixOrderSearch(no);
  48. }
  49. return resNode;
  50. }
  51. /**
  52. * 后续查找
  53. * @param no 序号
  54. * @return 查找到了返回 那啥 ,否则返回null
  55. */
  56. public HeroNode postOrderSearch(int no) {
  57. // 先判断当前节点的左子节点是否为空,如果不为空,
  58. // 则递归后需查找
  59. HeroNode resNode = null;
  60. if (this.left != null) {
  61. resNode = this.left.postOrderSearch(no);
  62. }
  63. if (resNode != null) {
  64. return resNode;
  65. }
  66. // 如果左子树没有找到,则向右子树递归进行后序变量查找
  67. if (this.right != null) {
  68. resNode = this.right.postOrderSearch(no);
  69. }
  70. if (resNode != null) {
  71. return resNode;
  72. }
  73. // 如果 左右子树 都没有找到,就比较当前节点是不是
  74. if (this.no == no) {
  75. return this;
  76. }
  77. return resNode;
  78. }

主方法

  1. // 前序查找
  2. System.out.println("前序查找~~~");
  3. HeroNode resNode = binaryTree.preOrderSearch(5);
  4. if (resNode != null) {
  5. System.out.printf("找到了,信息为no=%d name=%s", resNode.getNo(), resNode.getName());
  6. } else {
  7. System.out.println("没有找到该英雄");
  8. }
  9. /*
  10. * 前序查找~~~
  11. 找到了,信息为no=5 name=关胜
  12. Process finished with exit code 0
  13. * */

哔哩哔哩动画

完整代码

  1. package com.atguigu.tree;
  2. /**
  3. * @author victor
  4. * @site https://victorfengming.gitee.io/
  5. * @project data_algorithm
  6. * @package com.atguigu.tree
  7. * @created 2021-02-24 21:40
  8. */
  9. public class BinaryTreeDemo {
  10. public static void main(String[] args) {
  11. // 先需要创建一颗二叉树
  12. BinaryTree binaryTree = new BinaryTree();
  13. // 创建需要的节点
  14. HeroNode root = new HeroNode(1, "松江");
  15. HeroNode node2 = new HeroNode(2, "吴用");
  16. HeroNode node3 = new HeroNode(3, "卢俊义");
  17. HeroNode node4 = new HeroNode(4, "林冲");
  18. HeroNode node5 = new HeroNode(5, "关胜");
  19. // 说明: 这里我们先手动创建的二叉树
  20. // ,后面我们学习递归的方式创建二叉树
  21. root.setLeft(node2);
  22. root.setRight(node3);
  23. node3.setRight(node4);
  24. node3.setLeft(node5);
  25. binaryTree.setRoot(root);
  26. // 目前就挂载好了,二叉树的关系
  27. // 测试
  28. System.out.println("前序遍历");//1,2,3,5,4
  29. binaryTree.preOrder();
  30. System.out.println("中序遍历");//2,1,5.3.4
  31. binaryTree.infixOrder();
  32. System.out.println("后序遍历");//2,5,4,3,1
  33. binaryTree.postOrder();
  34. // 前序查找
  35. System.out.println("前序查找~~~");
  36. HeroNode resNode = binaryTree.preOrderSearch(5);
  37. if (resNode != null) {
  38. System.out.printf("找到了,信息为no=%d name=%s", resNode.getNo(), resNode.getName());
  39. } else {
  40. System.out.println("没有找到该英雄");
  41. }
  42. /*
  43. * 前序查找~~~
  44. * */
  45. // 中序遍历
  46. System.out.println("中序查找~~~");
  47. HeroNode resNode2 = binaryTree.infixOrderSearch(5);
  48. if (resNode2 != null) {
  49. System.out.printf("找到了,信息为no=%d name=%s", resNode2.getNo(), resNode2.getName());
  50. } else {
  51. System.out.println("没有找到该英雄");
  52. }
  53. /*
  54. 中序查找~~~
  55. * */
  56. System.out.println("后序查找~~~");
  57. HeroNode resNode3 = binaryTree.infixOrderSearch(5);
  58. if (resNode3 != null) {
  59. System.out.printf("找到了,信息为no=%d name=%s", resNode3.getNo(), resNode3.getName());
  60. } else {
  61. System.out.println("没有找到该英雄");
  62. }
  63. /*
  64. * 前序查找~~~
  65. 前序查找次数+1
  66. 前序查找次数+1
  67. 前序查找次数+1
  68. 前序查找次数+1
  69. 前序查找次数+1
  70. 找到了,信息为no=5 name=关胜
  71. * 中序查找~~~
  72. 中序查找次数+1
  73. 中序查找次数+1
  74. 中序查找次数+1
  75. 找到了,信息为no=5 name=关胜
  76. * 后序查找~~~
  77. 中序查找次数+1
  78. 中序查找次数+1
  79. 中序查找次数+1
  80. 找到了,信息为no=5 name=关胜
  81. Process finished with exit code 0
  82. * */
  83. }
  84. }
  85. // 定义一个 BinaryTree
  86. class BinaryTree {
  87. private HeroNode root;
  88. public void setRoot(HeroNode root) {
  89. this.root = root;
  90. }
  91. // 真正的遍历操作
  92. // 前序遍历
  93. public void preOrder() {
  94. if (this.root != null) {
  95. this.root.preOrder();
  96. } else {
  97. System.out.println("当前二叉树为空,无法遍历!");
  98. }
  99. }
  100. // 中序遍历
  101. public void infixOrder() {
  102. if (this.root != null) {
  103. this.root.infixOrder();
  104. } else {
  105. System.out.println("二叉树为空,无法遍历");
  106. }
  107. }
  108. // 后续遍历
  109. public void postOrder() {
  110. if (this.root != null) {
  111. this.root.postOrder();
  112. } else {
  113. System.out.println("二叉树为空,无法遍历");
  114. }
  115. }
  116. /**
  117. * 前序查找
  118. *
  119. * @param no
  120. * @return
  121. */
  122. public HeroNode preOrderSearch(int no) {
  123. System.out.println("前序查找次数+1");
  124. if (root != null) {
  125. return root.preOrderSearch(no);
  126. } else {
  127. return null;
  128. }
  129. }
  130. /**
  131. * 中序查找
  132. *
  133. * @param no
  134. * @return
  135. */
  136. public HeroNode infixOrderSearch(int no) {
  137. if (root != null) {
  138. return root.infixOrderSearch(no);
  139. } else {
  140. return null;
  141. }
  142. }
  143. /**
  144. * 后序查找
  145. *
  146. * @param no
  147. * @return
  148. */
  149. public HeroNode postOrderSearch(int no) {
  150. if (root != null) {
  151. return root.postOrderSearch(no);
  152. } else {
  153. return null;
  154. }
  155. }
  156. }
  157. // 先创建 HeroNode 结点
  158. class HeroNode {
  159. private int no;
  160. private String name;
  161. private HeroNode left; // 默认为空
  162. private HeroNode right; // 默认为空
  163. public HeroNode(int no, String name) {
  164. this.no = no;
  165. this.name = name;
  166. }
  167. @Override
  168. public String toString() {
  169. return "HeroNode{" +
  170. "no=" + no +
  171. ", name='" + name + '\'' +
  172. '}';
  173. }
  174. public int getNo() {
  175. return no;
  176. }
  177. public void setNo(int no) {
  178. this.no = no;
  179. }
  180. public String getName() {
  181. return name;
  182. }
  183. public void setName(String name) {
  184. this.name = name;
  185. }
  186. public HeroNode getLeft() {
  187. return left;
  188. }
  189. public void setLeft(HeroNode left) {
  190. this.left = left;
  191. }
  192. public HeroNode getRight() {
  193. return right;
  194. }
  195. public void setRight(HeroNode right) {
  196. this.right = right;
  197. }
  198. // 编写前序遍历的方法
  199. public void preOrder() {
  200. //
  201. System.out.println(this);
  202. // 先输出父节点
  203. // 递归向左子树前序比遍历
  204. if (this.left != null) {
  205. // 左边递归
  206. this.left.preOrder();
  207. }
  208. // 递归向右子树前序遍历
  209. if (this.right != null) {
  210. this.right.preOrder();
  211. }
  212. }
  213. // 中序遍历
  214. public void infixOrder() {
  215. // 递归向左子树中序遍历
  216. if (this.left != null) {
  217. this.left.infixOrder();
  218. }
  219. // 输出父节点
  220. System.out.println(this);
  221. // 递归向右子树遍历
  222. if (this.right != null) {
  223. this.right.infixOrder();
  224. }
  225. }
  226. // 后续遍历
  227. public void postOrder() {
  228. if (this.left != null) {
  229. this.left.postOrder();
  230. }
  231. if (this.right != null) {
  232. this.right.postOrder();
  233. }
  234. System.out.println(this);
  235. }
  236. /**
  237. * 前序查找
  238. *
  239. * @param no 查找no
  240. * @return 如果找到返回节点, 没有找到返回null
  241. */
  242. public HeroNode preOrderSearch(int no) {
  243. System.out.println("前序查找次数+1");
  244. // 比较当前节点是不是
  245. if (this.no == no) {
  246. return this;
  247. }
  248. // 1. 则判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
  249. // 2. 如果左递归前序查找,找到节点,则返回
  250. HeroNode resNode = null;
  251. if (this.left != null) {
  252. resNode = this.left.preOrderSearch(no);
  253. }
  254. if (resNode != null) {
  255. return resNode;
  256. }
  257. // 1. 左递归前序查找,找到节点,则返回,否则继续判断
  258. // 2. 当前的节点的右子节点是否为空,如果不空,则继续向右递归前序查找
  259. if (this.right != null) {
  260. resNode = this.right.preOrderSearch(no);
  261. }
  262. return resNode;
  263. }
  264. /**
  265. * 中序查找
  266. *
  267. * @param no
  268. * @return
  269. */
  270. public HeroNode infixOrderSearch(int no) {
  271. // 判断当前节点的左子节点是否为空
  272. HeroNode resNode = null;
  273. if (this.left != null) {
  274. resNode = this.left.infixOrderSearch(no);
  275. }
  276. if (resNode != null) {
  277. return resNode;
  278. }
  279. System.out.println("中序查找次数+1");
  280. // 如果找到则返回,如果没有找到,就和当前节点比较,如果是则返回当前节点
  281. if (this.no == no) {
  282. return this;
  283. }
  284. // 否则继续记性右递归的中序查找
  285. if (this.right != null) {
  286. resNode = this.right.infixOrderSearch(no);
  287. }
  288. return resNode;
  289. }
  290. /**
  291. * 后续查找
  292. *
  293. * @param no 序号
  294. * @return 查找到了返回 那啥 ,否则返回null
  295. */
  296. public HeroNode postOrderSearch(int no) {
  297. // 先判断当前节点的左子节点是否为空,如果不为空,
  298. // 则递归后需查找
  299. HeroNode resNode = null;
  300. if (this.left != null) {
  301. resNode = this.left.postOrderSearch(no);
  302. }
  303. if (resNode != null) {
  304. return resNode;
  305. }
  306. // 如果左子树没有找到,则向右子树递归进行后序变量查找
  307. if (this.right != null) {
  308. resNode = this.right.postOrderSearch(no);
  309. }
  310. if (resNode != null) {
  311. return resNode;
  312. }
  313. System.out.println("后序查找次数+1");
  314. // 如果 左右子树 都没有找到,就比较当前节点是不是
  315. if (this.no == no) {
  316. return this;
  317. }
  318. return resNode;
  319. }
  320. }
  321. // 结点的方法

查找2号

  1. 前序遍历
  2. HeroNode{no=1, name='松江'}
  3. HeroNode{no=2, name='吴用'}
  4. HeroNode{no=3, name='卢俊义'}
  5. HeroNode{no=5, name='关胜'}
  6. HeroNode{no=4, name='林冲'}
  7. 中序遍历
  8. HeroNode{no=2, name='吴用'}
  9. HeroNode{no=1, name='松江'}
  10. HeroNode{no=5, name='关胜'}
  11. HeroNode{no=3, name='卢俊义'}
  12. HeroNode{no=4, name='林冲'}
  13. 后序遍历
  14. HeroNode{no=2, name='吴用'}
  15. HeroNode{no=5, name='关胜'}
  16. HeroNode{no=4, name='林冲'}
  17. HeroNode{no=3, name='卢俊义'}
  18. HeroNode{no=1, name='松江'}
  19. 前序查找~~~
  20. 前序查找次数+1
  21. 前序查找次数+1
  22. 前序查找次数+1
  23. 找到了,信息为no=2 name=吴用中序查找~~~
  24. 中序查找次数+1
  25. 找到了,信息为no=2 name=吴用后序查找~~~
  26. 中序查找次数+1
  27. 找到了,信息为no=2 name=吴用
  28. Process finished with exit code 0