public class BinaryDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
HeroNode2 hero1 = new HeroNode2(3, "张三");
HeroNode2 hero2 = new HeroNode2(5, "李四");
HeroNode2 hero3 = new HeroNode2(2, "王二");
HeroNode2 hero4 = new HeroNode2(8, "小明");
HeroNode2 hero5 = new HeroNode2(9, "小红");
BinaryTree b1 = new BinaryTree();
b1.setRoot(hero1);
b1.add(hero2);
b1.add(hero3);
b1.add(hero4);
b1.add(hero5);
System.out.println(b1.perOrderSearch(8).toString());
System.out.println(b1.midOrderSearch(8).toString());
System.out.println(b1.postOrderSearch(8).toString());;
}
}
class BinaryTree {
private HeroNode2 root;
public void setRoot(HeroNode2 root) {
this.root = root;
}
// 前序遍历
public void perOrder() {
if (root != null) {
root.perOrder();
} else {
System.out.println("二叉树为空");
}
}
// 前序查找
public HeroNode2 perOrderSearch(int no) {
if(root!=null) {
return root.perOrderSearch(no);
}else {
return null;
}
}
// 中序遍历
public void midOrder() {
if (root != null) {
root.midOrder();
} else {
System.out.println("二叉树为空");
}
}
// 中序查找
public HeroNode2 midOrderSearch(int no) {
if(root!=null) {
return root.perOrderSearch(no);
}else {
return null;
}
}
// 后序遍历
public void postOrder() {
if (root != null) {
root.postOrder();
} else {
System.out.println("二叉树为空");
}
}
// 后序查找
public HeroNode2 postOrderSearch(int no) {
if(root!=null) {
return root.perOrderSearch(no);
}else {
return null;
}
}
// 添加元素
public void add(HeroNode2 heroNode) {
if (root == null) {
root = heroNode;
return;
}
HeroNode2 head = root;
while (true) {
if (head.getNo() > heroNode.getNo()) {
if (head.getLeft() == null) {
head.setLeft(heroNode);
return;
}
head = head.getLeft();
}
if (head.getNo() < heroNode.getNo()) {
if (head.getRight() == null) {
head.setRight(heroNode);
return;
}
head = head.getRight();
}
}
}
}
class HeroNode2 {
private int no;
private String name;
private HeroNode2 left;
private HeroNode2 right;
public HeroNode2(int no, String name) {
super();
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 HeroNode2 getLeft() {
return left;
}
public void setLeft(HeroNode2 left) {
this.left = left;
}
public HeroNode2 getRight() {
return right;
}
public void setRight(HeroNode2 right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + "]";
}
// 编写前序遍历的方法
public void perOrder() {
System.out.println(this.toString());
if (this.left != null) {
this.left.perOrder();
}
if (this.right != null) {
this.right.perOrder();
}
}
// 前序查找
public HeroNode2 perOrderSearch(int no) {
if (this.no == no) {
return this;
}
// if(this.left!=null) {
// return this.left.perOrderSearch(no);
// }
// if(this.right!=null) {
// return this.right.perOrderSearch(no);
// }
// return null;
// 不能这样写,如果查找的元素在右边回使提前返回null,导致提前结束
HeroNode2 heroNode = null;
// 不能HeroNode2 heroNode;要初始化
if (this.left != null) {
heroNode = this.left.perOrderSearch(no);
}
if (heroNode != null) {
return heroNode;
}
if (this.right != null) {
heroNode = this.right.perOrderSearch(no);
}
return heroNode;
}
// 中序遍历
public void midOrder() {
if (this.left == null) {
this.left.midOrder();
}
System.out.println(this.toString());
if (this.right == null) {
this.right.midOrder();
}
}
// 中序查找
public HeroNode2 midOrderSearch(int no) {
if (this.no == no) {
return this;
}
HeroNode2 heroNode = null;
if (this.left != null) {
heroNode = this.left.midOrderSearch(no);
}
if (heroNode != null) {
return heroNode;
}
if (this.right != null) {
heroNode = this.right.midOrderSearch(no);
}
return heroNode;
}
// 后序遍历
public void postOrder() {
if (this.left == null) {
this.left.postOrder();
}
if (this.right == null) {
this.right.postOrder();
}
System.out.println(this.toString());
}
// 后序查找
public HeroNode2 postOrderSearch(int no) {
if (this.no == no) {
return this;
}
HeroNode2 heroNode = null;
if (this.left != null) {
heroNode = this.left.postOrderSearch(no);
}
if (heroNode != null) {
return heroNode;
}
if (this.right != null) {
heroNode = this.right.postOrderSearch(no);
}
return heroNode;
}