栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java数据结构《二叉树》的手动生成、前序中序后序遍历和查找以及删除节点

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java数据结构《二叉树》的手动生成、前序中序后序遍历和查找以及删除节点

package tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        Heronode root = new Heronode(1,"宋江");
        Heronode node1 = new Heronode(2,"吴用");
        Heronode node2 = new Heronode(3,"卢俊义");
        Heronode node3 = new Heronode(4,"武松");
        Heronode node4 = new Heronode(5,"鲁智深");
        //手动生成树
        root.left = node1;
        root.right = node2;
        node2.left = node3;
        node2.right = node4;
        BinaryTree binaryTree = new BinaryTree();
        binaryTree.setRoot(root);
        System.out.println("前序遍历~~~~");
        binaryTree.preList();
//        System.out.println("中序遍历~~~~");
//        binaryTree.infixList();
//        System.out.println("后序遍历~~~~");
//        binaryTree.postList();
//        Heronode res1 = binaryTree.preSearch(4);
//        if (res1 == null) {
//            System.out.println("前序没找到");
//        }else {
//            System.out.println("前序找到的结果为:" + res1);
//        }
//        Heronode res2 = binaryTree.infixSearch(3);
//        if (res2 == null) {
//            System.out.println("中序序没找到");
//        }else {
//            System.out.println("中序找到的结果为:" + res2);
//        }
//        Heronode res3 = binaryTree.postSearch(10);
//        if (res3 == null) {
//            System.out.println("后序没找到");
//        }else {
//            System.out.println("后序找到的结果为:" + res3);
//        }
        binaryTree.delete(10);
        binaryTree.preList();




    }
}
class BinaryTree{
    private Heronode root;

    public void setRoot(Heronode root) {
        this.root = root;
    }
    //前序遍历
    public void preList(){
        if (root == null) {
            System.out.println("该树为空");
            return;
        }
        root.preOrderList();
    }
    //中序遍历
    public void infixList(){
        if (root == null) {
            System.out.println("该树为空");
            return;
        }
        root.infixOrderList();
    }
    //后序遍历
    public void postList(){
        if (root == null) {
            System.out.println("该树为空");
            return;
        }
        root.postOrderList();
    }
    //前序查找
    public Heronode preSearch(int no){
        if (root == null) {
            return null;
        }
        return root.preSearch(no);
    }
    //中序查找
    public Heronode infixSearch(int no){
        if (root == null) {
            return null;
        }
        return root.infixSearch(no);
    }
    //后序查找
    public Heronode postSearch(int no){
        if (root == null) {
            return null;
        }
        return root.postSearch(no);
    }
    //删除
    public void delete(int no){
        if (root == null) {
            System.out.println("该树为空,删除失败");
            return;
        }
        if (root.getNo() == no){
            root = null;
            System.out.println("删除成功");
        }else {
            root.delete(no);
        }
    }
}
class HeroNode{
    private int no;
    private String name;
    Heronode left;
    Heronode right;

    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;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + ''' +
                '}';
    }
    //前序遍历
    public void preOrderList(){
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrderList();
        }
        if (this.right != null) {
            this.right.preOrderList();
        }
    }
    //中序遍历
    public void infixOrderList(){
        if (this.left != null) {
            this.left.infixOrderList();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrderList();
        }
    }
    //后序遍历
    public void postOrderList(){
        if (this.left != null) {
            this.left.postOrderList();
        }
        if (this.right != null) {
            this.right.postOrderList();
        }
        System.out.println(this);
    }
    //前序查找
    public Heronode preSearch(int no){
        Heronode resNode = null;
        if (this.no == no){
            return this;
        }
        if (this.left != null) {
            resNode = this.left.preSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.preSearch(no);
        }
        return resNode;
    }
    //中序查找
    public Heronode infixSearch(int no){
        Heronode resNode = null;
        if (this.left != null) {
            resNode = this.left.infixSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no){
            return this;
        }
        if (this.right != null) {
            resNode = this.right.infixSearch(no);
        }
        return resNode;
    }
    //后序查找
    public Heronode postSearch(int no){
        Heronode resNode = null;
        if (this.left != null) {
            resNode = this.left.postSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.right != null) {
            resNode = this.right.postSearch(no);
        }
        if (resNode != null) {
            return resNode;
        }
        if (this.no == no){
            return this;
        }
        return null;
    }
    //删除
    public void delete(int no){
        if (this.left != null && this.left.no == no) {
            this.left = null;
            System.out.println("删除成功");
            return;
        }
        if (this.right != null && this.right.no == no){
            this.right = null;
            System.out.println("删除成功");
            return;
        }
        if (this.left != null) {
            this.left.delete(no);
        }
        if (this.right != null){
            this.right.delete(no);
        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/712452.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号