简体   繁体   English

BST中节点的深度,包括重复项

[英]Depth of a Node in BST including duplicates

i have implemented a function to find the depth of a node in a binary search tree but my implementation does not take care of duplicates. 我已经实现了在二进制搜索树中查找节点深度的功能,但是我的实现并未处理重复项。 I have my code below and would like some suggestions on how to consider duplicates case in this function. 我在下面有我的代码,并希望就如何在此函数中考虑重复情况提出一些建议。 WOuld really appreciate your help. 非常感谢您的帮助。

public int depth(Node n) {
    int result=0;
    if(n == null || n == getRoot())
        return 0;

    return (result = depth(getRoot(), n, result));
}
public int depth(Node temp, Node n, int result) {
    int cmp = n.getData().compareTo(temp.getData());

    if(cmp == 0) {
        int x = result;
        return x;
    }
    else if(cmp < 0) {
            return depth(temp.getLeftChild(), n, ++result);
        }
        else {
            return depth(temp.getRightChild(), n, ++result);
        }                   
}

Well, if there's duplicates, then the depth of a node with a given value doesn't make any sense on its own, because there may be multiple nodes with that value, hence multiple depths. 好吧,如果有重复项,那么具有给定值的节点的深度本身就没有任何意义,因为可能有多个具有该值的节点,因此具有多个深度。

You have to decide what it means, which could be (not necessarily an exhaustive list): 必须决定它的含义,可能是(不一定是详尽的列表):

  • the depth of the deepest node with that value. 具有该值的最深节点的深度。
  • the depth of the shallowest node with that value. 具有该值的最浅节点的深度。
  • the depth of the first node found with that value. 使用该值找到的第一个节点的深度。
  • the average depth of all nodes with that value. 具有该值的所有节点的平均深度。
  • the range (min/max) of depths of all nodes with that value. 具有该值的所有节点的深度范围(最小/最大)。
  • a list of depths of all nodes with that value. 具有该值的所有节点的深度的列表。
  • an error code indicating your query made little sense. 表示您的查询的错误代码毫无意义。

Any of those could make sense in specific circumstances. 在特定情况下,其中任何一个都有意义。


Of course, if n is an actual pointer to a node, you shouldn't be comparing values of nodes at all, you should be comparing pointers. 当然,如果n是指向节点的实际指针,则根本不应该比较节点的值 ,而应该比较指针。 That way, you will only ever find one match and the depth of it makes sense. 这样一来,您将永远找不到一场比赛,而且比赛的深度很有意义。

Something like the following pseudo-code should do: 像下面的伪代码这样的事情应该做:

def getDepth (Node needle, Node haystack, int value):
    // Gone beyond leaf, it's not in tree

    if haystack == NULL: return -1

    // Pointers equal, you've found it.

    if needle == haystack: return value

    // Data not equal search either left or right subtree.

    if needle.data < haystack.data:
        return getDepth (needle, haystack.left, value + 1)

    if needle.data > haystack.data:
        return getDepth (needle, haystack.right, value + 1)

    // Data equal, need to search BOTH subtrees.

    tryDepth = getDepth (needle, haystack.left, value + 1)
    if trydepth == -1:
        tryDepth = getDepth (needle, haystack.right, value + 1)

    return trydepth

The reason why you have to search both subtrees when the values are equal is because the desired node may be in either subtree. 当值相等时必须搜索两个子树的原因是因为所需的节点可能在两个子树中。 Where the values are unequal, you know which subtree it's in. So, for the case where they're equal, you check one subtree and, if not found, you check the other. 如果值不相等,则知道它在哪个子树中。因此,对于相等的情况,请检查一个子树,如果找不到,则检查另一个子树。

In the code you show, there is no way to prefer one node with same value over another. 在您显示的代码中,没有办法偏爱具有相同值的一个节点而不是另一个。 You need to have some criteria for differentiation. 您需要具有一些区分标准。 You can retrieve the list of all duplicate nodes depths using the following approach, for example: 您可以使用以下方法来检索所有重复节点深度的列表,例如:

  1. Find the depth of your node. 找到您节点的深度。
  2. Find depth of the same node for the left subtree emerging from the found node - stop if not found. 为从找到的节点出现的左子树查找同一节点的深度-如果找不到,则停止。
  3. Add depth of the previously found node (in 1) to the depth of the duplicate 将先前找到的节点(在1中)的深度添加到重复项的深度中
  4. Find depth of the same node for the right subtree emerging from the found node (in 1) - stop if not found. 为从找到的节点中出现的右子树查找同一节点的深度(在1中)-如果找不到,则停止。
  5. Add depth of the previously found node (in 1) to the depth of the duplicate 将先前找到的节点(在1中)的深度添加到重复项的深度中
  6. Repeat for left and right subtrees. 对左右子树重复上述步骤。

Also see here: What's the case for duplications in BST? 另请参阅此处: BST中重复出现什么情况?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM