Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is a red-black tree my ideal data structure?

I have a collection of items (big rationals) that I'll be processing. In each case, processing will consist of removing the smallest item in the collection, doing some work, and then adding 0-2 new items (which will always be larger than the removed item). The collection will be initialised with one item, and work will continue until it is empty. I'm not sure what size the collection is likely to reach, but I'd expect in the range 1M-100M items. I will not need to locate any item other than the smallest.

I'm currently planning to use a red-black tree, possibly tweaked to keep a pointer to the smallest item. However I've never used one before, and I'm unsure whether my pattern of use fits its characteristics well.

1) Is there a danger the pattern of deletion from the left + random insertion will affect performance, eg by requiring a significantly higher number of rotations than random deletion would? Or will delete and insert operations still be O(log n) with this pattern of use?

2) Would some other data structure give me better performance, either because of the deletion pattern or taking advantage of the fact I only ever need to find the smallest item?

Update: glad I asked, the binary heap is clearly a better solution for this case, and as promised turned out to be very easy to implement.

Hugo

like image 657
Hugo van der Sanden Avatar asked Mar 24 '10 14:03

Hugo van der Sanden


People also ask

Is red-black tree a data structure?

Properties of Red-Black tree It is a self-balancing Binary Search tree. Here, self-balancing means that it balances the tree itself by either doing the rotations or recoloring the nodes. This tree data structure is named as a Red-Black tree as each node is either Red or Black in color.

Which data structure uses red-black tree?

Applications of Red-Black Trees Also, the Completely Fair Scheduler in the Linux kernel uses this data structure.

Is it good to implement Red-Black trees as an array?

Yes, you can represent red-black tree as an array, but it's not worth it. Maximum height of red-black tree is 2*log2(n+1), where n is number of entries. Number of entries in array representation on each level is 2**n, where n is level. So to store 1_000 entries you'd have to allocate array of 1_048_576 entries.

Why Red-Black trees are preferred?

7. Why Red-black trees are preferred over hash tables though hash tables have constant time complexity? Explanation: Redblack trees have O(logn) for ordering elements in terms of finding first and next elements.


1 Answers

A binary heap is a lot better for what you want. It is easier to implement and faster since you only care about locating the smallest element and insertions. Locating the smallest element is O(1), removing it is O(log N), and an insertion is also O(log N).

like image 61
IVlad Avatar answered Oct 01 '22 19:10

IVlad