Java构造二叉树,遍历,叶子节点,高度,二叉树的左右视图

Java构造二叉树,遍历,叶子节点,高度,二叉树的左右视图

节点node

package Study_1;

public class Node {
    private Object  val;
    Node left;
    public Node right;
    public Node(Object val) {
        this.val = val;
    }
    public Node(Node left, Node node, Node right, Object val) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
    public String toString(){
        return "[ "+val+" ]";
    }
}


构造二叉树方法 遍历,叶子节点,高度,二叉树的左右视图

package Study_1;

import javax.swing.tree.TreeNode;
import java.util.ArrayList;
import java.util.List;

public class BinaryTree {
    private Node root;

    public BinaryTree(Object Data) {
        this.root = new Node(Data);
    }
    //二叉树的右视图
    List<Object> num = new ArrayList<>();
    public List<Object> rightSideView(Node root) {

        dfs(root,0);
        return num;
    }
    public void dfs(Node root,int depth){
        if(root==null){
            return;
        }
        if(depth == num.size()){
            num.add(root.getVal());
        }
        depth++;
        dfs(root.left,depth);
        dfs(root.right,depth);

    }

    //
    public Node add(Node parentNode, Object data, Boolean isLeft) {
        if (parentNode == null) {
            throw new RuntimeException("父节点为空,无法添加子节点");
        }
        if (isLeft && parentNode.left != null) {
            throw new RuntimeException("左节点已存在,无法添加");
        }
        if (!isLeft && parentNode.right != null) {
            throw new RuntimeException("右节点已存在,添加失败");
        }
        Node newNode = new Node(data);
        if (isLeft) {
            parentNode.left = newNode;
        } else {
            parentNode.right = newNode;
        }
        return newNode;
    }
    //先序遍历
    public List<Node> preBinaryTree() {
        return preTrase(root);
    }

    public List<Node> preTrase(Node node) {
        List<Node> list = new ArrayList<>();
        list.add(node);
        if(node.left!=null){
            list.addAll(preTrase(node.left));
        }
        if(node.right!=null){
            list.addAll(preTrase(node.right));
        }
        return list;
    }
    //后序遍历
    public List<Node> last_tree(){
        return last(root);
    }
    public List<Node> last(Node root){
        List<Node> list = new ArrayList<>();
        if(root.left!=null){
            list.addAll(last(root.left));
        }
        if(root.right!=null){
            list.addAll(last(root.right));
        }
        list.add(root);
        return list;
    }
    //中序遍历
    public List<Node> middle(){
        return middleTree(root);
    }
    public List middleTree(Node root){
        List<Node> list = new ArrayList<>();

        if(root.left!=null){
            list.addAll(middleTree(root.left));
        }
        list.add(root);
        if(root.right!=null){
            list.addAll(middleTree(root.right));
        }
        return list;

    }
    public int numLeaves(Node root){
        if(root==null){
            return 0;
        }
        if(root.left==null&&root.right==null)
        {
            System.out.println(root);
            return 1;
        }
        return (root.left==null?0:numLeaves(root.left))+(root.right==null?0:numLeaves(root.right));
    }
    public int height(Node root){

        if(root==null)
            return -1;
        int leftHeight = root.left==null?0:height(root.left);
        int rightHeight = root.right==null?0:height(root.right);
        return Math.max(leftHeight,rightHeight)+1;
    }
    public static void main(String[] args) {
        BinaryTree binaryTree = new BinaryTree("根节点");
        Node t1 = binaryTree.add(binaryTree.root,"左节点",true);
        Node t2 = binaryTree.add(binaryTree.root,"右节点",false);
        Node t12 = binaryTree.add(t1,"左节点的右节点",false);
        Node t21  = binaryTree.add(t2,"右节点的左节点",true);
        Node t3  = binaryTree.add(t21,"右节点的左节点的左节点",true);
       System.out.println(binaryTree.preBinaryTree());
        System.out.println(binaryTree.middle());
        System.out.println(binaryTree.last_tree());
        System.out.println("叶子节点数为:"+binaryTree.numLeaves(binaryTree.root));
        System.out.println("树的高度为:"+binaryTree.height(binaryTree.root));
        System.out.println("树的右视图"+binaryTree.rightSideView(binaryTree.root));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值