Java 二叉搜索树递归-我需要使用setLeft和setright吗?

Java 二叉搜索树递归-我需要使用setLeft和setright吗?,java,recursion,binary-search-tree,Java,Recursion,Binary Search Tree,到目前为止我所做的一切。如果root不是null,我需要它调用私有递归方法。在这个新方法中,它需要递归地调用BSTNode的getL或getR。您可以尝试这样插入: public class BSTNode { Profile data = null; BSTNode l = null; BSTNode r = null; public BSTNode(Profile data) { super(); this.data = d

到目前为止我所做的一切。如果root不是null,我需要它调用私有递归方法。在这个新方法中,它需要递归地调用BSTNode的getL或getR。

您可以尝试这样插入:

public class BSTNode {

    Profile data = null;
    BSTNode l = null;
    BSTNode r = null;

    public BSTNode(Profile data) {
        super();
        this.data = data;
    }

    public Profile getData() {  //not sure
        return data;
    }

    public void setData(Profile data) { //not sure
        this.data = data;
    }

    public Profile getProfile() {
        return data;

    }

    public BSTNode getL() {
        return l;
    }

    public void setL(BSTNode l) {
        this.l = l;
    }

    public BSTNode getR() {
        return r;
    }

    public void setR(BSTNode r) {
        this.r = r;
    }

    @Override
    public String toString() {
        return "BSTNode [data=" + data + ", l=" + l + ", r=" + r + "]";
    }   
}


public class BST {

    BSTNode root = null;

    public BST() {

    }   
    public void insertProfile(Profile p) {
        BSTNode node = new BSTNode(p);

        if (root == null) {
            root = node;
            return;
        }
    }
}
class Node {
  Node left;
  Node right;
  Object data;

  public void insert(Object data) {
    // decide whether it goes to the left or to the right. 
    // assume the left:
    if (left != null) {
      left.insert(data);
    } else {
      left = new Node(data);
    }
  }
}

所以,如果我理解正确,你有递归插入,在根上只有一个异常。因此,除了根以外的所有内容都将由insert处理。因此,如果这是您的问题,请在(递归)insert中使用setLeft和setRight。好的,只是为了澄清一下,如果没有先使用setLeft/setRight,我无法使用getLeft/getRight?如果您以前没有构建任何树节点,getLeft/Right将返回null,因为它是空的。但是,如果在我的节点类中已经有setLeft、setRight、getLeft和getRight方法,并且我需要在二元搜索树的递归私有方法中使用它们,那么我将如何执行此操作呢?
setLeft()
setRight()
的参数是什么?显示代码plzpublic void setL(bstnodel){this.l=l;}public BSTNode getL(){return l;}我指的是问题帖子
Node root;

class Node {
    Item data;
    Node left;
    Node right;

    public Node(Item e) {
        data = e;
    }
}

public void insert(Item item) {
    root = insert(root, item);
}

private Node insert(Node node, Item item) {
    if (node== null) {
        return new Node(item);
    } else if (item.compareTo(node.data) == 0) {
        return node;
    } else if (item.compareTo(node.data) < 0) {
        node.right = insert(node.right, item);
        return node;
    } else {
        node.left = insert(node.left, item);
        return node;
    }
}
public class BST {

    public static void main(String[] args) {
        BinarySearchTree tree = new BinarySearchTree();

        // Tree building...
        tree.insert(new Profile("50"));
        tree.insert(new Profile("70"));
        tree.insert(new Profile("80"));
        tree.insert(new Profile("60"));
        tree.insert(new Profile("30"));
        tree.insert(new Profile("40"));
        tree.insert(new Profile("20"));

        System.out.println("\nSearch the node with data 60: " + tree.find(new Profile("60")));
        System.out.println("Search the node with data 65: " + tree.find(new Profile("65")));
        System.out.println("Search the node with data 20: " + tree.find(new Profile("20")));
        System.out.println("Search the node with data 25: " + tree.find(new Profile("25")));
    }

    static class BinarySearchTree {

        private BSTNode root;

        public void insert(Profile item) {
            root = insert(root, item);
        }

        private BSTNode insert(BSTNode node, Profile item) {
           if (node == null) {
                return new BSTNode(item);
            } else if (item.id.compareTo(node.data.id) == 0) {
                return node;
            } else if (item.id.compareTo(node.data.id) < 0) {
                node.setR(insert(node.r, item));
                return node;
            } else {
                node.setL(insert(node.l, item));
                return node;
            }
        }

        public Profile find(Profile target) {
            return find(root, target);
        }

        private Profile find(BSTNode node, Profile target) {
            if (node == null) {
                return null;
            }
            int cmd = target.id.compareTo(node.data.id);
            if (cmd == 0) {
                return node.data;
            } else if (cmd < 0) {
                return find(node.getR(), target);
            } else {
                return find(node.getL(), target);
            }
        }

    }

    static class Profile {
        String id;

        public Profile(String id) {
            this.id = id;
        }

        @Override
        public int hashCode() {
            return id.hashCode();
        }

        @Override
        public boolean equals(Object obj) {
            return this.id.equals(((Profile) obj).id);
        }

        @Override
        public String toString() {
            return id;
        }
    }

    static class BSTNode {
        Profile data = null;
        BSTNode l = null;
        BSTNode r = null;

        public BSTNode(Profile data) {
            super();
            this.data = data;
        }

        public Profile getData() { // not sure
            return data;
        }

        public void setData(Profile data) { // not sure
            this.data = data;
        }

        public Profile getProfile() {
            return data;

        }

        public BSTNode getL() {
            return l;
        }

        public void setL(BSTNode l) {
            this.l = l;
        }

        public BSTNode getR() {
            return r;
        }

        public void setR(BSTNode r) {
            this.r = r;
        }

        @Override
        public String toString() {
            return "BSTNode [data=" + data + ", l=" + l + ", r=" + r + "]";
        }
    }
}
Search the node with data 60: 60
Search the node with data 65: null
Search the node with data 20: 20
Search the node with data 25: null