Java数据结构与算法(二叉树)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值