1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
| public class BinaryTreeDemo {
public static void main(String[] args) { HeroNode root = new HeroNode(1, "宋江"); HeroNode node2 = new HeroNode(2, "吴用"); HeroNode node3 = new HeroNode(3, "卢俊义"); HeroNode node4 = new HeroNode(4, "林冲"); HeroNode node5 = new HeroNode(5, "关胜"); root.setLeft(node2); root.setRight(node3); node3.setLeft(node4); node3.setRight(node5);
BinaryTree binaryTree = new BinaryTree(root); System.out.println("前序遍历查找的方法:"); int searchNo = 3; HeroNode resNode1 = binaryTree.preOrderSearch(searchNo); if(resNode1 != null) { System.out.printf("找到了,no=%d name=%s",resNode1.getNo(),resNode1.getName()); }else { System.out.printf("没有找到 no=%d 的英雄",searchNo); } System.out.println(); System.out.println("中序遍历查找的方法:"); HeroNode resNode2 = binaryTree.infixOrderSearch(searchNo); if(resNode2 != null) { System.out.printf("找到了,no=%d name=%s",resNode2.getNo(),resNode2.getName()); }else { System.out.printf("没有找到 no=%d 的英雄",searchNo); } System.out.println(); System.out.println("后序遍历查找的方法:"); HeroNode resNode3 = binaryTree.postOrderSearch(searchNo); if(resNode3 != null) { System.out.printf("找到了,no=%d name=%s",resNode3.getNo(),resNode3.getName()); }else { System.out.printf("没有找到 no=%d 的英雄",searchNo); } }
}
|