二叉树的前中后序遍历java实现

package com.atguigu.Tree;


/**
 * 二叉树的前中后序遍历
 *
 *
 *                     1、宋江
 *                   ·       ·
 *                  ·          ·
 *                2、吴用        3、卢俊义
 *                                      ·
 *                                         ·
 *                                         4、林冲
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();

        HeroNode node = new HeroNode(1,"宋江");
        HeroNode node2 = new HeroNode(2,"吴用");
        HeroNode node3 = new HeroNode(3,"卢俊义");
        HeroNode node4 = new HeroNode(4,"林冲");

        tree.setRoot(node);
        node.setLeft(node2);
        node.setRight(node3);
        node3.setRight(node4);

        System.out.println("前序遍历结果为");//1,2,3,4
        tree.preOrder();

        System.out.println("中序遍历结果为");//2,1,3,4
        tree.infixOrder();
        System.out.println("后序遍历结果为");//2,4,3,1
        tree.postOrder();

        int no = 4;
        System.out.println("查找编号为" + no + "的前序查找的结果为" + tree.preOrderSearch(no));

        System.out.println("查找编号为" + no + "的中序查找的结果为" + tree.infixOrderSearch(no));

        System.out.println("查找编号为" + no + "的后序查找的结果为" + tree.postOrderSearch(no));

        int delno = 4;
        System.out.println("删除节点" + delno + "前的二叉树为");
        tree.preOrder();
        Boolean result = tree.delHeroNode(delno);
        System.out.println(result?"删除成功":"删除失败");
        System.out.println("删除节点" + delno + "后的二叉树为");
        tree.preOrder();
    }
}
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;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "排名" + no +
                "名字'" + 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;
    }
    //前序遍历
    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 postfixOrder(){
        if (this.left != null){
            this.left.postfixOrder();
        }
        if (this.right != null){
            this.right.postfixOrder();
        }
        System.out.println(this);
    }

    //前序遍历查找
    public HeroNode preOederSearch(int no){
        System.out.println("前序比较次数");
        if (this.no == no){
            return this;
        }
        HeroNode resHeronode = null;
        if (this.left != null){
            resHeronode = this.left.preOederSearch(no);
        }
        if (resHeronode != null){
            return resHeronode;
        }
        if (this.right != null){
            resHeronode = this.right.preOederSearch(no);
        }
        return resHeronode;
    }
    //中序遍历查找
    public HeroNode infixOederSearch(int no){
        HeroNode resHeronode = null;
        if (this.left != null){
            resHeronode = this.left.infixOederSearch(no);
        }
        if (resHeronode != null){
            return resHeronode;
        }
        System.out.println("中序比较次数");
        if (this.no == no){
            return this;
        }
        if (this.right != null){
            resHeronode = this.right.infixOederSearch(no);
        }
        return resHeronode;
    }
    //后序遍历查找
    public HeroNode postOederSearch(int no){
        HeroNode resHeronode = null;
        if (this.left != null){
            resHeronode = this.left.postOederSearch(no);
        }
        if (resHeronode != null){
            return resHeronode;
        }
        if (this.right != null){
            resHeronode = this.right.postOederSearch(no);
        }
        if (resHeronode != null){
            return resHeronode;
        }
        System.out.println("后序比较次数");
        return this.no == no? this: null;
    }

    //删除节点
    public boolean delHeroNode(int no){
        boolean flag = false;
        if (this.left != null && this.left.getNo() == no ){
               this.left = null;
               return true;
        }
        if (this.right != null && this.right.getNo() == no ){
                this.right = null;
                return true;
        }
        if (this.left != null){
            flag = this.left.delHeroNode(no);
        }
        if (this.right != null){
           flag = this.right.delHeroNode(no);
        }
        return flag;
    }
}
class BinaryTree{
    private HeroNode root;

    public BinaryTree() {
    }

    public void setRoot(HeroNode root) {

        this.root = root;
    }

    public BinaryTree(HeroNode root) {

        this.root = root;
    }
    public void preOrder(){
        root.preOrder();
    }
    public void infixOrder(){
        root.infixOrder();
    }
    public void postOrder(){
        root.postfixOrder();
    }
    public HeroNode preOrderSearch(int no){
        return root.preOederSearch(no);
    }

    public HeroNode infixOrderSearch(int no) {
        return root.infixOederSearch(no);
    }
    public HeroNode postOrderSearch(int no) {
        return root.postOederSearch(no);
    }
    public boolean delHeroNode(int no){
        if (root.getNo() == no){
            root = null;
            return true;
        }else {
            return root.delHeroNode(no);
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值