Redian新闻
>
感觉avl tree的插入不是O(lgn)啊
avatar
感觉avl tree的插入不是O(lgn)啊# JobHunting - 待字闺中
l*a
1
复习data structure时,发现插入后做rebalance需要height。网上能找到的getHeight
()函数的时间复杂度是O(n)的。插入不可能做到O(lgn)。
public int getHeight ()
{
if (this.leftChild == null && this.rightChild == null)
{
return 0;
}
else
{
int leftH = 0;
int rightH = 0;
if (this.leftChild != null)
{
leftH++;
leftH += this.getLeftChild().getHeight();
}
if (this.rightChild != null)
{
rightH++;
rightH += this.getRightChild().getHeight();
}
return Math.max(leftH,rightH);
}
}
avatar
a*m
2
高度可以存在结点上。
avatar
e*r
3
getHeight复杂度是O(lgN)还有可以插入的时候就顺便更新了
相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。