Does Cocoa Touch have a search tree data structure

2020-02-12 03:09发布

问题:

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.

回答1:

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:

You use CFTree to create tree structures that represent hierarchical organizations of information. In such structures, each tree node has exactly one parent tree (except for the root tree, which has no parent) and can have multiple children.

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):

  • AVL Tree
  • Abstract Binary Search Tree
  • Andersson Tree
  • Linked List
  • Search Tree
  • Red Black Tree
  • Unbalanced Tree
  • Queue
  • Heap

    You can download CHDataStructures here.



回答2:

CoreFoundation has a CFBinaryHeap type you can use. There's no Obj-C wrapper for it, but a little C never hurt anyone.



回答3:

You can use std::set from the C++ standard library, if you name your file with a .mm extension (Objective-C++ mode).



回答4:

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.