ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

9-1二叉树

2021-11-23 21:35:02  阅读:112  来源: 互联网

标签:leftNode HeroNode heroNode rightNode 二叉树 null public


二叉树

基本介绍

1621436705114

二叉树遍历

1621436723810

图解

1621436769026

代码实现

package com.company.tree;

import com.sun.source.tree.IfTree;
import com.sun.tools.javac.Main;

/**
 * @Function :
 * date 2021/5/19 - 23:06
 * How :
 */
public class BinaryTree {
    public static void main(String[] args) {
        HeroNode zhangsan = new HeroNode(1, "zhangsan");
        HeroNode zhangsan1 = new HeroNode(2, "zhangsan1");
        HeroNode zhangsan2 = new HeroNode(3, "zhangsan2");
        HeroNode zhangsan3 = new HeroNode(4, "zhangsan3");
        HeroNode zhangsan4 = new HeroNode(5, "zhangsan4");
        zhangsan.leftNode = zhangsan1;
        zhangsan.rightNode = zhangsan2;
        zhangsan2.leftNode = zhangsan3;
        zhangsan2.rightNode = zhangsan4;
        binaryTree1 binaryTree = new binaryTree1(zhangsan);
        binaryTree.preOrder();

    }
}
class binaryTree1{
    private HeroNode root;

    public binaryTree1(HeroNode root) {
        this.root = root;
    }

    public void preOrder(){
        if (root!=null){
            root.preOrder();
        }
    }

}
//创建hero节点
class HeroNode{
    public int id;
    public String name;
    public HeroNode leftNode;
    public HeroNode rightNode;

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍历
    public void preOrder(){
        System.out.println(this.toString());
        if (this.leftNode!=null){
            this.leftNode.preOrder();
        }
        if (this.rightNode!=null){
            this.rightNode.preOrder();
        }

    }
    //中序遍历
    public void inOrder(){

        if (this.leftNode!=null){
            this.leftNode.inOrder();
        }
        System.out.println(this.toString());
        if (this.rightNode!=null){
            this.rightNode.inOrder();
        }

    }
    //后序遍历
    public void postOrder(){
        System.out.println(this.toString());
        if (this.leftNode!=null){
            this.leftNode.postOrder();
        }
        if (this.rightNode!=null){
            this.rightNode.postOrder();
        }

    }

}

二叉树查找

1621588821004

1621589051693

代码实现

package com.company.tree;

import com.sun.source.tree.IfTree;
import com.sun.tools.javac.Main;

/**
 * @Function :
 * date 2021/5/19 - 23:06
 * How :
 */
public class BinaryTree {
    public static void main(String[] args) {
        HeroNode zhangsan = new HeroNode(1, "zhangsan");
        HeroNode zhangsan1 = new HeroNode(2, "zhangsan1");
        HeroNode zhangsan2 = new HeroNode(3, "zhangsan2");
        HeroNode zhangsan3 = new HeroNode(4, "zhangsan3");
        HeroNode zhangsan4 = new HeroNode(5, "zhangsan4");
        zhangsan.leftNode = zhangsan1;
        zhangsan.rightNode = zhangsan2;
        zhangsan2.leftNode = zhangsan3;
        zhangsan2.rightNode = zhangsan4;
        binaryTree1 binaryTree = new binaryTree1(zhangsan);
        binaryTree.preOrder();

        HeroNode searchId = binaryTree.preOrderSearch(3);
        System.out.println(searchId);


    }
}
class binaryTree1{
    private HeroNode root;

    public binaryTree1(HeroNode root) {
        this.root = root;
    }

    public void preOrder(){
        if (root!=null){
            root.preOrder();
        }
    }

    public HeroNode preOrderSearch(int no){
        if (root!=null){
            return root.preOrderSearch(no);
        }
        throw new RuntimeException("root节点为空");
    }


}
//创建hero节点
class HeroNode{
    public int id;
    public String name;
    public HeroNode leftNode;
    public HeroNode rightNode;

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍历
    public void preOrder(){
        System.out.println(this.toString());
        if (this.leftNode!=null){
            this.leftNode.preOrder();
        }
        if (this.rightNode!=null){
            this.rightNode.preOrder();
        }

    }
    //中序遍历
    public void inOrder(){

        if (this.leftNode!=null){
            this.leftNode.inOrder();
        }
        System.out.println(this.toString());
        if (this.rightNode!=null){
            this.rightNode.inOrder();
        }

    }
    //后序遍历
    public void postOrder(){
        System.out.println(this.toString());
        if (this.leftNode!=null){
            this.leftNode.postOrder();
        }
        if (this.rightNode!=null){
            this.rightNode.postOrder();
        }

    }

    //前序查找
    public HeroNode preOrderSearch(int no){
        HeroNode heroNode = null;

        if (this.id == no){
            System.out.println("--找到--");
            heroNode = this;
            return this;
        }

        if (this.leftNode!=null){
            heroNode = this.leftNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        if(this.rightNode!=null){
            heroNode = this.rightNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        return heroNode;
    }

    //中序查找
    public HeroNode inOrderSearch(int no){
        HeroNode heroNode = null;

        if (this.leftNode!=null){
            heroNode = this.leftNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        if (this.id == no){
            System.out.println("--找到--");
            heroNode = this;
            return this;
        }
        if(this.rightNode!=null){
            heroNode = this.rightNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        return heroNode;
    }

    //后查找
    public HeroNode postOrderSearch(int no){
        HeroNode heroNode = null;

        if (this.leftNode!=null){
            heroNode = this.leftNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        if(this.rightNode!=null){
            heroNode = this.rightNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        if (this.id == no){
            System.out.println("--找到--");
            heroNode = this;
            return this;
        }
        return heroNode;
    }
}

二叉树删除

1621591082993

代码实现

package com.company.tree;

import com.sun.source.tree.IfTree;
import com.sun.tools.javac.Main;

/**
 * @Function :
 * date 2021/5/19 - 23:06
 * How :
 */
public class BinaryTree {
    public static void main(String[] args) {
        HeroNode zhangsan = new HeroNode(1, "zhangsan");
        HeroNode zhangsan1 = new HeroNode(2, "zhangsan1");
        HeroNode zhangsan2 = new HeroNode(3, "zhangsan2");
        HeroNode zhangsan3 = new HeroNode(4, "zhangsan3");
        HeroNode zhangsan4 = new HeroNode(5, "zhangsan4");
        zhangsan.leftNode = zhangsan1;
        zhangsan.rightNode = zhangsan2;
        zhangsan2.leftNode = zhangsan3;
        zhangsan2.rightNode = zhangsan4;
        binaryTree1 binaryTree = new binaryTree1(zhangsan);
        binaryTree.preOrder();

        HeroNode searchId = binaryTree.preOrderSearch(3);
        System.out.println("查找节点的信息为"+searchId);

        binaryTree.delNOde(3);
        binaryTree.preOrder();


    }
}
class binaryTree1{
    private HeroNode root;

    public binaryTree1(HeroNode root) {
        this.root = root;
    }

    public void preOrder(){
        if (root!=null){
            root.preOrder();
        }
    }

    public HeroNode preOrderSearch(int no){
        if (root!=null){
            return root.preOrderSearch(no);
        }
        throw new RuntimeException("root节点为空");
    }

    public void delNOde(int id){
        if (root!=null){
            if (root.id==id){
                root=null;
            }
            root.delNode(id);
        }
    }


}
//创建hero节点
class HeroNode{
    public int id;
    public String name;
    public HeroNode leftNode;
    public HeroNode rightNode;

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

    @Override
    public String toString() {
        return "HeroNode{" +
                "id=" + id +
                ", name='" + name + '\'' +
                '}';
    }
    //前序遍历
    public void preOrder(){
        System.out.println(this.toString());
        if (this.leftNode!=null){
            this.leftNode.preOrder();
        }
        if (this.rightNode!=null){
            this.rightNode.preOrder();
        }

    }
    //中序遍历
    public void inOrder(){

        if (this.leftNode!=null){
            this.leftNode.inOrder();
        }
        System.out.println(this.toString());
        if (this.rightNode!=null){
            this.rightNode.inOrder();
        }

    }
    //后序遍历
    public void postOrder(){
        System.out.println(this.toString());
        if (this.leftNode!=null){
            this.leftNode.postOrder();
        }
        if (this.rightNode!=null){
            this.rightNode.postOrder();
        }

    }

    //前序查找
    public HeroNode preOrderSearch(int no){
        HeroNode heroNode = null;

        if (this.id == no){
            System.out.println("--找到--");
            heroNode = this;
            return this;
        }

        if (this.leftNode!=null){
            heroNode = this.leftNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        if(this.rightNode!=null){
            heroNode = this.rightNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        return heroNode;
    }

    //中序查找
    public HeroNode inOrderSearch(int no){
        HeroNode heroNode = null;

        if (this.leftNode!=null){
            heroNode = this.leftNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        if (this.id == no){
            System.out.println("--找到--");
            heroNode = this;
            return this;
        }
        if(this.rightNode!=null){
            heroNode = this.rightNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        return heroNode;
    }

    //后查找
    public HeroNode postOrderSearch(int no){
        HeroNode heroNode = null;

        if (this.leftNode!=null){
            heroNode = this.leftNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        if(this.rightNode!=null){
            heroNode = this.rightNode.preOrderSearch(no);
            if (heroNode!=null){
                return heroNode;
            }
        }
        if (this.id == no){
            System.out.println("--找到--");
            heroNode = this;
            return this;
        }
        return heroNode;
    }

    //删除节点
    public void delNode(int id){
        if (this.leftNode!=null && this.leftNode.id==id){
            this.leftNode=null;
            return;
        }
        if (this.rightNode!=null && this.rightNode.id==id){
            this.rightNode=null;
            return;
        }
        if (this.leftNode!=null){
            this.leftNode.delNode(id);
        }
        if (this.rightNode!=null){
            this.rightNode.delNode(id);
        }
    }
}

标签:leftNode,HeroNode,heroNode,rightNode,二叉树,null,public
来源: https://www.cnblogs.com/jsxz/p/15595392.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有