I've been looking into this on Google and read the Collections entry in the SDK documentation, and turned up nothing. Is there a BST (any of its variants) implementation available out of the box with the iOS SDK?
It seems odd that something so basic would be missing from a major development platform. Is their hash implementation just that magical? Or do the devs assume no one is going to do inserts/deletes on things that have an order?
I can use NSSet
for now, as I know most of us (myself included) aren't really writing anything with tons of computation on iOS that need a guaranteed access time, but it's still gnawing at me.
You can use
std::set
from the C++ standard library, if you name your file with a.mm
extension (Objective-C++ mode).CoreFoundation has a CFBinaryHeap type you can use. There's no Obj-C wrapper for it, but a little C never hurt anyone.
CFBinaryHeap looks pretty promising and useful, but it might not be exactly what you want, as it's not really a binary search tree but a heap. They are similar, but not the same, so I feel like Core Foundation's CFTree class might be a little better. Here's a description from the CFTree class reference:
If you're not comfortable with C (Core Foundation is C, not Objective-C), you can use the JKPTree library which is an Objective-C wrapper of CFTree. You can download it here.
UPDATE:
I just found another library called CHDataStructures that simplifies the creation of a wide variety of data structures. It supports the following data structures (and many other unlisted ones):
Heap
You can download CHDataStructures here.
It isn't Cocoa Touch, but the GNU Objective-C collections library has a Red-Black Tree and abstract Binary Tree, as well as a bunch of other non-tree collections stuff.