public class BinaryTreeDemo {
public static void main(String[] args) {
BinaryTree binaryTree=new BinaryTree();
HerNode root=new HerNode(1,"宋江");
HerNode node2=new HerNode(2,"吴用");
HerNode node3=new HerNode(3,"张三");
HerNode node4=new HerNode(4,"李四");
HerNode node5=new HerNode(5,"王五");
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
/*
System.out.println("前序遍历查找");
HerNode resNode=binaryTree.preOrderSearch(5);
if(resNode!=null){
System.out.printf("找到了,信息为 no=%d name=%s",resNode.getNo(),resNode.getName());
}else{
System.out.printf("没有找到 no=%d 的英雄",5);
}
*/
/*
System.out.println("中序遍历查找");
HerNode resNode=binaryTree.infixOrderSearch(5);
if(resNode!=null){
System.out.printf("找到了,信息为 no=%d name=%s",resNode.getNo(),resNode.getName());
}else{
System.out.printf("没有找到 no=%d 的英雄",5);
}
*/
System.out.println("后序遍历查找");
HerNode resNode = binaryTree.postOrderSearch(5);
if(resNode!=null){
System.out.printf("找到了,信息为 no=%d name=%s",resNode.getNo(),resNode.getName());
}else{
System.out.printf("没有找到 no=%d 的英雄",5);
}
}
}
class BinaryTree{
private HerNode root;
public void setRoot(HerNode root) {
this.root = root;
}
//前序遍历查找
public HerNode preOrderSearch(int no){
if(root!=null){
return root.preOrderSearch(no);
}
else return null;
}
//中序遍历查找
public HerNode infixOrderSearch(int no){
if(root!=null){
return root.infixOrderSearch(no);
}
else return null;
}
public HerNode postOrderSearch(int no){
if(root!=null){
return root.postOrderSearch(no);
}
else return null;
}
}
class HerNode{
private int no;
private String name;
private HerNode left;
private HerNode right;
public HerNode(int no,String name){
this.no=no;
this.name=name;
}
public int getNo() {
return no;
}
public void setNo(int no) {
this.no = no;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public HerNode getLeft() {
return left;
}
public void setLeft(HerNode left) {
this.left = left;
}
public HerNode getRight() {
return right;
}
public void setRight(HerNode right) {
this.right = right;
}
@Override
public String toString() {
return "HerNode{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
//先序遍历查找
public HerNode preOrderSearch(int no){
//先看看根节点
System.out.println("先看看前序遍历");
if(this.no==no)
return this;
HerNode resNode=null;
//再看看左子节点
if(this.left!=null){
resNode=this.left.preOrderSearch(no);
}
if(resNode!=null)
return resNode;
//最后看看右子节点
if(this.right!=null){
resNode=this.right.preOrderSearch(no);
}
//不管找没找到 都要返回resNode
return resNode;
}
//中序遍历查找
public HerNode infixOrderSearch(int no){
HerNode resNode=null;
if(this.left!=null){
resNode=this.left.infixOrderSearch(no);
}
if(this.no==no){
return this;
}
System.out.println("进入中序查找");
if(resNode!=null){
return resNode;
}
if(this.right!=null){
resNode=this.right.infixOrderSearch(no);
}
return resNode;
}
public HerNode postOrderSearch(int no){
HerNode resNode=null;
if(this.left!=null){
resNode=this.left.postOrderSearch(no);
}
if(this.no==no){
return this;
}
if(resNode!=null){
return resNode;
}
if(this.right!=null){
resNode=this.right.postOrderSearch(no);
}
System.out.println("进入后序查找");
return resNode;
}
}
----参考:韩顺平数据结构