1.二叉树-------查找指定节点
1)编写前序查找,中序查找和后序查找的方法
2)并分别用三种查找的方式,查找heroNO=5的节点
3)分析各种查找方式,分别比较查找了多少次
4)思路分析图解
2. 代码实现
package BinaryTreeDemo;
public class BinaryTreeSearch {
public static void main(String[] args) {
//创建一颗二叉树
BinaryTree1 binaryTree1=new BinaryTree1();
//创建需要的节点
HeroNode1 root=new HeroNode1(1,"宋江");
HeroNode1 node2=new HeroNode1(2,"吴用");
HeroNode1 node3=new HeroNode1(3,"卢俊义");
HeroNode1 node4=new HeroNode1(4,"林冲");
HeroNode1 node5=new HeroNode1(5,"关胜");
//手动创建二叉树
root.setLeft(node2);
root.setRight(node3);
node3.setLeft(node4);
node3.setRight(node5);
binaryTree1.setRoot(root);
//前序遍历
System.out.println("前序遍历的方式--");
HeroNode1 heroNode = binaryTree1.preOrdersearch(5);
if (heroNode!=null){
System.out.printf("找到了,信息为no=%d name=%s",heroNode.getNo(),heroNode.getName());
}else {
System.out.println("没有找到");
}
// //中序遍历
// System.out.println("中序遍历的方式--");
// HeroNode1 heroNode1 = binaryTree1.infixOrdersearch(5);
// if (heroNode1!=null){
// System.out.printf("找到了,信息为no=%d name=%s",heroNode1.getNo(),heroNode1.getName());
// }else {
// System.out.println("没有找到");
// }
//
// //后序遍历
// System.out.println("后序遍历的方式--");
// HeroNode1 heroNode2 = binaryTree1.postOrdersearch(5);
// if (heroNode2!=null){
// System.out.printf("找到了,信息为no=%d name=%s",heroNode2.getNo(),heroNode2.getName());
// }else {
// System.out.println("没有找到");
// }
}
}
//定义BinaryTree二叉树
class BinaryTree1{
private HeroNode1 root;
public void setRoot(HeroNode1 root){
this.root=root;
}
//前序遍历
public HeroNode1 preOrdersearch(int no){
if (root!=null){
return root.preOrdersearch(no);
}else {
return null;
}
}
//中序遍历
public HeroNode1 infixOrdersearch(int no){
if (root!=null){
return root.infixOrdersearch(no);
}else {
return null;
}
}
//后序遍历
public HeroNode1 postOrdersearch(int no){
if (root!=null){
return root.postOrdersearch(no);
}else {
return null;
}
}
}
//先创建heroNode节点
class HeroNode1{
private int no;
private String name;
private HeroNode1 left;
private HeroNode1 right;
public HeroNode1(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 HeroNode1 getLeft() {
return left;
}
public void setLeft(HeroNode1 left) {
this.left = left;
}
public HeroNode1 getRight() {
return right;
}
public void setRight(HeroNode1 right) {
this.right = right;
}
@Override
public String toString() {
return "HeroNode1{" +
"no=" + no +
", name='" + name + '\'' +
'}';
}
//前序遍历查找
public HeroNode1 preOrdersearch(int no){
System.out.println("进入前序遍历");
//比较当前节点是不是
if (this.no==no){
return this;
}
//1.判断当前节点的左子节点是否为空,如果不为空,则递归前序查找
//2.如果左递归前序查找,找到节点,返回
HeroNode1 resNode=null;
if (this.left!=null){
resNode=this.left.preOrdersearch(no);
}
if (resNode!=null){
//说明左子树找到
return resNode;
}
//1.右递归前序遍历,找到节点就返回空,不然继续判断
//2.当前右节点是否为空,如果不为空,就继续向右节点前序查找
if (this.right!=null){
resNode=this.right.preOrdersearch(no);
}
if (resNode!=null){
//说明左子树找到
return resNode;
}
return resNode;
}
//中序遍历查找
public HeroNode1 infixOrdersearch(int no){
//1.判断当前节点的左子节点是否为空,如果不为空,则递归中序查找
//2.如果左递归前序查找,找到节点,返回
HeroNode1 resNode=null;
if (this.left!=null){
resNode=this.left.infixOrdersearch(no);
}
if (resNode!=null){
//说明左子树找到
return resNode;
}
System.out.println("进入中序遍历");
//比较当前节点是不是
if (this.no==no){
return this;
}
//1.右递归中序遍历,找到节点就返回空,不然继续判断
//2.当前右节点是否为空,如果不为空,就继续向右节点中序查找
if (this.right!=null){
resNode=this.right.infixOrdersearch(no);
}
return resNode;
}
//后序遍历查找
public HeroNode1 postOrdersearch(int no){
//1.判断当前节点的左子节点是否为空,如果不为空,则递归后序查找
//2.如果左递归前序查找,找到节点,返回
HeroNode1 resNode=null;
if (this.left!=null){
resNode=this.left.postOrdersearch(no);
}
if (resNode!=null){
//说明左子树找到
return resNode;
}
//1.右递归后序遍历,找到节点就返回空,不然继续判断
//2.当前右节点是否为空,如果不为空,就继续向右节点后序查找
if (this.right!=null){
resNode=this.right.postOrdersearch(no);
}
if (resNode!=null){
//说明右子树找到
return resNode;
}
System.out.println("进入后序遍历");
//比较当前节点是不是
if (this.no==no){
return this;
}
return resNode;
}
}