二叉树

2020-10-13  本文已影响0人  乙腾

二叉树概念

image.png
image.png

notice:

左边连续,即从左往右没有空缺的节点。

遍历

前序遍历: 先输出父节点,在遍历左子树和右子树。
中序遍历:先遍历左子树,在输出父节点,在输出右子树。
后序遍历:先遍历左子树,在遍历右子树,最后输出父节点。

遍历口诀:

前序
先根节点 左顺 右顺
中序
左逆 根节点 右顺
后续
左逆 右逆 根节点
code
BinarySortTreeNode

package com.pl.arithmetic.binarySortTree;

/**
 * <p>
 *
 * @Description: TODO
 * </p>
 * @ClassName BinarySortTreeNode
 * @Author pl
 * @Date 2020/10/13
 * @Version V1.0.0
 */
public class BinarySortTreeNode {
    private int value;
    private String name;
    BinarySortTreeNode leftNode;
    BinarySortTreeNode rightNode;

    /**
     *前序排序
     * 
     * @param 
     * @return void
     * @exception 
     * @author silenter
     * @date 2020/10/13 7:04
    */
    public void preOrder(){
        System.out.println(this.value);
        if (this.leftNode!=null){
            this.leftNode.preOrder();
        }
        if (this.rightNode!=null){
            this.rightNode.preOrder();
        }
    }

    /**
     *中序排序
     * 
     * @param 
     * @return void
     * @exception 
     * @author silenter
     * @date 2020/10/13 7:04
    */
    public void infixOrder(){
        if (this.leftNode!=null){
            this.leftNode.infixOrder();
        }
        System.out.println(this.value);
        if (this.rightNode!=null){
            this.rightNode.infixOrder();
        }
    }

    /**
     *后续排序
     *
     * @param
     * @return void
     * @exception
     * @author silenter
     * @date 2020/10/13 7:05
    */
    public void postOrder(){
        if (this.leftNode!=null){
            this.leftNode.postOrder();
        }
        if (this.rightNode!=null){
            this.rightNode.postOrder();
        }
        System.out.println(this.value);
    }

    public BinarySortTreeNode(int value, String name) {
        this.value = value;
        this.name = name;
    }


    public BinarySortTreeNode(int value) {
        this.value = value;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public BinarySortTreeNode getLeftNode() {
        return leftNode;
    }

    public void setLeftNode(BinarySortTreeNode leftNode) {
        this.leftNode = leftNode;
    }

    public BinarySortTreeNode getRightNode() {
        return rightNode;
    }

    public void setRightNode(BinarySortTreeNode rightNode) {
        this.rightNode = rightNode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

BinarySortTree

package com.pl.arithmetic.binarySortTree;

/**
 * <p>
 *
 * @Description: TODO
 * </p>
 * @ClassName BinarySortTree
 * @Author pl
 * @Date 2020/10/13
 * @Version V1.0.0
 */
public class BinarySortTree {
    private BinarySortTreeNode root;


    public void setRoot(BinarySortTreeNode root) {
        this.root = root;
    }

    public void preOrder(){
        if(verifyRootNode()){
            this.root.preOrder();
        }
    }

    public void infixOrder(){
        if(verifyRootNode()){
            this.root.preOrder();
        }
    }

    public void postOrder(){
        if(verifyRootNode()){
            this.root.preOrder();
        }
    }


    public boolean verifyRootNode(){
        if (this.root!=null){
            return true;
        }
        System.out.println("root为空");
        return false;
    }
}
上一篇 下一篇

猜你喜欢

热点阅读