[英]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): 您必须决定它的含义,可能是(不一定是详尽的列表):
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:
您可以使用以下方法来检索所有重复节点深度的列表,例如:
Also see here: What's the case for duplications in BST? 另请参阅此处: BST中重复出现什么情况?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.