Java算法 数据结构 leetcodePython

数据结构之二叉树

2019-06-15  本文已影响2人  smallmartial

二叉树

1.为什么需要树这种数据结构

2.树示意图

![1560596464787.png](https://img.haomeiwen.com/i7149586/771c0432ec6a3699.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

3.二叉树的概念

![1560601642234.png](https://img.haomeiwen.com/i7149586/015059e224c77f99.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

3.1二叉树遍历

3.2代码实现

package cn.smallmartial.tree;

/**
 * @Author smallmartial
 * @Date 2019/6/15
 * @Email smallmarital@qq.com
 */
public class BinaryTreeDemo {
    public static void main(String[] args) {
        //创建一个二叉树
        BinaryTree binaryTree = new BinaryTree();
        HeroNode root = new HeroNode(1, "doodou");
        HeroNode heroNode2 = new HeroNode(2, "smallmartial");
        HeroNode heroNode3 = new HeroNode(3, "张三");
        HeroNode heroNode4 = new HeroNode(4, "李四");

        //先手动创建二叉树
        root.setLeft(heroNode2);
        root.setRiht(heroNode3);
        heroNode3.setRiht(heroNode4);
        binaryTree.setRoot(root);
        System.out.println("前序遍历");
        binaryTree.preOrder();

        System.out.println("中序遍历");
        binaryTree.infixOrder();
        System.out.println("后续序遍历");
        binaryTree.postOrder();
    }
}
//创建二叉树
class BinaryTree{

    private HeroNode root;

    public void setRoot(HeroNode root){
        this.root = root;
    }
    //前序遍历
    public void preOrder(){
        if (this.root != null){
            this.root.proOrder();
        }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("二叉树为空,无法遍历");
        }
    }
}

class HeroNode{
    private int no;
    private String name;
    private HeroNode left;
    private 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;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRiht() {
        return right;
    }

    public void setRiht(HeroNode riht) {
        this.right = riht;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", left=" + left +
                ", riht=" + right +
                '}';
    }
    //前序遍历
    public void proOrder(){
        System.out.println(this);
        //递归向左子树前序遍历
        if (this.left != null){
            this.left.proOrder();
        }
        //递归向右子树前序遍历
        if (this.right != null){
            this.right.proOrder();
        }
        //中序遍历
    }

    //中序遍历
    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);
    }

}

运行结果

3.3二叉树-查找指定节点

3.4二叉树的删除

1560604802227.png
上一篇 下一篇

猜你喜欢

热点阅读