二叉树

📅 2026/7/28 15:56:54
二叉树
public static void main(String[] args) { //先需要创建一颗二叉树 BinaryTree binaryTreenew BinaryTree(); //创建需要的节点 HeroNode rootnew HeroNode(1,宋江); HeroNode node2new HeroNode(2,mao); HeroNode node3new HeroNode(3,gou); HeroNode node4new HeroNode(4,zhu); HeroNode node5new HeroNode(5,ma); //先手动创建 root.setLeft(node2); root.setRight(node3); node3.setRight(node4); node3.setLeft(node5); binaryTree.setRoot(root); //测试 /* //前序 System.out.println(前序); binaryTree.preOrder(); //中序 System.out.println(中序); binaryTree.infixOrder(); //前序 System.out.println(后序); binaryTree.postOrder(); */ //前序遍历 System.out.println(前序遍历方式); HeroNode resNodebinaryTree.postSearch(5); if(resNode!null) System.out.printf(找到了信息为no%d name%s,resNode.getVal(),resNode.getName()); else { System.out.printf(没有找到 no %d 的英雄,2); } } } //定义二叉树 class BinaryTree { private HeroNode root; public void setRoot(HeroNode root) { this.root root; } public void preOrder() { if(this.root!null) this.root.preOrder(); else System.out.println(无法遍历); } public void infixOrder() { if(this.root!null) this.root.infixOrder(); else System.out.println(无法遍历); } public void postOrder() { if(this.root!null) this.root.postOrder(); else System.out.println(无法遍历); } //前序查找 public HeroNode preSearch(int val) { if(this.root!null) return this.root.preOrdersearch(val); else System.out.println(无法遍历); return null; } public HeroNode infixSearch(int val) { if(this.root!null) return this.root.infixOrdersearch(val); else System.out.println(无法遍历); return null; } public HeroNode postSearch(int val) { if(this.root!null) return this.root.postOrdersearch(val); else System.out.println(无法遍历); return null; } } //先创建HeroNode class HeroNode { private int val; private String name; private HeroNode left; private HeroNode Right; public HeroNode(int val, String name) { this.val val; this.name name; } public int getVal() { return val; } public void setVal(int val) { this.val val; } public String getName() { return name; } public void setName(String name) { this.name name; } public HeroNode getLeft() { return left; } public void setLeft(HeroNode left) { this.left left; } public HeroNode getRight() { return Right; } public void setRight(HeroNode right) { Right right; } Override public String toString() { return HeroNode{ val val , name name \ }; } //编写前序遍历方法 public void preOrder() { System.out.println(this); if(this.left!null) this.left.preOrder(); if(this.Right!null) this.Right.preOrder(); } public void infixOrder() { if(this.left!null) this.left.infixOrder(); System.out.println(this); if(this.Right!null) this.Right.infixOrder(); } public void postOrder() { if(this.left!null) this.left.postOrder(); if(this.Right!null) this.Right.postOrder(); System.out.println(this); } //前序遍历查找 找到 返回该node 没找到 返回null public HeroNode preOrdersearch(int no) { //比较当前节点是不是 if(this.valno) return this; HeroNode resNodenull; if(this.left!null) { resNode this.left.preOrdersearch(no); } if(resNode!null) { System.out.println(找到了); //说明找到 return resNode; } if(this.Right!null) { resNode this.Right.preOrdersearch(no); } return resNode; } //中序遍历查找 找到 返回该node 没找到 返回null public HeroNode infixOrdersearch(int no) { HeroNode resNodenull; if(this.left!null) { resNode this.left.infixOrdersearch(no); } if(resNode!null) { //说明找到 return resNode; } System.out.println(进入中序查找); //比较当前节点是不是 if(this.valno) return this; if(this.Right!null) { resNode this.Right.infixOrdersearch(no); } return resNode; } //后序遍历查找 找到 返回该node 没找到 返回null public HeroNode postOrdersearch(int no) { HeroNode resNodenull; if(this.left!null) { resNode this.left.postOrdersearch(no); } if(resNode!null) { //说明找到 return resNode; } if(this.Right!null) { resNode this.Right.postOrdersearch(no); } if(resNode!null) { //说明找到 return resNode; } System.out.println(后续遍历); //比较当前节点是不是 if(this.valno) return this; return resNode; }