Javascript 我需要帮助将遍历树从Java转换为JS(答案在描述中,不要否决这篇文章,否则它会变成私有的。)

Javascript 我需要帮助将遍历树从Java转换为JS(答案在描述中,不要否决这篇文章,否则它会变成私有的。),javascript,java,node.js,Javascript,Java,Node.js,这是我需要帮助将Java转换为JavaScript的代码。这里的代码是一个用Java编写的遍历二叉树,我所需要的只是帮助将其从Java转换为JavaScript。 不同树遍历的Java程序 /* Class containing left and right child of current node and key value*/ class Node { int key; Node left, right; public Node(int item)

这是我需要帮助将Java转换为JavaScript的代码。这里的代码是一个用Java编写的遍历二叉树,我所需要的只是帮助将其从Java转换为JavaScript。
不同树遍历的Java程序

/* Class containing left and right child of current
   node and key value*/
class Node {
    int key;
    Node left, right;
 
    public Node(int item)
    {
        key = item;
        left = right = null;
    }
}
 
class BinaryTree {
    // Root of Binary Tree
    Node root;
 
    BinaryTree() { root = null; }
 
    /* Given a binary tree, print its nodes according to the
      "bottom-up" postorder traversal. */
    void printPostorder(Node node)
    {
        if (node == null)
            return;
 
        // first recur on left subtree
        printPostorder(node.left);
 
        // then recur on right subtree
        printPostorder(node.right);
 
        // now deal with the node
        System.out.print(node.key + " ");
    }
 
    /* Given a binary tree, print its nodes in inorder*/
    void printInorder(Node node)
    {
        if (node == null)
            return;
 
        /* first recur on left child */
        printInorder(node.left);
 
        /* then print the data of node */
        System.out.print(node.key + " ");
 
        /* now recur on right child */
        printInorder(node.right);
    }
 
    /* Given a binary tree, print its nodes in preorder*/
    void printPreorder(Node node)
    {
        if (node == null)
            return;
 
        /* first print data of node */
        System.out.print(node.key + " ");
 
        /* then recur on left sutree */
        printPreorder(node.left);
 
        /* now recur on right subtree */
        printPreorder(node.right);
    }
 
    // Wrappers over above recursive functions
    void printPostorder() { printPostorder(root); }
    void printInorder() { printInorder(root); }
    void printPreorder() { printPreorder(root); }
 
    // Driver method
    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.left = new Node(4);
        tree.root.left.right = new Node(5);
 
        System.out.println(
            "Preorder traversal of binary tree is ");
        tree.printPreorder();
 
        System.out.println(
            "\nInorder traversal of binary tree is ");
        tree.printInorder();
 
        System.out.println(
            "\nPostorder traversal of binary tree is ");
        tree.printPostorder();
    }
}

Disclamer:我研究过如何转换代码,但没有一个演示过如何转换代码。因此,请帮助。

此代码的答案在这里,因此,如果你们中的任何人有相同的问题,请过来复制此代码!我没有写这段代码,实际上是一个叫@wrangler的人写的。谢谢你和我分享你的代码

var displayTree = tree => console.log(JSON.stringify(tree, null, 2));
function Node(value) {
  this.value = value;
  this.left = null;
  this.right = null;
}
function BinarySearchTree() {
  this.root = null;
  // Only change code below this line
  this.inor=[];
  this.inorder = function(node=this.root){
    if(!this.root) return null;
    if(node==this.root) this.inor=[];
    if(!node) return this.inor;
    
    this.inorder(node.left);
    this.inor.push(node.value);
    this.inorder(node.right);

    return this.inor;
  }

  this.preor=[];
  this.preorder = function(node=this.root){
    if(!this.root) return null;
    if(node==this.root) this.preor=[];
    if(!node) return this.preor;
    
    this.preor.push(node.value);
    this.preorder(node.left);
    this.preorder(node.right);

    return this.preor;
  }

  this.postor=[];
  this.postorder = function(node=this.root){
    if(!this.root) return null;
    if(node==this.root) this.postor=[];
    if(!node) return this.postor;
  
    this.postorder(node.left);
    this.postorder(node.right);
    this.postor.push(node.value);
    
    return this.postor;
  }
  // Only change code above this line
}

因为您要求将JAVA代码转换为javascript解决方案,而不是检查JAVA解决方案?你可以在JS中查看我的一个DFS解决方案。希望有帮助!谢谢@wrangler,它确实有帮助。