zcq
    创建二叉树以及实现前中后序遍历查找删除。

    1. package com.atguigu.tree;
    2. public class BinaryTreeDemo {
    3. public static void main(String[] args) {
    4. //先需要创建一颗二叉树
    5. BinaryTree binaryTree = new BinaryTree();
    6. //创建需要的结点
    7. HeroNode root = new HeroNode(1, "宋江");
    8. HeroNode node2 = new HeroNode(2, "吴用");
    9. HeroNode node3 = new HeroNode(3, "卢俊义");
    10. HeroNode node4 = new HeroNode(4, "林冲");
    11. HeroNode node5 = new HeroNode(5, "关胜");
    12. //说明,我们先手动创建该二叉树,后面我们学习递归的方式创建二叉树
    13. root.setLeft(node2);
    14. root.setRight(node3);
    15. node3.setRight(node4);
    16. node3.setLeft(node5);
    17. binaryTree.setRoot(root);
    18. //测试
    19. // System.out.println("前序遍历"); // 1,2,3,5,4
    20. // binaryTree.preOrder();
    21. //测试
    22. // System.out.println("中序遍历");
    23. // binaryTree.infixOrder(); // 2,1,5,3,4
    24. //
    25. // System.out.println("后序遍历");
    26. // binaryTree.postOrder(); // 2,5,4,3,1
    27. //前序遍历
    28. //前序遍历的次数 :4
    29. // System.out.println("前序遍历方式~~~");
    30. // HeroNode resNode = binaryTree.preOrderSearch(5);
    31. // if (resNode != null) {
    32. // System.out.printf("找到了,信息为 no=%d name=%s", resNode.getNo(), resNode.getName());
    33. // } else {
    34. // System.out.printf("没有找到 no = %d 的英雄", 5);
    35. // }
    36. //中序遍历查找
    37. //中序遍历3次
    38. // System.out.println("中序遍历方式~~~");
    39. // HeroNode resNode = binaryTree.infixOrderSearch(5);
    40. // if (resNode != null) {
    41. // System.out.printf("找到了,信息为 no=%d name=%s", resNode.getNo(), resNode.getName());
    42. // } else {
    43. // System.out.printf("没有找到 no = %d 的英雄", 5);
    44. // }
    45. //后序遍历查找
    46. //后序遍历查找的次数 2次
    47. // System.out.println("后序遍历方式~~~");
    48. // HeroNode resNode = binaryTree.postOrderSearch(5);
    49. // if (resNode != null) {
    50. // System.out.printf("找到了,信息为 no=%d name=%s", resNode.getNo(), resNode.getName());
    51. // } else {
    52. // System.out.printf("没有找到 no = %d 的英雄", 5);
    53. // }
    54. //测试一把删除结点
    55. System.out.println("删除前,前序遍历");
    56. binaryTree.preOrder(); // 1,2,3,5,4
    57. binaryTree.delNode(5);
    58. //binaryTree.delNode(3);
    59. System.out.println("删除后,前序遍历");
    60. binaryTree.preOrder(); // 1,2,3,4
    61. }
    62. }
    63. //定义BinaryTree 二叉树
    64. class BinaryTree {
    65. private HeroNode root;
    66. public void setRoot(HeroNode root) {
    67. this.root = root;
    68. }
    69. //删除结点
    70. public void delNode(int no) {
    71. if(root != null) {
    72. //如果只有一个root结点, 这里立即判断root是不是就是要删除结点
    73. if(root.getNo() == no) {
    74. root = null;
    75. } else {
    76. //递归删除
    77. root.delNode(no);
    78. }
    79. }else{
    80. System.out.println("空树,不能删除~");
    81. }
    82. }
    83. //前序遍历
    84. public void preOrder() {
    85. if(this.root != null) {
    86. this.root.preOrder();
    87. }else {
    88. System.out.println("二叉树为空,无法遍历");
    89. }
    90. }
    91. //中序遍历
    92. public void infixOrder() {
    93. if(this.root != null) {
    94. this.root.infixOrder();
    95. }else {
    96. System.out.println("二叉树为空,无法遍历");
    97. }
    98. }
    99. //后序遍历
    100. public void postOrder() {
    101. if(this.root != null) {
    102. this.root.postOrder();
    103. }else {
    104. System.out.println("二叉树为空,无法遍历");
    105. }
    106. }
    107. //前序遍历
    108. public HeroNode preOrderSearch(int no) {
    109. if(root != null) {
    110. return root.preOrderSearch(no);
    111. } else {
    112. return null;
    113. }
    114. }
    115. //中序遍历
    116. public HeroNode infixOrderSearch(int no) {
    117. if(root != null) {
    118. return root.infixOrderSearch(no);
    119. }else {
    120. return null;
    121. }
    122. }
    123. //后序遍历
    124. public HeroNode postOrderSearch(int no) {
    125. if(root != null) {
    126. return this.root.postOrderSearch(no);
    127. }else {
    128. return null;
    129. }
    130. }
    131. }
    132. //先创建HeroNode 结点
    133. class HeroNode {
    134. private int no;
    135. private String name;
    136. private HeroNode left; //默认null
    137. private HeroNode right; //默认null
    138. public HeroNode(int no, String name) {
    139. this.no = no;
    140. this.name = name;
    141. }
    142. public int getNo() {
    143. return no;
    144. }
    145. public void setNo(int no) {
    146. this.no = no;
    147. }
    148. public String getName() {
    149. return name;
    150. }
    151. public void setName(String name) {
    152. this.name = name;
    153. }
    154. public HeroNode getLeft() {
    155. return left;
    156. }
    157. public void setLeft(HeroNode left) {
    158. this.left = left;
    159. }
    160. public HeroNode getRight() {
    161. return right;
    162. }
    163. public void setRight(HeroNode right) {
    164. this.right = right;
    165. }
    166. @Override
    167. public String toString() {
    168. return "HeroNode [no=" + no + ", name=" + name + "]";
    169. }
    170. //递归删除结点
    171. //1.如果删除的节点是叶子节点,则删除该节点
    172. //2.如果删除的节点是非叶子节点,则删除该子树
    173. public void delNode(int no) {
    174. //思路
    175. /*
    176. * 1. 因为我们的二叉树是单向的,所以我们是判断当前结点的子结点是否需要删除结点,而不能去判断当前这个结点是不是需要删除结点.
    177. 2. 如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将this.left = null; 并且就返回(结束递归删除)
    178. 3. 如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将this.right= null ;并且就返回(结束递归删除)
    179. 4. 如果第2和第3步没有删除结点,那么我们就需要向左子树进行递归删除
    180. 5. 如果第4步也没有删除结点,则应当向右子树进行递归删除.
    181. */
    182. //2. 如果当前结点的左子结点不为空,并且左子结点 就是要删除结点,就将this.left = null; 并且就返回(结束递归删除)
    183. if(this.left != null && this.left.no == no) {
    184. this.left = null;
    185. return;
    186. }
    187. //3.如果当前结点的右子结点不为空,并且右子结点 就是要删除结点,就将this.right= null ;并且就返回(结束递归删除)
    188. if(this.right != null && this.right.no == no) {
    189. this.right = null;
    190. return;
    191. }
    192. //4.我们就需要向左子树进行递归删除
    193. if(this.left != null) {
    194. this.left.delNode(no);
    195. }
    196. //5.则应当向右子树进行递归删除
    197. if(this.right != null) {
    198. this.right.delNode(no);
    199. }
    200. }
    201. //编写前序遍历的方法
    202. public void preOrder() {
    203. System.out.println(this); //先输出父结点
    204. //递归向左子树前序遍历
    205. if(this.left != null) {
    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 如果找到就返回该Node ,如果没有找到返回 null
    241. */
    242. public HeroNode preOrderSearch(int no) {
    243. System.out.println("进入前序遍历");
    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. public HeroNode infixOrderSearch(int no) {
    266. //判断当前结点的左子节点是否为空,如果不为空,则递归中序查找
    267. HeroNode resNode = null;
    268. if(this.left != null) {
    269. resNode = this.left.infixOrderSearch(no);
    270. }
    271. if(resNode != null) {
    272. return resNode;
    273. }
    274. System.out.println("进入中序查找");
    275. //如果找到,则返回,如果没有找到,就和当前结点比较,如果是则返回当前结点
    276. if(this.no == no) {
    277. return this;
    278. }
    279. //否则继续进行右递归的中序查找
    280. if(this.right != null) {
    281. resNode = this.right.infixOrderSearch(no);
    282. }
    283. return resNode;
    284. }
    285. //后序遍历查找
    286. public HeroNode postOrderSearch(int no) {
    287. //判断当前结点的左子节点是否为空,如果不为空,则递归后序查找
    288. HeroNode resNode = null;
    289. if(this.left != null) {
    290. resNode = this.left.postOrderSearch(no);
    291. }
    292. if(resNode != null) {//说明在左子树找到
    293. return resNode;
    294. }
    295. //如果左子树没有找到,则向右子树递归进行后序遍历查找
    296. if(this.right != null) {
    297. resNode = this.right.postOrderSearch(no);
    298. }
    299. if(resNode != null) {
    300. return resNode;
    301. }
    302. System.out.println("进入后序查找");
    303. //如果左右子树都没有找到,就比较当前结点是不是
    304. if(this.no == no) {
    305. return this;
    306. }
    307. return resNode;
    308. }
    309. }
    310. //
    1. package com.atguigu.tree;
    2. import java.util.HashSet;
    3. public class Test {
    4. @SuppressWarnings("unused")
    5. public static void main(String[] args) {
    6. }
    7. }