简体   繁体   English

AVL树实现的新功能

[英]New to AVL tree implementation

I am writing a sliding window compression algorithm (LZ77) that searches for phrases in a "moving" dictionary. 我正在编写一种滑动窗口压缩算法(LZ77),该算法在“移动”词典中搜索短语。

So far I have written a BST where each node is stored in an array and it's index in the array is also the value of the starting position in the window itself. 到目前为止,我已经编写了一个BST,其中每个节点都存储在一个数组中,并且它在数组中的索引也是窗口本身中起始位置的值。

I am now looking at transforming the BST to an AVL tree. 我现在正在考虑将BST转换为AVL树。 I am a little confused at the sample implementations I have seen. 我对看到的示例实现有些困惑。 Some only appear to store the balance factors whereas others store the height of each tree. 一些仅显示存储平衡因子,而其他一些存储每棵树的高度。

Are there any performance advantage/disadvantages of storing the height and/or balance factor for each node? 存储每个节点的高度和/或平衡因子是否有任何性能优势/劣势? Apologies if this is a very simple question, but I'm still not visualizing how I want to restructure my BST to implement height balancing. 抱歉,这是一个非常简单的问题,但我仍然无法想象要如何重组BST以实现高度平衡。

Thanks. 谢谢。

The balance is simply the difference between the two heights, so there shouldn't be any significant performance differences, except when the integer subtraction is implemented very slowly. 平衡只是两个高度之间的差异,因此,除非整数减法的执行速度非常慢,否则不应有任何明显的性能差异。 ;) Storing the heights may require more memory if you simply store them as ints, without compressing both into a single int, which you could safely do as the balance guarantees a practical limit to the maximum height. ;)如果仅将高度存储为int而不将它们都压缩为一个int,则存储高度可能需要更多的内存,您可以放心地这样做,因为天平保证了最大高度的实际限制。 But premature optimization, you know... With the heights you have more information available that you would need to calculate with an additional subtree traversal when you only have the balance available, but that depends on your requirements. 但是过早的优化,您知道...随着高度的增加,当您只有可用的天平时,您将需要使用其他子树遍历来计算更多的信息,但这取决于您的要求。

I recommend a deep study of Ben Pfaff's excellent introduction to BSTs: http://www.stanford.edu/~blp/avl/libavl.html/ 我建议深入研究Ben Pfaff对BST的出色介绍: http : //www.stanford.edu/~blp/avl/libavl.html/

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

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