二叉树学习笔记三 代码实现

package com.hao.firstdemo.datastruct.tree;

import lombok.Data;
import lombok.Setter;

/**
 * @author haoxiansheng
 * @data 2020/5/2
 */
public class BinaryTreeTest {
    public static void main(String[] args) {
        //创建二叉树
        BinaryTree binaryTree = new BinaryTree();
        //创建需要的节点
        HeroNode node1 = new HeroNode(1, "宋江");
        HeroNode node2 = new HeroNode(2, "吴用");
        HeroNode node3 = new HeroNode(3, "鲁骏逸");
        HeroNode node4 = new HeroNode(4, "林冲");

        //手动创建
        binaryTree.setRoot(node1);
        node1.setLeft(node2);
        node1.setRight(node3);
        node3.setRight(node4);
//        System.out.println("前序遍历 =》1 2 3 4");
//        binaryTree.preOrder();
//        System.out.println("前序遍历 =》2 1 3 4");
//        binaryTree.infixOrder();
//        System.out.println("后序遍历 =》 2 4 3 1");
//        binaryTree.postOrder();

        //前序遍历搜索测试
        //遍历比较次数分析 这个要看节点所在位置
//        System.out.println("=======前序遍历搜索========");
//        int no = 1;
//        HeroNode resNode = binaryTree.preOrderSearch(no); //前序遍历
//        HeroNode resNode = binaryTree.infixOrderSearch(no); //中序遍历
//        HeroNode resNode = binaryTree.postOrderSearch(no);  //后序遍历
//        if (resNode != null) {
//            System.out.printf("节点找到了 信息: no=%d, name=%s", resNode.getNo(), resNode.getName());
//        } else {
//            System.out.printf("没有找到no=%d的节点", no);
//        }

        //测试删除
        System.out.println("删除前 =》前序遍历");
        binaryTree.preOrder();
        binaryTree.delNode(4);
        System.out.println("删除后 =》前序遍历");
        binaryTree.preOrder();
    }
}

//二叉树创建

class BinaryTree {
    @Setter
    private HeroNode root;

    public void delNode(int no) {
        if (root != null) {
            //判断当前节点是不是要删除的节点
            if (root.getNo() == no) {
                root = null;
            } else {
                root.delNode(no); //递归删除
            }
        } else {
            System.out.println("这是一颗空树");
        }
    }

    //前序遍历
    public void preOrder() {
        if (this.root != null) {
            this.root.preOrder();
        } else {
            System.out.println("二叉树为null");
        }
    }

    //中序遍历
    public void infixOrder() {
        if (this.root != null) {
            this.root.infixOrder();
        } else {
            System.out.println("二叉树为null");
        }
    }

    //后序遍历
    public void postOrder() {
        if (this.root != null) {
            this.root.postOrder();
        } else {
            System.out.println("二叉树为null");
        }
    }

    //前序遍历搜索
    public HeroNode preOrderSearch(int no) {
        if (root != null) {
          return   this.root.preOrderSearch(no);
        } else {
            return null;
        }
    }

    //中序遍历搜索
    public HeroNode infixOrderSearch(int no) {
        if (root != null) {
            return   this.root.infixOrderSearch(no);
        } else {
            return null;
        }
    }

    //后序遍历搜索
    public HeroNode postOrderSearch(int no) {
        if (root != null) {
            return   this.root.postOrderSearch(no);
        } else {
            return null;
        }
    }
}

//先创建HeroNode 节点
@Data  //get set 方法
class HeroNode {
    private int no;

    private String name;

    private HeroNode left;   //默认为null

    private HeroNode right;  //默认为null

    public HeroNode(int no, String name) {
        this.no = no;
        this.name = name;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

    /**
     * 递归删除节点 这个只是一个开始思路并不完全对 可以先当作一个简单理解
     * 1、如果删除的节点是叶子节点,则删除该节点
     * 2、如果删除节点是非叶子节点,则删除该子树
     */
    public void delNode(int no) {

        // 当前节点的左子节点不为null,并且就是要删除的节点,就讲this.left = null 并返回
        if (this.left != null &&  this.left.no == no) {
            this.left = null;
            return;
        }
        // 当前节点的右子节点不为null,并且就是要删除的节点,就讲this.right = null 并返回
        if (this.right != null &&  this.right.no == no) {
            this.right = null;
            return;
        }
        // 向左子树递归删除
        if (this.left != null) {
            this.left.delNode(no);
        }
        // 向右子树递归删除
        if (this.right != null) {
            this.right.delNode(no);
        }
    }

    /**
     * 编写前序遍历的方法
     */
    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);
    }

    /**
     * 前序遍历查找
     * @param no
     * @return 如果存在就返回HeroNode 否则返回null
     */
    public HeroNode preOrderSearch(int no) {
        System.out.println("比较次数");
        if (this.no == no) {
            return this;
        }
        HeroNode resNode = null;
        if (this.left != null) { //判断当前节点是否为null, 如果没有找到返回null
            resNode = this.left.preOrderSearch(no);
        }
        if (resNode != null) { //说明找到了
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preOrderSearch(no);
        }
        return resNode;
    }

    /**
     * 中序遍历查找
     * @param no
     * @return
     */
    public HeroNode infixOrderSearch(int no) {
        HeroNode 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;
        }
        if (this.right != null) {
            resNode = this.right.infixOrderSearch(no);
        }
        return resNode;
    }

    /**
     * 后序遍历
     * @param no
     * @return
     */
    public HeroNode postOrderSearch(int no) {
        HeroNode resNode = null;
        //左子树找
        if (this.left != null) {
            resNode = this.left.postOrderSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        //右子树找
        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;
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值