Java 如何实现BinarySearchTree的delete方法?
这是我的密码。我被删除部分卡住了。insert和get方法都很好。我想保存旧的树,这样我可以很快将节点链接到一起Java 如何实现BinarySearchTree的delete方法?,java,binary-search-tree,Java,Binary Search Tree,这是我的密码。我被删除部分卡住了。insert和get方法都很好。我想保存旧的树,这样我可以很快将节点链接到一起 private BinaryTree<K, V> left, right; public BinaryTree(K key, V value) { // Leaf node constructor this.key = key; this.value = value; } public V put(K key, V value) { if
private BinaryTree<K, V> left, right;
public BinaryTree(K key, V value) {
// Leaf node constructor
this.key = key;
this.value = value;
}
public V put(K key, V value) {
if (this.key.equals(key)) {
// Replacing the value at the current node
V ret = this.value;
this.value = value;
return ret;
}
int comp = this.key.compareTo(key);
if (comp < 0) {
// this.key < key
// Put the key we are inserting to the right
// of the current key
if (right == null) {
// We have to create a node to the right
// (Leaf node)
right = new BinaryTree<K, V>(key, value);
return null; // Per interface
}
return right.put(key, value);
} else {
// We don't support keys where equals and compareTo
// disagree with each other.
assert(comp != 0);
// At this point, we know that comp > 0
// Therefore, this.key > key
// Put the key we are inserting to the left of current
if (left == null) {
// We have to create a node to the left
// (Leaf node)
left = new BinaryTree<K, V>(key, value);
return null; // Per interface
}
return left.put(key, value);
// Exercise: You could write a tiny little private method
// to implement this redundant code in one place.
}
}
public V get(K key) {
int comp = this.key.compareTo(key);
if (comp < 0) {
// this.key < key
// Recurse to the right
if (right == null) {
// Not in the tree
return null;
}
return right.get(key);
} else if (comp > 0) {
// this.key > key
// Recurse to the left
if (left == null) {
// Not in the tree
return null;
}
return left.get(key);
} else {
assert(this.key.equals(key));
return value;
}
}
public boolean containsKey(K key) {
// Note: Doesn't work with null values!
return get(key) != null;
}
public BinaryTree<K, V> delete(K key) {
BinaryTree<K,V> tmp,childL,childR,OldTree;
int comp=this.key.compareTo(key);
if(comp<0){
//this.key<Key goes right
if(this.right==null){
throw new UnsupportedOperationException("Nothing you can delete here");
}
else{
return this.right.delete(key);
}
}
if(comp>0){
if(this.left==null){
throw new UnsupportedOperationException("Nothing you can delte here");
}
else{
return this.left.delete(key);
}
}
if(this.key.equals(key)){
}
{
}
//}
// IMPLEMENT THIS FOR YOUR ASSIGNMENT!
// Remove key from the tree, if it exists.
// Throw UnsupportedOperationException if key isn't in the tree.
// Return the new root node if we did delete the key.
// (The new root node may be the same as the old one.)
// If you deleted the last node in the tree, it will return null.
return null;
}}
私有二叉树左、右;
公共二叉树(K键,V值){
//叶节点构造函数
this.key=key;
这个值=值;
}
公共V输入(K键,V值){
如果(此键等于(键)){
//替换当前节点上的值
V ret=该值;
这个值=值;
返回ret;
}
int comp=此.key.compareTo(key);
if(comp<0){
//this.key0
//因此,这个.key>key
//把我们要插入的钥匙放在电流的左边
if(left==null){
//我们必须在左边创建一个节点
//(叶节点)
左=新的二进制树(键,值);
返回null;//每个接口
}
返回left.put(键、值);
//练习:你可以编写一个小小的私有方法
//在一个地方实现这个冗余代码。
}
}
公共V get(K键){
int comp=此.key.compareTo(key);
if(comp<0){
//this.key0){
//这个.key>key
//向左递归
if(left==null){
//不是在树上
返回null;
}
向左返回。获取(键);
}否则{
断言(this.key.equals(key));
返回值;
}
}
公共布尔containsKey(K键){
//注意:不适用于空值!
return get(key)!=null;
}
公共二进制树删除(K键){
二叉树tmp、childL、childR、OldTree;
int comp=此.key.compareTo(key);
如果(CopWe不在这里做你的家庭作业),我就完成了大部分,只是停留在这上面。欢迎到堆栈溢出。看起来你可能需要家庭作业帮助。虽然我们没有问题本身,请观察这些,并相应地编辑你的问题。(即使这不是作业,无论如何请考虑这个建议。)删除是二叉树中最重要的一步,这是您的家庭作业:)您必须保持树的有效性,因此当您找到要删除的节点时,有三种情况:叶(并删除它)、仅一个子节点(用子节点替换节点)、两个子节点(在最小的节点中找到最大的节点-向左走一步,然后一直向右走,直到你能做到这一点-并将其复制到删除的节点的位置,然后在找到的节点上递归地应用删除操作,直到达到前两种情况之一为止)。详细信息:你可以做到:)