Java:查找BST中未找到的节点的深度
我有一个二进制搜索树,它存储在字符串中,我已经能够找到存在于BST中的字符串的深度或高度,但我试图做的是找到不在BST中的字符串的深度 下面是我目前使用的算法,它可以完美地找到BST中已经存在的字符串的深度:Java:查找BST中未找到的节点的深度,java,binary-search-tree,depth,Java,Binary Search Tree,Depth,我有一个二进制搜索树,它存储在字符串中,我已经能够找到存在于BST中的字符串的深度或高度,但我试图做的是找到不在BST中的字符串的深度 下面是我目前使用的算法,它可以完美地找到BST中已经存在的字符串的深度: public int getDepth(String data) { return getDepth(root, data, 1); } public int getDepth(Node root, String data, int level) { if (root =
public int getDepth(String data) {
return getDepth(root, data, 1);
}
public int getDepth(Node root, String data, int level) {
if (root == null)
return 0;
if (root.getData().getName().equals(data))
return level;
int ret = getDepth(root.leftChild, data, level + 1);
if (ret != 0)
return ret;
ret = getDepth(root.rightChild, data, level + 1);
return ret;
}
我的BST包含按以下顺序插入的元素:“猫”、“鸟”、“狗”、“老虎”、“大象”、“熊猫”
因此,二叉树应该如下所示:
cat
/ \
bird dog
\
tiger
/ \
elephant panda
当我调用“dog”上的方法时,输出是:dog位于深度2
当我调用“hippo”上的方法时,输出是:hippo位于深度0
但是“河马”的预期输出应该是:河马在深度5
因为河马将是大象的孩子,大象已经深入浅出了
那么对于另一个例子,如果我想找到BST中没有的“cow”的深度呢?它必须是dog的左子对象,所以深度应该是3,但是,我仍然得到0
因此,问题是在不使用删除和/或恢复方法的情况下,我需要什么案例或什么案例我是否需要修改上述方法以确定不在BST内的字符串的深度?您需要模拟一个
插入操作,该操作完成实际插入所能完成的所有操作,但它不会将新节点附加到树中并跟踪深度。
搜索插入新值的正确位置-从根开始,将值与当前节点进行比较,根据比较结果向下搜索树。
找到正确的位置后,不要向树中添加值,只返回深度。您需要模拟一个插入操作,该操作完成真正插入所能完成的所有操作,但它不会将新节点附加到树中并跟踪深度。
搜索插入新值的正确位置-从根开始,将值与当前节点进行比较,根据比较结果向下搜索树。
找到正确的位置后,不要向树中添加值,只返回深度。您需要模拟一个插入操作,该操作完成真正插入所能完成的所有操作,但它不会将新节点附加到树中并跟踪深度。
搜索插入新值的正确位置-从根开始,将值与当前节点进行比较,根据比较结果向下搜索树。
找到正确的位置后,不要向树中添加值,只返回深度。您需要模拟一个插入操作,该操作完成真正插入所能完成的所有操作,但它不会将新节点附加到树中并跟踪深度。
搜索插入新值的正确位置-从根开始,将值与当前节点进行比较,根据比较结果向下搜索树。
找到正确的位置后,不要向树中添加值,只返回深度。尝试第二个getDepth()方法的此修改版本:
public int getDepth(Node root, String data) {
if (root == null || root.getData().getName().equals(data)) {
// you hit the spot - either you are below a leaf
// or you matched the node
// if you were doing insert/replace, you would put the data on this spot
return 0;
}
if (isSmallerThan(data, root.getData().getName())) {
return 1 + getDepth(root.leftChild, data);
}
return 1 + getDepth(root.rightChild, data);
}
其中isSmallerThan(字符串a,字符串b)
返回true
如果a
小于b
。此方法取决于您希望如何比较字符串
而且您不需要一直通过级别
。尝试第二个getDepth()
方法的修改版本:
public int getDepth(Node root, String data) {
if (root == null || root.getData().getName().equals(data)) {
// you hit the spot - either you are below a leaf
// or you matched the node
// if you were doing insert/replace, you would put the data on this spot
return 0;
}
if (isSmallerThan(data, root.getData().getName())) {
return 1 + getDepth(root.leftChild, data);
}
return 1 + getDepth(root.rightChild, data);
}
其中isSmallerThan(字符串a,字符串b)
返回true
如果a
小于b
。此方法取决于您希望如何比较字符串
而且您不需要一直通过级别
。尝试第二个getDepth()
方法的修改版本:
public int getDepth(Node root, String data) {
if (root == null || root.getData().getName().equals(data)) {
// you hit the spot - either you are below a leaf
// or you matched the node
// if you were doing insert/replace, you would put the data on this spot
return 0;
}
if (isSmallerThan(data, root.getData().getName())) {
return 1 + getDepth(root.leftChild, data);
}
return 1 + getDepth(root.rightChild, data);
}
其中isSmallerThan(字符串a,字符串b)
返回true
如果a
小于b
。此方法取决于您希望如何比较字符串
而且您不需要一直通过级别
。尝试第二个getDepth()
方法的修改版本:
public int getDepth(Node root, String data) {
if (root == null || root.getData().getName().equals(data)) {
// you hit the spot - either you are below a leaf
// or you matched the node
// if you were doing insert/replace, you would put the data on this spot
return 0;
}
if (isSmallerThan(data, root.getData().getName())) {
return 1 + getDepth(root.leftChild, data);
}
return 1 + getDepth(root.rightChild, data);
}
其中isSmallerThan(字符串a,字符串b)
返回true
如果a
小于b
。此方法取决于您希望如何比较字符串
您不需要一直通过级别
。我已经尝试过的好主意,但我仍然得到相同的结果,因为我猜我返回的结果都是错误的,这就是混淆的地方。@zoltack429 Jan给出了一个很好的实现示例,将它与您自己的进行比较,看看我已经尝试过的创意在逻辑上的差异在哪里,但我仍然得到了相同的结果,因为我猜我返回的内容都是错误的,这就是产生混淆的原因。@zoltack429 Jan给出了一个很好的实现示例,将它与您自己的进行比较,看看我已经尝试过的创意在逻辑上的差异在哪里,但我仍然得到了相同的结果,因为我猜我返回的内容都是错误的,这就是产生混淆的原因。@zoltack429 Jan给出了一个很好的实现示例,将它与您自己的进行比较,看看我已经尝试过的创意在逻辑上的差异在哪里,但我仍然得到了相同的结果,因为我猜我返回的内容都是错误的,这就是产生混淆的原因。@zoltack429 Jan给出了一个很好的实现示例,把它和你自己的比较一下,看看逻辑上的差异在哪里,我喜欢它!谢谢你的帮助!BST对我来说是新的,所以我仍在努力找出它们。我喜欢它!谢谢你的帮助!BST对我来说是新的,所以我仍在努力找出它们。我喜欢它!谢谢你的帮助!BST对我来说是新的,所以我仍在努力找出它们。我喜欢它!谢谢你的帮助!学士学位