java手写二叉树,前序、中序、后序遍历

本文介绍如何使用Java手动创建二叉树并实现前序、中序和后序遍历的方法。
摘要由CSDN通过智能技术生成

java手写二叉树,前序、中序、后序遍历

/**
 * Created by 码上风云 on 2020/10/7.
 */
class Node<T> {
    public Node left, right;
    public T data;

    //创建空节点
    public Node() {
        data = null;
        left = null;
        right = null;
    }
    //创建一个二叉树结点
    public Node(T x) {
        this.data = x;
        left = null;
        right = null;
    }

}

public class Tree<T> {
    public Node<T> root;

    public Tree(Node root) {
        this.root = root;
    }

    //创建一颗空二叉树
    public Tree() {
        this.root = new Node<T>();
    }

    //生产一颗二叉树
    public Tree(T x) {
        this.root = new Node<T>(x);
    }
   //插入左节点
    public boolean insertLeft(T x, Node<T> parent) {
        if (parent == null) {
            return false;
        }
        Node<T> p = new Node<T>(x);
        if (parent.left == null) {
            parent.left = p;
    
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值