10.2二叉树查找-创新互联
目录
在江山等地区,都构建了全面的区域性战略布局,加强发展的系统性、市场前瞻性、产品创新能力,以专注、极致的服务理念,为客户提供做网站、成都网站制作 网站设计制作按需定制网站,公司网站建设,企业网站建设,成都品牌网站建设,成都全网营销,成都外贸网站建设公司,江山网站建设费用合理。概述
代码
概述代码
public class BinaryTreeDemo {
public static void main(String[] args) {
//先需要创建一颗二叉树
BinaryTree binaryTree = new BinaryTree();
//创建需要的结点
HeroPoint root = new HeroPoint(1, "宋江");
HeroPoint node2 = new HeroPoint(2, "吴用");
HeroPoint node3 = new HeroPoint(3, "卢俊义");
HeroPoint node4 = new HeroPoint(4, "林冲");
HeroPoint node5 = new HeroPoint(5, "关胜");
//先手动创建该二叉树,后面递归的方式创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
System.out.println("前序遍历方式~~~");
HeroPoint resNode = binaryTree.lefSearch(1);
if (resNode != null) {
System.out.printf("找到了,信息为 no=%d name=%s", resNode.getId(), resNode.getName());
} else {
System.out.println("没有找到");
}
//中序遍历查找
System.out.println("中序遍历方式~~~");
resNode = binaryTree.centerSearch(5);
if (resNode != null) {
System.out.printf("找到了,信息为 no=%d name=%s", resNode.getId(), resNode.getName());
} else {
System.out.println("没有找到");
}
//后序遍历查找
System.out.println("后序遍历方式~~~");
resNode = binaryTree.finalSearch(5);
if (resNode != null) {
System.out.printf("找到了,信息为 no=%d name=%s", resNode.getId(), resNode.getName());
} else {
System.out.println("没有找到");
}
}
}
class BinaryTree{
private HeroPoint root;
public void setRoot(HeroPoint root) {
this.root = root;
}
public HeroPoint lefSearch(int no){
if (this.root != null){
return this.root.perSearch(no);
}else {
System.out.println("查找失败, 二叉树为空");
return null;
}
}
public HeroPoint centerSearch(int no){
if (this.root != null){
return this.root.centerSearch(no);
}else {
System.out.println("查找失败, 二叉树为空");
return null;
}
}
public HeroPoint finalSearch(int no){
if (this.root != null){
return this.root.finalSearch(no);
}else {
System.out.println("查找失败, 二叉树为空");
return null;
}
}
}
class HeroPoint{
private int id;
private String name;
private HeroPoint left;
private HeroPoint right;
public HeroPoint perSearch(int no){
if ( no == this.id){
return this;
}
HeroPoint temp = null;
if (this.left != null){
temp = this.left.perSearch(no);
}
if (temp != null){
return temp;
}
if (this.right != null){
temp = this.right.perSearch(no);
}
return temp;
}
public HeroPoint centerSearch(int no){
HeroPoint temp = null;
if (this.left != null){
temp = this.left.perSearch(no);
}
if (temp != null){
return temp;
}
if ( no == this.id){
return this;
}
if (this.right != null){
temp = this.right.perSearch(no);
}
return temp;
}
public HeroPoint finalSearch(int no){
HeroPoint temp = null;
if (this.left != null){
temp = this.left.perSearch(no);
}
if (temp != null){
return temp;
}
if (this.right != null){
temp = this.right.perSearch(no);
}
if (temp != null){
return temp;
}
if ( no == this.id){
return this;
}
return null;
}
public HeroPoint(int id, String name) {
this.id = id;
this.name = name;
}
}
你是否还在寻找稳定的海外服务器提供商?创新互联www.cdcxhl.cn海外机房具备T级流量清洗系统配攻击溯源,准确流量调度确保服务器高可用性,企业级服务器适合批量采购,新人活动首月15元起,快前往官网查看详情吧
文章题目:10.2二叉树查找-创新互联
网站地址:http://ybzwz.com/article/jdsih.html