二叉树的遍历,查找和删除

二叉树的遍历

二叉树的遍历分为三种:前序,后序,后序

1)前序遍历:先输出父节点,在输出左子树和右子树。

2)中序遍历:先输出左子树,再输出父节点,最后输出右子树。

3)后序遍历:先输出左子树,再输出右子树,最后输出父节点。

二叉树的遍历,根据父节点的输出顺序,判断哪种类型的遍历。

遍历二叉树

前序遍历:

1)先输出当前节点(最初为根节点)

2)如果当前节点的左子树不为空的话,递归进入左子树前序遍历

3)如果当前节点的右子树不为空的话,递归进入右子树前序遍历

中序遍历:

1)如果当前节点的左子树不为空的话,递归进入左子树前序遍历

2)先输出当前节点(最初为根节点)

3)如果当前节点的右子树不为空的话,递归进入右子树前序遍历

后序遍历:

1)如果当前节点的左子树不为空的话,递归进入左子树前序遍历

2)如果当前节点的右子树不为空的话,递归进入右子树前序遍历 

3)先输出当前节点(最初为根节点)

查找指定节点

前序遍历查找:

1)先判断当前节点是否为要查找的节点。

2)如果是需要查找的节点,则返回当前节点。

3)如果不是需要查找的节点,则判断左子树是否为null,不是的话,递归前序查找。

4)如果左子树找到需要查找的节点,则返回,否则判断右子树是否为null,,如果不为null,递归前序

查找。

删除节点

1)如果当前节点的左子树不为空,并且左子节点就是要删除的节点,此时,this.left=null,并且返回

2)如果当前节点的右子树不为空,并且左子节点就是要删除的节点,此时,this.right=null,并且返回

3)如果前两个步骤都没有找到要删除的节点,此时,向左子树递归删除

4)如果前三个步骤都没有找到要删除的节点,此时,向右子树递归删除

源代码

package cn.xlystar;

public class Solution{

    public static void main(String[] args) {

        HeroNode root =new HeroNode(1);

        HeroNode heroNode =new HeroNode(2);

        HeroNode heroNode1 =new HeroNode(3);

        HeroNode heroNode2 =new HeroNode(4);

        HeroNode heroNode3 =new HeroNode(5);

        root.setLeft(heroNode);

        root.setRight(heroNode1);

        heroNode.setLeft(heroNode2);

        heroNode.setRight(heroNode3);

        BinTree binTree =new BinTree();

        binTree.setRoot(root);

        // 前序遍历

        binTree.preOrder();

//        // 中序遍历

//        binTree.infixOrder();

//        // 后序遍历

//        binTree.postOrder();

        // 根据需要搜索相关节点

        System.out.println(binTree.postOrderSearch(2));

    }

}

class BinTree{

    private HeroNoderoot;

    public HeroNodegetRoot() {

        return root;

    }

    public void setRoot(HeroNode root) {

        this.root = root;

    }

    // 前序遍历

    public void preOrder() {

        if (this.root !=null) {

            this.root.preOrder();

        } else {

            System.out.println("此树为空树");

        }

}

    // 中序遍历

    public void infixOrder() {

        if (this.root !=null) {

            this.root.infixOrder();;

        } else {

            System.out.println("此树为空树");

        }

}

    // 后序遍历

    public void postOrder() {

        if (this.root !=null) {

            this.root.postOrder();

        } else {

            System.out.println("此树为空树");

        }

}

    public HeroNodepreOrderSearch(int no) {

        if (this.root !=null) {

            return this.root.preOrderSearch(no);

        } else {

            return null;

        }

}

    public HeroNodeinfixOrderSearch(int no) {

        if (this.root !=null) {

            return this.root.infixOrderSearch(no);

        } else {

            return null;

        }

}

    public HeroNodepostOrderSearch(int no) {

        if (this.root !=null) {

            return this.root.postOrderSearch(no);

        } else {

            return null;

        }

}

}

class HeroNode{

    private HeroNodeleft;

    private HeroNoderight;

    private int no;

    public HeroNode(int no) {

        this.no = no;

    }

    public int getNo() {

        return no;

    }

    public void setNo(int no) {

        this.no = no;

    }

    public HeroNodegetLeft() {

        return left;

    }

    public void setLeft(HeroNode left) {

        this.left = left;

    }

    public HeroNodegetRight() {

        return right;

    }

    public void setRight(HeroNode right) {

        this.right = right;

    }

    // 前序遍历

    public void preOrder() {

        System.out.println(this);

        if (this.left !=null) {

            this.left.preOrder();

        }

        if (this.right !=null) {

            this.right.preOrder();

        }

}

    // 中序遍历

    public void infixOrder() {

        if (this.left!=null) {

            this.left.infixOrder();

        }

        System.out.println(this);

        if (this.right !=null) {

            this.right.infixOrder();

        }

}

    //后序遍历

    public void postOrder() {

        if (this.left !=null) {

            this.left.postOrder();

        }

        if (this.right !=null) {

            this.right.postOrder();

        }

        System.out.println(this);

    }

    public HeroNodepreOrderSearch(int no) {

        if (this.no == no) {

            return this;

        }

        HeroNode anw =null;

        if (this.left !=null) {

            anw =this.left.preOrderSearch(no);

        }

        if (anw!=null) {

            return anw;

        }

        if (this.right !=null) {

            anw =this.left.preOrderSearch(no);

        }

        return anw;

    }

    public HeroNodeinfixOrderSearch(int no) {

        HeroNode anw =null;

        if (this.left !=null) {

            anw =this.left.infixOrderSearch(no);

        }

        if (anw!=null) {

            return anw;

        }

        if (this.no == no) {

            return this;

        }

        if (this.right !=null) {

            anw =this.left.infixOrderSearch(no);

        }

        return anw;

    }

    public HeroNodepostOrderSearch(int no) {

        HeroNode anw =null;

        if (this.left !=null) {

            anw =this.left.postOrderSearch(no);

        }

        if (anw!=null) {

            return anw;

        }

        if (this.right !=null) {

            anw =this.left.postOrderSearch(no);

        }

        if (anw !=null) {

            return anw;

        }

        if (this.no == no) {

            return this;

        }

        return anw;

    }

    @Override

    public StringtoString() {

        return "HeroNode{" +

"left=" +left +

", right=" +right +

", no=" +no +

'}';

    }

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 把写代码过程中经常用的内容记录起来,下边代码是关于java二叉树查找、遍历、添加与删除的代码。 package c...
    laohuli阅读 459评论 0 2
  • "use strict";function _classCallCheck(e,t){if(!(e instanc...
    久些阅读 2,054评论 0 2
  • 二叉树:每个节点最多有两个孩子,是一种动态数据结构,具有递归结构。 其中空树和只有根节点的树都是二叉树。 满二叉树...
    代夫阿普曼阅读 367评论 0 4
  • 二叉树上 1.开篇思考题 二叉树有哪几种存储方式?什么样的二叉树适合用数组来存储? 2.二叉树 树的高度,深度,层...
    木木_6088阅读 376评论 0 0
  • 偶然在明信片上看到这么一段话:每个人心中,都有一只已经走失,或从未到来的猫。在花开烂漫的午后,在寂静无人的深夜,温...
    慕敖阅读 1,063评论 9 11