简体   繁体   English

二叉树-删除

[英]Binary trees - deletion

I basically need a way of guaranteed O(log n) deletion. 我基本上需要一种保证O(log n)删除的方法。 Can this be done with a binary tree, or is it always worst case O(n)? 可以用二叉树来完成,还是总是最坏的情况O(n)?

What if I balance the tree everytime? 如果我每次都平衡树怎么办?

please help 请帮忙

You need a balanced binary tree for the guarantee to work. 您需要一个平衡的二叉树来保证工作。 Red Black Trees are an example of the balanced tree structure and the implementation is not too hard. 红黑树是平衡树结构的一个示例,实现起来并不难。

Red Black Trees (wiki) 红黑树(wiki)

And here is a nice lecture for that .. 这是一个很好的演讲 ..

If you are looking for a balanced binary tree, you could use the "heap" 如果您正在寻找平衡的二叉树,则可以使用“堆”

http://en.wikipedia.org/wiki/Heap_(data_structure ) http://en.wikipedia.org/wiki/Heap_(data_structure

You need a Binary Search Tree 您需要一个二叉搜索树

As the wiki page above said: 正如上面的Wiki页面所说:

thus in the worst case it requires time proportional to the height of the tree 因此在最坏的情况下,它需要的时间与树的高度成比例

which means if you can make it always balanced, you can get O(logN) for deletion 这意味着如果您可以使其始终保持平衡,则可以获取O(logN)进行删除

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM