辛星Java树算法教程第零篇:树的表示

经过一段时间的纠结,辛星Java树算法终于开始更新了,本次从最简单的二叉树开始吧,其中也都是一些常见的知识点。
本期的每节的篇幅都不会太长,主要是写起来比较轻盈简单。作为第一节,这里只介绍一下树的表示,这里的树我们可以用递归的方式来表示,即如下:

package com.mengzhidu.teach.algorithm.tree.demo;

/**
 * Created by xinxing on 2019/3/20
 */
public class TreeNode {

    private String value;

    private TreeNode left;

    private TreeNode right;

    public TreeNode(String value, TreeNode left, TreeNode right) {
        this.value = value;
        this.left = left;
        this.right = right;
    }

    public String getValue() {
        return value;
    }

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

    public TreeNode getLeft() {
        return left;
    }

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

    public TreeNode getRight() {
        return right;
    }

    public void setRight(TreeNode right) {
        this.right = right;
    }
}

然后我们再写一个TreeNodeHelper,它可以得到一颗默认的树,并且返回根节点,如下:

package com.mengzhidu.teach.algorithm.tree.demo;

/**
 * Created by xinxing on 2019/3/20
 *               A
 *            /    \
 *           B      C
 *          / \      \
 *         D  E      F
 */
public class TreeNodeHelper {

    public static TreeNode getTreeNode() {
        TreeNode dNode = new TreeNode("D", null, null);
        TreeNode eNode = new TreeNode("E", null, null);
        TreeNode fNode = new TreeNode("F", null, null);
        TreeNode bNode = new TreeNode("B", dNode, eNode);
        TreeNode cNode = new TreeNode("C", null, fNode);
        TreeNode aNode = new TreeNode("A", bNode, cNode);
        return aNode;
    }
}

默认就是这样的啦,以后会更新具体的算法。

你可能感兴趣的:(算法)