Node.js 将二叉树中的新节点插入树中的特定节点

Node.js 将二叉树中的新节点插入树中的特定节点,node.js,binary-search-tree,add,treenode,Node.js,Binary Search Tree,Add,Treenode,我需要创建一个函数: 公共静态二进制树getFamilyTree(字符串路径) 字符串路径是计算机内部的一个位置 我使用scanner和file reader读取这些字符串,并通过拆分(,)like,this={“like”,“this”}将它们添加到字符串数组中,并将这些字符串作为节点数据 public static BinaryTree getFamilyTree(String path) { File fr = new File(path); BinaryTree bt =

我需要创建一个函数: 公共静态二进制树getFamilyTree(字符串路径) 字符串路径是计算机内部的一个位置

我使用scanner和file reader读取这些字符串,并通过拆分(,)like,this={“like”,“this”}将它们添加到字符串数组中,并将这些字符串作为节点数据

public static BinaryTree getFamilyTree(String path) {
    File fr = new File(path);
    BinaryTree bt = new BinaryTree();
    try {
        Scanner sc =new Scanner(fr);
        while(sc.hasNext())
        {
            String st = sc.nextLine();
            String [] carry = st.split(",");
            BTNode [] he = new BTNode [3];
            for (int j = 0; j < carry.length; j++) {
                he[j] = new BTNode(carry[j]);
            }
            LinkedList role = new LinkedList();
            role.AddFirst(carry[0]);
            if(bt.getRoot()==null)
                bt.setRoot(he[0]);
            if(contains(role, bt)){
                if(SearchB(bt.getRoot(), he[0].getData()) != null) {
                    if(carry.length == 3) {
                        if (bt.getRoot().getLeft() == null) 
                        {
                            bt.getRoot().setLeft(new BTNode(carry[2]));
                            bt.getRoot().getLeft().setParent(bt.getRoot());}
                        if (bt.getRoot().getRight() == null)
                        {
                            bt.getRoot().setRight(new BTNode(carry[2]));
                            bt.getRoot().getRight().setParent(bt.getRoot());}
                    }
                    else if(carry.length == 2) 
                    {
                         if(bt.getRoot().getLeft() == null) {
                            bt.getRoot().setLeft(new BTNode(carry[1]));
                            bt.getRoot().getLeft().setParent(bt.getRoot());}
                    }

                }
            }
        }} catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return bt;
    }
我使用这段代码在BinaryTree中搜索树中的特定数据-我要查找的数据是父名称,因此我可以根据文件添加他的孩子


非常抱歉问了这么长的问题:)

什么是
root
?如果要定义变量,必须使用
var
let
const
,例如:
const rootStr=root.getData()。如果您想声明一个具有所需类型的变量,那么您可以执行
constrootstr:string=root.getData()。但无论如何,我认为你应该试着重写你的问题,这是不可理解的。给我们更多的你的逻辑和你的代码。我编辑以上感谢你的提醒,我尽了我最大的努力解释所有我想做的
public static BTNode SearchB(BTNode root, String data) 
{ 
    if (null == root || null == data) return root;
    String rootStr = root.getData();
    if (rootStr.compareToIgnoreCase(data) == 0) return root;
    BTNode left = SearchB(root.getLeft(), data);
    if(null != left) return left;
    BTNode right = SearchB(root.getRight(), data);
    if (null != right) return right;
    return null;
}