【问题标题】:How can I return a node with a specific value in a BST?如何在 BST 中返回具有特定值的节点?
【发布时间】:2016-10-11 07:01:22
【问题描述】:

我必须制作一个所谓的“命中平衡树”。不同之处在于,如您所见,我的节点类有一个名为 numberOfHits 的实例变量,每当您调用 contains 方法或 findNode 方法时,它都会递增。这个练习的重点是让命中数最高的节点位于顶部,因此树基本上会重建自身(或旋转)。 Root 显然具有最高的命中数。

我有一个关于我必须创建的方法的问题,该方法返回命中数最高的节点。稍后我将需要它来使树自行旋转(我想,至少这是计划)。这是我的节点类。 (当然是所有的吸气剂)

public class HBTNode<T> {


private HBTNode<T> left;
private HBTNode<T> right;
private T element;
private int numberOfHits;


public HBTNode(T element){
    this.left = null;
    this.right = null;
    this.element = element;
    this.numberOfHits = 0;
}

到目前为止,我所拥有的是:

public int findMaxCount(HBTNode<T> node) {

    int max = node.getNumberOfHits();
    if (node.getLeft() != null) {
        max = Math.max(max, findMaxCount(node.getLeft()));
    }
    if (node.getRight() != null) {
        max = Math.max(max, findMaxCount(node.getRight()));
    }
    return max;
}

这很好用,除了它返回一个整数。我需要返回节点本身。由于我必须递归地执行此操作,我决定找到最大的命中计数,然后在另一个返回节点的方法中使用此方法,就像这样(它可能真的效率低下,所以如果你有改进的提示,我在听) :

public int findMaxCount() {
    return findMaxCount(root);
}


public HBTNode<T> findMaxCountNode(HBTNode<T> node) {

    if (node.getNumberOfHits() == this.findMaxCount()) {
        return node;
    }

    if (node.getLeft() != null ) {
        return findMaxCountNode(node.getLeft());
    }
    if (node.getRight() != null) {
        return findMaxCountNode(node.getRight());
    }

    return null;
}

我这样调用方法:

public HBTNode<T> findMaxCountNode() {
    return findMaxCountNode(root);
}

即使我认为它应该没问题,它也会返回 null,我不擅长递归,所以很明显我错过了一些东西。如果你对我的这个练习有任何帮助,我愿意接受任何帮助,还有新的建议。非常感谢。

测试代码:

public static void main(String[] args) {


    HBTree<Integer> tree = new HBTree<Integer>();

    tree.add(50);
    tree.add(25);
    tree.add(74);
    tree.add(19);
    tree.add(8);
    tree.add(6);
    tree.add(57);
    tree.add(108);


    System.out.println(tree.contains(108)); //contains method increases the count by one
    System.out.println(tree.contains(8));
    System.out.println(tree.contains(8));
    System.out.println(tree.contains(108));
    System.out.println(tree.contains(8));
    System.out.println(tree.contains(108));
    System.out.println(tree.contains(108));
    System.out.println(tree.contains(108));

    System.out.println(tree.findMaxCountNode());

}

当前输出:true true true true true true true true null

预期输出:true true true true true true true true Element: 108 Left child: 6 //this is just a toString, doesn't matter at this point Right child: null Number of hits: 5

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 Minimal, complete, verifiable example 适用于此。在您发布代码并准确描述问题之前,我们无法有效地帮助您。具体来说,出现问题的测试用例在哪里,也许还有一些跟踪输出?我们应该看到调试尝试的输出,即使只有几个策略性放置的 print 语句。
  • 感谢您的耐心等待,我将对其进行编辑并发布输出。

标签: java recursion binary-search-tree


【解决方案1】:

看起来你的两个函数应该如下所示。我在这里假设的是,这些在 HBTNode 类中定义的函数旨在找到最高命中计数的节点低于本身:

public HBTNode<T> findMaxCountNode(HBTNode<T> node) {
    return findMaxCountNode(node, node);
}

public HBTNode<T> findMaxCountNode(HBTNode<T> node, HBTNode<T> maxNode) {

    HBTNode<T> currMax = (node.getNumberOfHits() > maxNode.getNumberOfHits()) ? node : maxNode;

    if (node.getLeft() != null ) {
       currMax = findMaxCountNode(node.getLeft(), currMax);
    }

    if (node.getRight() != null) {
        currMax = findMaxCountNode(node.getRight(), currMax);
    }

    return currMax;
}

public int findMaxCount(HBTNode<T> node) {
    HBTNode<T> maxNode = findMaxCountNode(node);
    if (maxNode != NULL)
        return maxNode.getNumberOfHits();
    else
        return -1;
}

如果有任何问题,请告诉我,这不是我的想法,但我认为指出您的方法的“整数”版本应该只使用“节点查找”版本会很有帮助方法。你写的求最大value的方法和我写的求最大node的方法很相似。

【讨论】:

  • 是的,它有效。我的一个朋友实际上有相同的解决方案,除了方法的第一行是 if(....) 。出于某种原因,我完全没有想到同样的概念。我仍然想知道为什么我的解决方案如此错误。关键是找到命中数最高的节点是的,所以我们知道哪个节点被搜索得最频繁。现在我必须根据最高命中数节点以某种方式旋转树,它应该自动成为根节点,不过感谢您的回复。
  • @d3nls 如果您不想做繁重的工作,请随时提出另一个 SO 问题 :) 不过,尝试弄清楚这是一个很好的学习体验。
  • 是的,我理解它为什么工作以及它是如何工作的,我的问题在于递归思维和产生更复杂的递归算法,而迭代并不让我头疼。不过感谢您的帮助!
猜你喜欢
  • 2022-01-08
  • 2023-03-12
  • 1970-01-01
  • 2013-04-22
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 2021-12-02
  • 2017-09-07
相关资源
最近更新 更多