问题描述
我正在尝试将堆实现为二叉树。我需要在 O(log n) 时间内执行插入和删除功能。它只需要删除最低的、最右边的节点。这是我到目前为止的 remove 函数(它是 O(n) 运行时):
public Node removeLast() {
Node k = findLastForDeletion(this.root,1,findHeightforDeletion());
if (k.parent != null && k.parent.left == k) {
k.parent.left = null;
}
else if (k.parent != null) {
k.parent.right = null;
}
return k;
}
public Node findLastForDeletion(Node n,int current,int height) {
if (current == height && n != null) {
return n;
}
else if (n == null) {
return n;
}
else {
Node k = findLastForDeletion(n.right,current + 1,height);
if (k != null) {
return k;
}
else {
return findLastForDeletion(n.left,height);
}
}
}
public int findHeightforDeletion() {
Node l = this.root;
int lCount = 0;
while (l != null) {
l = l.left;
lCount ++;
}
return lCount;
}
我的add函数和这个过程是一样的。我只需要帮助弄清楚如何在删除之前获取最后一个节点。感谢您的帮助!
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)