FSTLLRBNode.h 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #import <Foundation/Foundation.h>
  2. NS_ASSUME_NONNULL_BEGIN
  3. /**
  4. * A FSTLLRBColor is the color of a tree node. It can be RED, BLACK, or unset.
  5. */
  6. typedef NS_ENUM(NSInteger, FSTLLRBColor) {
  7. FSTLLRBColorUnspecified = 0,
  8. FSTLLRBColorRed = 1,
  9. FSTLLRBColorBlack = 2,
  10. };
  11. /**
  12. * FSTLLRBNode is the interface for a node in a FSTTreeSortedDictionary.
  13. */
  14. @protocol FSTLLRBNode <NSObject>
  15. /**
  16. * Creates a copy of the given node, changing any values that were specified.
  17. * For any parameter that is left as nil, this instance's value will be used.
  18. */
  19. - (instancetype)copyWith:(nullable id)aKey
  20. withValue:(nullable id)aValue
  21. withColor:(FSTLLRBColor)aColor
  22. withLeft:(nullable id<FSTLLRBNode>)aLeft
  23. withRight:(nullable id<FSTLLRBNode>)aRight;
  24. /** Returns a tree node with the given key-value pair set/updated. */
  25. - (id<FSTLLRBNode>)insertKey:(id)aKey forValue:(id)aValue withComparator:(NSComparator)aComparator;
  26. /** Returns a tree node with the given key removed. */
  27. - (id<FSTLLRBNode>)remove:(id)key withComparator:(NSComparator)aComparator;
  28. /** Returns the number of elements at this node or beneath it in the tree. */
  29. - (NSUInteger)count;
  30. /** Returns true if this is an FSTLLRBEmptyNode -- a leaf node in the tree. */
  31. - (BOOL)isEmpty;
  32. - (BOOL)inorderTraversal:(BOOL (^)(id key, id value))action;
  33. - (BOOL)reverseTraversal:(BOOL (^)(id key, id value))action;
  34. /** Returns the left-most node under (or including) this node. */
  35. - (id<FSTLLRBNode>)min;
  36. /** Returns the key of the left-most node under (or including) this node. */
  37. - (nullable id)minKey;
  38. /** Returns the key of the right-most node under (or including) this node. */
  39. - (nullable id)maxKey;
  40. /** Returns true if this node is red (as opposed to black). */
  41. - (BOOL)isRed;
  42. /** Checks that this node and below it hold the red-black invariants. Throws otherwise. */
  43. - (int)check;
  44. // Accessors for properties.
  45. - (nullable id)key;
  46. - (nullable id)value;
  47. - (FSTLLRBColor)color;
  48. - (nullable id<FSTLLRBNode>)left;
  49. - (nullable id<FSTLLRBNode>)right;
  50. @end
  51. NS_ASSUME_NONNULL_END