我需要帮助将遍历树从Java转换为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)
{
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();
}
}

免责声明:我已经研究过如何转换代码,但没有一个能说明如何转换。所以请帮忙。

这个代码的答案在这里,所以如果你们中的任何人有同样的问题,那么就来这里复制这个代码!我没有写这个代码——实际上是一个叫@wrnger的人。谢谢你和我分享你的代码!

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
}

最新更新