前言
最近面临毕业就业,在复习数据结构与算法,为了更好地掌握,加深印象,所以决定写一些博客来知识复现。
温馨提示:这篇博客可能不适合刚学数据结构的新手。
代码实现
package sjjg;
public class BinarySearchTree {
public static void main(String[] args) {
// TODO Auto-generated method stub
BinaryTree3 binaryTree = new BinaryTree3();
HeroNode3 root = new HeroNode3(1, "宋江");
HeroNode3 node2 = new HeroNode3(2, "吴用");
HeroNode3 node3 = new HeroNode3(3, "卢俊义");
HeroNode3 node4 = new HeroNode3(4, "林冲");
HeroNode3 node5 = new HeroNode3(5, "关胜");
root.setLeft(node2);
root.setRight(node3);
node3.setRight(node4);
node3.setLeft(node5);
binaryTree.setRoot(root);
System.out.println("前序查找-----");
HeroNode3 resNode = binaryTree.preSearch(3);
if (resNode != null) {
System.out.printf("找到了,信息为 no=%d name=%s", resNode.getNo(), resNode.getName());
} else {
System.out.printf("没有找到 no = %d 的英雄", 5);
}
HeroNode3 resNode2 = binaryTree.infixSearch(3);
System.out.println("\n中序查找-----");
if (resNode2 != null) {
System.out.printf("找到了,信息为 no=%d name=%s", resNode2.getNo(), resNode2.getName());
} else {
System.out.printf("没有找到 no = %d 的英雄", 5);
}
HeroNode3 resNode3 = binaryTree.preSearch(3);
System.out.println("\n后序查找-----");
if (resNode3 != null) {
System.out.printf("找到了,信息为 no=%d name=%s", resNode3.getNo(), resNode3.getName());
} else {
System.out.printf("没有找到 no = %d 的英雄", 5);
}
}
}
//先创建HeroNode 结点
class HeroNode3 {
private int no;
private String name;
private HeroNode3 left; // 默认null
private HeroNode3 right; // 默认null
public HeroNode3(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 HeroNode3 getLeft() {
return left;
}
public void setLeft(HeroNode3 left) {
this.left = left;
}
public HeroNode3 getRight() {
return right;
}
public void setRight(HeroNode3 right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode [no=" + no + ", name=" + name + "]";
}
// 前序查找
public HeroNode3 preSearch(int no) {
if (this.no == no) {
return this;
}
HeroNode3 resNode = null;
if (this.left != null) {
resNode = this.left.preSearch(no);
}
if (this.right != null) {
resNode = this.right.preSearch(no);
}
return resNode;
}
// 中序查找
public HeroNode3 infixSearch(int no) {
HeroNode3 resNode = null;
if (this.left != null) {
resNode = this.left.infixSearch(no);
}
if (this.no == no) {
return this;
}
if (this.right != null) {
resNode = this.right.infixSearch(no);
}
return resNode;
}
// 后序查找
public HeroNode3 postSearch(int no) {
HeroNode3 resNode = null;
if (this.left != null) {
resNode = this.left.postSearch(no);
}
if (this.right != null) {
resNode = this.right.postSearch(no);
}
if (this.no == no) {
return this;
}
return resNode;
}
}
class BinaryTree3 {
private HeroNode3 root;
public HeroNode3 getRoot() {
return root;
}
public void setRoot(HeroNode3 root) {
this.root = root;
}
//前序查找
public HeroNode3 preSearch(int no) {
if (root != null) {
return root.preSearch(no);
} else {
return null;
}
}
//中序查找
public HeroNode3 infixSearch(int no) {
if (root != null) {
return root.infixSearch(no);
} else {
return null;
}
}
//后序查找
public HeroNode3 postSearch(int no) {
if (root != null) {
return root.postSearch(no);
} else {
return null;
}
}
}
结束语
欢迎访问我的博客,一起学习,一起进步!