二叉搜索树中FindParent,无需在接口上添加方法
温馨提示:
本文最后更新于 2024年04月12日,已超过 37 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我。
I am given an interface to implement methods for binary search tree. The interface is below. I can not add methods to this interface for my implementation. I got other methods working but for the parent method the implementation I have requires to add method on the interface however I can not do so. How to implement parent method without requiring to add another method on the interface. Thank you for your help.
public interface BinarySearchTree<K extends Comparable<K>, V> {
public NonemptyBinarySearchTree<K, V> insert(K key, V value);
public int size();
public V searchForKey(K key);
public K max() throws EmptyTreeException;
public K min() throws EmptyTreeException;
public K parent(K key);
public NonemptyBinarySearchTree<K, V> deleteKey(K targetKey);
}
public K parent(K key) {
if(key == null) {
throw new IllegalArgumentException();
}
return parent(key, null);
}
public K parent(K key,K root)
{
K returnKey = null;
if(key.compareTo(this.key) < 0) {
returnKey = left.parent(key, this.key);
}
else if (key.compareTo(this.key) > 0) {
returnKey = right.parent(key, this.key);
} else {
returnKey = root;
}
return returnKey;
}
正文到此结束
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2350
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权