栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/362398.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号