java - 如何从二叉搜索树中删除字符串
问题描述
我正在尝试从 BST 中删除一个字符串,但我无法弄清楚。我知道如何删除整数,但无法将我的代码转换为删除具有字符串的节点。这是我目前拥有的代码
/*
* Removes the specified string from the BST
*/
public boolean remove(String s){
Node ans = remove(root, s);
if(ans == null){
return false;
} else {
return true;
}
}
private static Node remove(Node root, String s) {
if (root == null)
return null;
if (s.compareTo(root.data) < 0) {
root.left = remove(root.left, s);
} else if (s.compareTo(root.data) > 0) {
root.right = remove(root.right, s);
} else {
if (root.left == null) {
return root.right;
} else if (root.right == null)
return root.left;
root.data = minimum(root.right);
root.right = remove(root.right, root.data);
}
return root;
}
public static String minimum(Node root) {
String minimum = root.data;
while (root.left != null) {
minimum = root.left.data;
root = root.left;
}
return minimum;
}
解决方案
将所有 "Integer" 替换为 "String" 。
推荐阅读
- linux - 为什么 Post Build 操作不会覆盖 Jenkins 中的构建状态?
- sql-server - SQL 简单舍入
- javascript - Nodemailer:没有定义收件人,如何解决?
- spring - Spring Boot Html to Image(Html 渲染)
- android - 在 Android 上模糊图像中的区域(在 Java 中)
- c# - 使用 AWS 开发工具包从 Amazon S3 下载对象 - 结果文件已损坏
- virtual-machine - 如何将文件从本地 win 10 操作系统移动到 VMware ESXi 上的虚拟机?
- java - 按钮悬停和按下效果 CSS Javafx
- java - 在android中根据设备区域禁用逗号或点
- javascript - 离线模式处于活动状态时 react-native-firebase 是否安全?