Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中不使用add方法的二叉树_Java_Oop_Binary Tree - Fatal编程技术网

Java中不使用add方法的二叉树

Java中不使用add方法的二叉树,java,oop,binary-tree,Java,Oop,Binary Tree,我一直在尝试从Node切换到Java,我想知道的一件事是如何在不使用排序算法的情况下构造二叉树。在Node中,我可以简单地键入以下内容: function TreeNode(val) { this.val = val; this.left = this.right = null; } let tree = new TreeNode(4); tree.left = new TreeNode(2); tree.left.left = new TreeNode(1); Java与此等

我一直在尝试从Node切换到Java,我想知道的一件事是如何在不使用排序算法的情况下构造二叉树。在Node中,我可以简单地键入以下内容:

function TreeNode(val) {
    this.val = val;
    this.left = this.right = null;
}
let tree = new TreeNode(4);
tree.left = new TreeNode(2);
tree.left.left = new TreeNode(1);
Java与此等价的是什么?这是我目前的思考过程

public class BinaryTree {
    private static TreeNode root;
    public static void main(String[] args) {
        BinaryTree bt = new BinaryTree();
        bt.TreeNode = ??

    }

  public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode(int x) { val = x; }
  }

}

您可以这样构造代码

class Node {
    int data;
    Node left, right;

    public Node(int data) {
        super();
        this.data = data;
        this.left = this.right = null;
    }

}

public class BinaryTree {
    Node root;

    public int height(Node node) {
        if (node == null)
            return 0;
        return Math.max(height(node.left), height(node.right)) + 1;
    }

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree();
        tree.root = new Node(1);
        tree.root.left = new Node(2);
        tree.root.right = new Node(3);
        tree.root.left.right = new Node(4);

        System.out.println("The height of given binary tree is : " + tree.height(tree.root));
    }

}

@EJP:不需要两个类。每个树节点都是一棵树

OP:你能给我看一节课吗

基于代码


可能的副本不需要两个类。每个树节点都是一棵树。但是仍然有两个类而不是一个。看起来可行。我不确定如何通过赋值删除根节点(节点1)并使根节点成为其他节点(例如4)<代码>二进制树根=。。。;根=新根我们这里有一个开放的数据结构,其中操作由抽象边界之外的代码执行。使用这种API,不需要明确区分根节点。事实上,每个节点都是根节点。。。子树的。我觉得不错。取决于要求,选择什么样的设计。这适用于所有的数据结构。这可能是我第一次看到有一个类的二叉树。我读到的每一个地方,pople都倾向于有一个单独的节点类。
public class BinaryTree {
    int data;
    BinaryTree left, right;

    public BinaryTree(int data) {
        super();
        this.data = data;
        this.left = this.right = null; // redundant ...
    }

    public int height() {
        height(this);
    }

    private int height(BinaryTree node) {
        if (node == null) {
            return 0;
        }
        return Math.max(height(node.left), height(node.right)) + 1;
    }

    public static void main(String[] args) {
        BinaryTree tree = new BinaryTree(1);
        tree.left = new BinaryTree(2);
        tree.right = new BinaryTree(3);
        tree.left.right = new BinaryTree(4);

        System.out.println("The height of given binary tree is : " + tree.height());
    }

}