作业:Java二进制搜索树。编辑特定节点

作业:Java二进制搜索树。编辑特定节点,java,binary-search-tree,Java,Binary Search Tree,我需要一个二进制搜索树的帮助。以下是节点和BST类: public class Node { private int key; private Node parent; private Node leftChild; private Node rightChild; public Node(int key, Node leftChild, Node rightChild) { this.setKey(key); this.

我需要一个二进制搜索树的帮助。以下是节点和BST类:

public class Node {
    private int key;
    private Node parent;
    private Node leftChild;
    private Node rightChild;

    public Node(int key, Node leftChild, Node rightChild) {
        this.setKey(key);
        this.setLeftChild(leftChild);
        this.setRightChild(rightChild);
    }

    public void setKey(int key) {
        this.key = key;
    }

    public int getKey() {
        return key;
    }

    public void setParent(Node parent) {
        this.parent = parent;
    }

    public Node getParent() {
        return parent;
    }

    public void setLeftChild(Node leftChild) {
        this.leftChild = leftChild;
    }

    public Node getLeftChild() {
        return leftChild;
    }

    public void setRightChild(Node rightChild) {
        this.rightChild = rightChild;
    }

    public Node getRightChild() {
        return rightChild;
    }
}

public class BinarySearchTree {

    private Node root;

    public void insert(int key) {
        insert(new Node(key, null, null));
    }

    public void insert(Node z) {

        Node y = null;
        Node x = root;

        while (x != null) {
            y = x;

            if (z.getKey() < x.getKey()) {
                x = x.getLeftChild();
            } else {
                x = x.getRightChild();
            }
        }

        z.setParent(y);

        if (y == null) {
            root = z;
        } else if (z.getKey() < y.getKey()) {
            y.setLeftChild(z);
        } else {
            y.setRightChild(z);
        }
    }

    public void preorderTraversal() {
        preorderTraversal(root);
    }

    public void preorderTraversal(Node node) {
        if (node != null) {
            System.out.print(node.getKey() + " ");
            preorderTraversal(node.getLeftChild());
            preorderTraversal(node.getRightChild());            
        }
    }

    public void inorderTraversal() {
        inorderTraversal(root);
    }

    private void inorderTraversal(Node node) {
        if (node != null) {
            inorderTraversal(node.getLeftChild());
            System.out.print(node.getKey() + " ");
            inorderTraversal(node.getRightChild());
        }
    }

    public void postorderTraversal() {
        postorderTraversal(root);
    }

    private void postorderTraversal(Node node) {
        if (node != null) {
            postorderTraversal(node.getLeftChild());
            postorderTraversal(node.getRightChild());
            System.out.print(node.getKey() + " ");
        }
    }
}
公共类节点{
私钥;
私有节点父节点;
私有节点leftChild;
私有节点rightChild;
公共节点(int键、节点leftChild、节点rightChild){
这个.setKey(key);
this.setLeftChild(leftChild);
这个.setRightChild(rightChild);
}
公共无效设置键(int键){
this.key=key;
}
public int getKey(){
返回键;
}
公共void setParent(节点父节点){
this.parent=parent;
}
公共节点getParent(){
返回父母;
}
公共void setLeftChild(节点leftChild){
this.leftChild=leftChild;
}
公共节点getLeftChild(){
返回leftChild;
}
公共无效setRightChild(节点rightChild){
this.rightChild=rightChild;
}
公共节点getRightChild(){
还权子;
}
}
公共类二进制搜索树{
私有节点根;
公共无效插入(int键){
插入(新节点(键,空,空));
}
公共空心插入(节点z){
节点y=null;
节点x=根;
while(x!=null){
y=x;
if(z.getKey()
(摘自)这是一个相对简单的实现。但是,我需要在每个节点中存储额外的信息。该节点必须包含选举的候选人以及候选人的投票数。关于这项任务(以及为什么必须使用BST)有很多抱怨,但请不要介入其中

我将我的候选人编号为1-20,并将其作为二叉搜索树中的键。我的问题是,使用这段代码(或稍加修改的版本),如何更新给定密钥的特定节点信息

如果该人投票给候选人4),我如何更新候选人4的投票信息

我见过一些find方法,但我不知道应该在哪个节点上调用它


任何帮助都将不胜感激

您只需向
节点
类添加另外两个属性,即可存储所需的额外信息。因此,节点类看起来有点像这样:

public class Node {

  private int key;
  private Node parent;
  private Node leftChild;
  private Node rightChild;

  public string candidateName;
  public int votes;

  // .. other properties and methods
}
Node node = root;

while (node.key != 4) {
  // implement your tree traversal algorithm here
}

// when you reach here, node is the candidate you were searching
// for, or null if not found

if (node != null) {
  node.votes++;
}
当您获得候选人4的投票时,遍历树以找到键为4的节点,然后执行
Node.voces++

在伪代码中,它将如下所示:

public class Node {

  private int key;
  private Node parent;
  private Node leftChild;
  private Node rightChild;

  public string candidateName;
  public int votes;

  // .. other properties and methods
}
Node node = root;

while (node.key != 4) {
  // implement your tree traversal algorithm here
}

// when you reach here, node is the candidate you were searching
// for, or null if not found

if (node != null) {
  node.votes++;
}

确保在节点构造函数中将
投票
初始化为0。

非常感谢,您的方法非常完美。一个简单的问题,根在java中有特殊的意义吗?不,这里的根是指树的根节点。