28.前中后序查找

1.前中后序查找思路分析
1.1前序查找
- 判断与当前节点是否相等,如果相等则返回当前节点,否则判断当前节点的左子节点是否为空
- 如果左子节点不为空,则递归前序查找左子树,找到就返回节点,否则就继续判断当前节点右子节点是否为空
- 如果右子节点不为空,则递归前序查找右子树,找到就返回节点,否则返回null
//前序查找
    public HeroNode preIndexSearch(int no){
        //判断与当前节点no是否相同,相同返回
        if(this.no==no){
            return this;
        }
        //判断左子节点是否为空,如果不为空,递归前序查询左子树,如果相等,返回
        HeroNode temp = null;
        if(this.left!=null){
            temp =  this.left.preIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.right!=null){
            temp = this.right.preIndexSearch(no);
        }
        return temp;
    }
1.2中序查找
- 判断当前节点的左子节点是否为空,如果不为空就递归中序查找,找到就返回
- 否则与当前节点比较,如果相等就返回,否则继续判断当前节点的右子节点是否为空
-如果右子节点不为空,就递归中序查找右子树,找到就返回,否则返回null
 //中序查找
    public HeroNode infixIndexSearch(int no){
        HeroNode temp = null;
        if(this.left!=null){
            temp = this.left.infixIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if (this.no==no){
            return this;
        }
        if(this.right!=null){
            temp = this.right.infixIndexSearch(no);
        }
        return temp;
    }
1.3后序查找
- 判断当前节点的左子树是否为空,如果不为空,则递归后序查找左子树,找到就返回
-否则判断当前节点的右子节点是否为空,如果不为空,则递归查找右子树,找到就返回
- 否则与当前节点比较,相等返回当前节点,否则返回null
//后序查找
    public HeroNode postIndexSearch(int no){
        HeroNode temp = null;
        if(this.left!=null){
            temp = this.left.postIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.right!=null){
            temp = this.right.postIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.no==no){
            return this;
        }
        return temp;
    }

2.完整代码

package com.yc.day06;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "卢俊义");
        HeroNode node4 = new HeroNode(4, "林冲");
        binaryTree.setRoot(root);
        root.setLeft(node2);
        root.setRight(node3);
        node3.setRight(node4);
        System.out.println("前序遍历:");
        binaryTree.preSort();
        System.out.println("");
        HeroNode heroNode = binaryTree.preIndexSearch(2);
        System.out.println("前序查找结果:"+heroNode);
        HeroNode heroNode1 = binaryTree.infixIndexSearch(2);
        System.out.println("中序查找结果:"+heroNode1);
        HeroNode heroNode2 = binaryTree.postIndexSearch(2);
        System.out.println("后序查找结果:"+heroNode2);
    }
}
//创建二叉树
class BinaryTree{
    HeroNode root;
    public void setRoot(HeroNode heroNode){
        this.root = heroNode;
    }
    //前序遍历
    public void preSort(){
        root.preSort();
    }
    //中序遍历
    public void infixSort(){
        root.infixSort();
    }
    //后序遍历
    public void postSort(){
        root.postSort();
    }
    //前序查找
    public HeroNode preIndexSearch(int no){
        if(root!=null){
            return root.preIndexSearch(no);
        }
       return null;
    }
    //中序查找
    public HeroNode infixIndexSearch(int no){
        if(root!=null){
            return root.infixIndexSearch(no);
        }
        return null;
    }
    //后序查找
    public HeroNode postIndexSearch(int no){
        if(root!=null) {
            return root.postIndexSearch(no);
        }
        return null;
    }
}
//创建节点类
class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private HeroNode right;

    public HeroNode() {
    }

    public HeroNode(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 HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍历
    public void preSort(){
        System.out.println(this);
        if(this.left!=null){
            this.left.preSort();
        }
        if(this.right!=null){
            this.right.preSort();
        }
    }
    //中序遍历
    public void infixSort(){
        if(this.left!=null){
            this.left.infixSort();
        }
        System.out.println(this);
        if(this.right!=null){
            this.right.infixSort();
        }
    }
    //后序遍历
    public void postSort(){
        if(this.left!=null){
            this.left.postSort();
        }
        if(this.right!=null){
            this.right.postSort();
        }
        System.out.println(this);
    }
    //前序查找
    public HeroNode preIndexSearch(int no){
        //判断与当前节点no是否相同,相同返回
        if(this.no==no){
            return this;
        }
        //判断左子节点是否为空,如果不为空,递归前序查询左子树,如果相等,返回
        HeroNode temp = null;
        if(this.left!=null){
            temp =  this.left.preIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.right!=null){
            temp = this.right.preIndexSearch(no);
        }
        return temp;
    }
    //中序查找
    public HeroNode infixIndexSearch(int no){
        HeroNode temp = null;
        if(this.left!=null){
            temp = this.left.infixIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if (this.no==no){
            return this;
        }
        if(this.right!=null){
            temp = this.right.infixIndexSearch(no);
        }
        return temp;
    }
    //后序查找
    public HeroNode postIndexSearch(int no){
        HeroNode temp = null;
        if(this.left!=null){
            temp = this.left.postIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.right!=null){
            temp = this.right.postIndexSearch(no);
        }
        if(temp!=null){
            return temp;
        }
        if(this.no==no){
            return this;
        }
        return temp;
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容