-
Notifications
You must be signed in to change notification settings - Fork 3
/
NUTree.m
executable file
·1084 lines (912 loc) · 33.6 KB
/
NUTree.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// NUTree.m
// NUTree
//
// Created by . Carlin on 10/16/13.
// Copyright (c) 2013 Carlin Creations. All rights reserved.
//
#import "NUTree.h"
#define DEFAULT_NODE_CAPACITY 3 // Must be > 2, or can't split branches properly
#pragma mark - NUTreeNode
@implementation NUTreeNode
- (id)init
{
self = [super init];
if (self) {
_data = [NSMutableArray new];
_children = [NSMutableArray new];
}
return self;
}
/** @brief Initialize with parent node */
- (id)initWithParent:(NUTreeNode *)parent
{
self = [super init];
if (self) {
_parent = parent;
_data = [NSMutableArray new];
_children = [NSMutableArray new];
}
return self;
}
/** @brief Get index of node in children array */
- (NSUInteger)indexOfChildNode:(NUTreeNode *)child
{
return [self.children indexOfObject:child];
// Binary search method
// [self.children indexOfObject:child
// inSortedRange:NSMakeRange(0, self.children.count)
// options:NSBinarySearchingFirstEqual
// usingComparator:^NSComparisonResult(id obj1, id obj2) {
// NUTreeNode *n1 = (NUTreeNode *)obj1;
// NUTreeNode *n2 = (NUTreeNode *)obj2;
// if (n1 == n2) {
// return NSOrderedSame;
// } else {
// return [n1.data[0] compare:n2.data[0]];
// }
// }];
}
/** @brief Get index of object in data array */
- (NSUInteger)indexOfDataObject:(id)object
{
return [self.data indexOfObject:object
inSortedRange:NSMakeRange(0, self.data.count)
options:NSBinarySearchingFirstEqual
usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
}
/** @brief Print entire tree */
- (NSString *)printTree
{
return [self printTreeNode:self indent:1];
}
- (NSString *)printTreeNode:(NUTreeNode *)node indent:(int)indent
{
// Build indent
NSMutableString *padding = [NSMutableString new];
for (int i = 0; i < indent; ++i) {
[padding appendString:@"\t"];
}
// Build string
NSMutableString *string = [[node description] mutableCopy];
for (NUTreeNode *child in node.children) {
[string appendString:[NSString stringWithFormat:@"\n%@%@",
padding, [self printTreeNode:child indent:indent + 1]]];
}
return string;
}
/** @brief Prints and checks for pointer discrepancies in children, returns YES if all children have appropriate pointers to each other and their parent. Does not include the childrens' children. */
- (BOOL)hasValidPointerStructure
{
BOOL valid = YES;
NUTreeNode *current, *next;
// Trivial case
if (!self.children.count) {
return YES;
}
// Iterate through children and check pointers
for (int i = 0; i < self.children.count; ++i)
{
current = self.children[i];
if (current.parent != self) {
valid = NO;
NSLog(@"Child with wrong parent pointer: %@", current);
}
if (i + i < self.children.count) {
next = self.children[i + 1];
if (next.previous != current
|| current.next != next) {
valid = NO;
NSLog(@"Siblings with wrong pointers: %@ <-> %@", current, next);
}
}
}
return valid;
}
- (NSString *)description
{
return [NSString stringWithFormat:@"%p: %@", self, [[self.data valueForKey:@"description"] componentsJoinedByString:@", "]];
}
- (id)copyWithZone:(NSZone *)zone
{
id copy = [[[self class] alloc] init];
if (copy) {
[copy setData:[self.data copyWithZone:zone]];
[copy setChildren:[self.children copyWithZone:zone]];
}
return copy;
}
@end
#pragma mark - NUTree
@interface NUTree()
@property (nonatomic, strong) NUTreeNode *root;
@property (nonatomic, assign) NSInteger nodeMinimum;
@property (nonatomic, assign, readwrite) NSInteger nodeCapacity;
@property (nonatomic, assign, readwrite) NSInteger count;
// Cache and flag for quick access
@property (nonatomic, assign, readwrite) BOOL cacheOutdated;
@property (nonatomic, strong) NSArray *cache;
// Fast enumeration
@property (nonatomic, assign) BOOL fastEnumerating;
@end
@implementation NUTree
#pragma mark - Constructors
- (id)init
{
self = [super init];
if (self) {
_nodeCapacity = DEFAULT_NODE_CAPACITY;
_nodeMinimum = _nodeCapacity / 2;
_cacheOutdated = NO;
_fastEnumerating = NO;
_root = [NUTreeNode new];
}
return self;
}
/** @brief Create tree with a certain number of allowable children */
- (id)initWithNodeCapacity:(NSUInteger)nodeCapacity
{
self = [super init];
if (self) {
_nodeCapacity = MAX(nodeCapacity, DEFAULT_NODE_CAPACITY);
_nodeMinimum = _nodeCapacity / 2;
_cacheOutdated = NO;
_fastEnumerating = NO;
_root = [NUTreeNode new];
}
return self;
}
/** @brief Create tree with a certain number of allowable children using the given array of objects as its base data */
- (id)initWithNodeCapacity:(NSUInteger)nodeCapacity withSortedObjects:(NSArray *)data
{
self = [super init];
if (self) {
_nodeCapacity = MAX(nodeCapacity, DEFAULT_NODE_CAPACITY);
_nodeMinimum = _nodeCapacity / 2;
_cacheOutdated = NO;
_fastEnumerating = NO;
_root = [self buildTreeWithNodeCapacity:_nodeCapacity withSortedObjects:data];
_count = data.count;
}
return self;
}
/** @brief Description when printed using NSLog */
- (NSString *)description
{
return [self printTree]; // Print whole tree
}
/** @brief Construct tree by bulkloading given array of object data
@param data NSArray of objects, must be sorted.
@return NUTreeNode * Root of data tree.
*/
- (NUTreeNode *)buildTreeWithNodeCapacity:(NSUInteger)nodeCapacity withSortedObjects:(NSArray *)data
{
NSMutableArray *children = [NSMutableArray new];
NSMutableArray *parents = [NSMutableArray new];
NUTreeNode *child, *parent, *prev;
// Create leaves
for (NSUInteger i = 0; i < data.count; )
{
// Create new leaf node, set pointers
child = [NUTreeNode new];
if (prev) {
child.previous = prev;
prev.next = child;
}
prev = child; // update previous
// Fill it with max capacity + 1 data, except for last node
NSUInteger fillCount = nodeCapacity
+ (data.count - i > nodeCapacity + 1 ? 1 : 0);
for (NSUInteger j = 0; j < fillCount && i < data.count; ++j, ++i) {
[child.data addObject:data[i]];
}
// Add child to array
[children addObject:child];
}
// Build rest of tree from leaves
while (children.count > 1)
{
// NSLog(@"CHILDREN: \n%@", children);
// Setup for next level
[parents removeAllObjects];
prev = nil;
// Create parents using children
for (NSUInteger i = 0; i < children.count - 1; )
{
// Create parent node, set pointers
parent = [NUTreeNode new];
if (prev) {
parent.previous = prev;
prev.next = parent;
}
prev = parent; // update previous
// Fill it with data & children
NSUInteger fillCount = nodeCapacity
+ (children.count - i > nodeCapacity + 1 ? 1 : 0);
for (NSUInteger j = 0; j < fillCount && i < children.count - 1; ++j, ++i)
{
child = children[i];
NSUInteger index = child.data.count - 1;
// Add child
[parent.children addObject:child];
child.parent = parent;
// Add data from end of child
[parent.data addObject:child.data[index]];
[child.data removeObjectAtIndex:index];
}
// Add last child
if (i == children.count - 1)
{
child = children[i];
[parent.children addObject:child];
child.parent = parent;
}
// Add parent to array
[parents addObject:parent];
}
children = parents;
}
return children[0];
}
#pragma mark - Public Methods
/** @brief Add object to tree, YES if successful */
- (BOOL)addObject:(id)object
{
if (!object) {
return NO;
}
if ([self addObject:object withChild:nil toNode:
[self getLeafNodeForObject:object inNode:self.root]]) {
self.count++;
self.cacheOutdated = YES;
return YES;
}
return NO;
}
/** @brief Remove object from tree, returns NO if not in tree */
- (BOOL)removeObject:(id)object
{
if (!object || self.root.data.count <= 0) {
return NO;
}
if ([self removeObject:object fromNode:
[self getFirstNodeThatContains:object inBranch:self.root]]) {
self.count--;
self.cacheOutdated = YES;
return YES;
}
return NO;
}
/** @brief Search for object in tree, returns NO if not found */
- (BOOL)containsObject:(id)object
{
if (!object || self.root.data.count <= 0) {
return NO;
}
return ([self getFirstNodeThatContains:object inBranch:self.root] != nil);
}
/** @brief Returns YES if tree is empty */
- (BOOL)isEmpty
{
return (self.root.data.count == 0);
}
/** @brief Returns minimum element, or nil if none */
- (id)minimum
{
if (self.root.data.count) {
NUTreeNode *node = [self getLeftMostNode:self.root];
if (node.data && node.data.count) {
return [node.data objectAtIndex:0];
} else {
NSLog(@"Warning! Non-root node with empty data!");
}
}
return nil;
}
/** @brief Returns maximum element, or nil if none */
- (id)maximum
{
if (self.root.data.count) {
NSArray *data = [[self getRightMostNode:self.root] data];
return [data objectAtIndex:data.count - 1];
}
return nil;
}
/** @brief Returns sorted array of tree contents */
- (NSArray *)toArray
{
// Check cache & rebuild if necessary
if (!self.cache || self.cacheOutdated) {
[self rebuildCache];
}
return self.cache;
}
/** @brief Rebuild cache for fast access, returns NO if cache could not be refreshed (probably due to someone iterating over it) */
- (BOOL)rebuildCache
{
// Don't rebuild cache if fast enumerating
if (self.fastEnumerating) {
return NO;
}
NSMutableArray *storage = [NSMutableArray new];
// Traverse and add data into array in order
[self traverse:^BOOL(NUTreeNode *node, id data, id extra) {
[(NSMutableArray *)extra addObject:data];
return YES;
}
extraData:storage
onTree:self.root
withAlgorithm:NUTreeTraverseAlgorithmInorder];
// Set cache, clear flag
self.cache = storage;
self.cacheOutdated = NO;
return YES;
}
/** @brief Returns number of elements in tree */
- (NSUInteger)trueCount
{
static NSString *KEY_COUNT = @"total";
if (self.root.data.count) {
NSMutableDictionary *extra = [@{
KEY_COUNT: [NSNumber numberWithInt:0]
} mutableCopy];
[self traverse:^BOOL(NUTreeNode *node, id data, id extra) {
extra[KEY_COUNT] = [NSNumber
numberWithInteger:[extra[KEY_COUNT] unsignedIntegerValue] + 1];
return 1;
} extraData:extra onTree:self.root
withAlgorithm:NUTreeTraverseAlgorithmInorder];
return [extra[KEY_COUNT] unsignedIntegerValue];
}
return 0;
}
/** @brief Returns printout of the tree */
- (NSString *)printTree
{
NSMutableString *result = [NSMutableString new];
[self traverse:^BOOL(NUTreeNode *node, id data, id extra) {
NSMutableString *padding = [NSMutableString new];
for (NUTreeNode *parent = node.parent; parent; parent = parent.parent) {
[padding appendString:@"\t"];
}
[extra appendString:[NSString stringWithFormat:@"%@%@\n", padding, data]];
return YES;
} extraData:result onTree:self.root
withAlgorithm:NUTreeTraverseAlgorithmInorder];
return result;
}
/** @brief Returns object at index, or nil if none / out of bounds */
- (id)objectAtIndex:(NSUInteger)index
{
// Insanity checks
// if (index < 0) {
// return nil;
// }
// Check cache & rebuild if necessary
if (!self.cache || self.cacheOutdated) {
[self rebuildCache];
}
// Check index is within bounds
if (index >= self.cache.count) {
return nil;
}
return self.cache[index];
}
/** @brief Traverse the tree in sorted order while executing block on every element
@param block Traversal block to be called on data as we traverse
@param extra User defined object that will be passed to block to help do things like aggregate calculations.
@param algo Traversal algorithm: inorder, postorder, preorder, bfs
@return BOOL YES if traversed through entire tree, FALSE if cut short by traversal block
*/
- (BOOL)traverse:(NUTreeTraverseBlock)block
extraData:(id)extra
withAlgorithm:(NUTreeTraverseAlgorithm)algo
{
return [self traverse:block extraData:extra onTree:self.root withAlgorithm:algo];
}
#pragma mark - Tree Methods
/** @brief Adds an object to a node in sorted order, with an accompanying child branch if relevant.
@param object Object to be added.
@param child Child branch to add to node after the data is added.
@param node Node to add the data to.
@return BOOL YES if adding is successful, NO if error
*/
- (BOOL)addObject:(id)object withChild:(NUTreeNode *)child toNode:(NUTreeNode *)node
{
if (!object || !node) {
return NO;
}
// Find index where we should put it, and add it
NSUInteger index = [node.data indexOfObject:object
inSortedRange:NSMakeRange(0, node.data.count)
options:NSBinarySearchingInsertionIndex
usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
[node.data insertObject:object atIndex:index];
// Add child if exists, need to add right after data insertion
if (child)
{
if (index + 1 > node.children.count) {
NSLog(@"Warning! Adding child at index greater than children count for child: %@", child);
}
// Insert & change parent pointer
[node.children insertObject:child atIndex:index + 1];
child.parent = node;
// Switch up sibling pointers
NUTreeNode *sibling = node.children[index];
if (sibling) {
child.next = sibling.next;
child.previous = sibling;
child.previous.next = child;
if (child.next) {
child.next.previous = child;
}
}
else // This shouldn't happen
{
NSLog(@"Warning! Checking next sibling pointer while adding child: %@", child);
}
}
// Rebalance as needed
[self rebalanceNode:node];
return YES;
}
/** @brief Removes an object from a node
@param object Object to be removed.
@param node Node to remove object from.
@return BOOL YES if removed, NO if not found or if there was an error.
*/
- (BOOL)removeObject:(id)object fromNode:(NUTreeNode *)node
{
if (!object || !node || node.data.count <= 0) {
return NO;
}
// NSLog(@"Removing object %@ from node %@", object, node);
// Get index to remove from
NSUInteger index = [node indexOfDataObject:object];
if (index == NSNotFound) {
NSLog(@"Warning! Could not find index of object for removal: %@", object);
return NO;
}
// If leaf node, simple remove
if (!node.children.count)
{
// If we use removeObject:(id) it removes all occurrences
[node.data removeObjectAtIndex:index];
// Rebalance as needed
[self rebalanceNode:node];
}
else // Deal with replacing separator
{
// Replace with smallest value from right subtree
NUTreeNode *child = [self getLeftMostNode:node.children[index + 1]];
id replacementObject = child.data[0];
[node.data replaceObjectAtIndex:index withObject:replacementObject];
[child.data removeObjectAtIndex:0];
// Rebalance child node if needed
[self rebalanceNode:child];
}
return YES;
}
/** @brief Returns the first node that contains the given object using standard comparison rules, starting from given node branch. */
- (NUTreeNode *)getFirstNodeThatContains:(id)object inBranch:(NUTreeNode *)node
{
if (!object || !node || !node.data.count) {
return nil;
}
// Search for item in node data
NSUInteger index = [node.data indexOfObject:object
inSortedRange:NSMakeRange(0, node.data.count)
options:NSBinarySearchingInsertionIndex
| NSBinarySearchingFirstEqual
usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
// If within bounds of data (note the <= count due to subtree indexing)
if (index <= node.data.count)
{
// Check if item is equal at index
if (index < node.data.count && [node.data[index] compare:object] == NSOrderedSame) {
return node;
}
// If has children, need to search subtree
if (node.children.count) {
return [self getFirstNodeThatContains:object inBranch:node.children[index]];
}
}
return nil;
}
/** @brief Returns the lowest node that contains the given object using standard comparison rules, starting from given node branch. */
- (NUTreeNode *)getLowestNodeThatContains:(id)object inBranch:(NUTreeNode *)node
{
if (!object || !node || !node.data.count) {
return nil;
}
// NSLog(@"Get: %@ in %@", object, node);
// Search for item in node data
NSUInteger index = [node.data indexOfObject:object
inSortedRange:NSMakeRange(0, node.data.count)
options:NSBinarySearchingInsertionIndex
| NSBinarySearchingFirstEqual
usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
// If within bounds of data (note the <= count due to subtree indexing)
if (index <= node.data.count)
{
// Search subtree (don't terminate early on find because it's worth finding and deleting from leaf node to prevent restructuring)
NUTreeNode *child = nil;
if (node.children.count) {
child = [self getLowestNodeThatContains:object inBranch:node.children[index]];
}
// If item exists and is equal at index and no child with value exists, then use as return value
if (index < node.data.count && [node.data[index] compare:object] == NSOrderedSame) {
return (child) ? child : node;
}
return child;
}
return nil;
}
/** @brief Searches for and returns the appropriate leaf node for an object to be inserted, starting from given node. */
- (NUTreeNode *)getLeafNodeForObject:(id)object inNode:(NUTreeNode *)node
{
if (!object || !node) {
return nil;
}
// If there are children, go farther down
if (node.children.count)
{
// Search for item in node data
NSUInteger index = [node.data indexOfObject:object
inSortedRange:NSMakeRange(0, node.data.count)
options:NSBinarySearchingInsertionIndex
| NSBinarySearchingFirstEqual
usingComparator:^NSComparisonResult(id obj1, id obj2) {
return [obj1 compare:obj2];
}];
// If within bounds of children
if (index < node.children.count) {
return [self getLeafNodeForObject:object
inNode:node.children[index]];
} else {
NSLog(@"Warning: could not find leaf node for object: %@", object);
return nil; // This shouldn't happen!
}
}
else { // Found the node
return node;
}
}
/** @brief Returns left-most node in tree starting from given node */
- (NUTreeNode *)getLeftMostNode:(NUTreeNode *)node
{
while (node.children.count) {
node = node.children[0];
}
return node;
}
/** @brief Returns right-most node in tree starting from given node */
- (NUTreeNode *)getRightMostNode:(NUTreeNode *)node
{
while (node.children.count) {
node = node.children[node.children.count-1];
}
return node;
}
/** @brief Traverse the tree in sorted order while executing block on every element
@param block Traversal block to be called on data as we traverse
@param extra User defined object that will be passed to block to help do things like aggregate calculations.
@param root Tree to traverse starting at given node
@param algo Traversal algorithm: inorder, postorder, preorder, bfs
@return BOOL YES if traversed through entire tree, FALSE if cut short by traversal block
*/
- (BOOL)traverse:(NUTreeTraverseBlock)block extraData:(id)extra onTree:(NUTreeNode *)root withAlgorithm:(NUTreeTraverseAlgorithm)algo
{
// Return condition
if (!root) {
return YES;
}
// If Breadth First traversal
if (algo == NUTreeTraverseAlgorithmBreadthFirst)
{
// Go through data
for (NSUInteger i = 0; i < root.data.count; ++i) {
if (!block(root, root.data[i], extra)) {
return NO; // If block cuts traversal short
}
}
// Go to next sibling node, or next level's leftmost node
if (root.next) {
if (![self traverse:block extraData:extra onTree:root.next withAlgorithm:algo]) {
return NO; // If block cuts traversal short
}
}
else // Find next level's leftmost node
{
// Go to leftmost node in current level
NUTreeNode *node = root;
while (node.previous) {
node = node.previous;
}
// Start traversal on it's leftmost child
if (node.children.count) {
if (![self traverse:block extraData:extra onTree:node.children[0] withAlgorithm:algo]) {
return NO; // Traversal cut short
}
} else {
// NSLog(@"End of Breadth First Traversal");
return YES;
}
}
}
else // Depth First traversal
{
if (algo == NUTreeTraverseAlgorithmPostorder)
{
for (NSUInteger i = 0; i < root.children.count; ++i) {
if (![self traverse:block extraData:extra onTree:root.children[i] withAlgorithm:algo]) {
return NO; // Traversal cut short
}
}
}
// Process data, note the <= count for subtree traversal
for (NSUInteger i = 0; i <= root.data.count; ++i)
{
// Process subtrees in order
if (algo == NUTreeTraverseAlgorithmInorder &&
i < root.children.count &&
![self traverse:block extraData:extra onTree:root.children[i] withAlgorithm:algo])
{
return NO; // Traversal cut short
}
// Process data in order
if (i < root.data.count && !block(root, root.data[i], extra))
{
return NO; // Traversal cut short
}
}
if (algo == NUTreeTraverseAlgorithmPreorder)
{
for (NSUInteger i = 0; i < root.children.count; ++i) {
if (![self traverse:block extraData:extra onTree:root.children[i] withAlgorithm:algo]) {
return NO; // Traversal cut short
}
}
}
}
return YES; // Made it through traversal
}
- (void)rebalanceNode:(NUTreeNode *)node
{
// NSLog(@"Tree State: \n%@", [self printTree]);
// If node is at capacity, need to split
if (node.data.count > self.nodeCapacity)
{
// NSLog(@"Rebalance Node with Max Capacity: %@", node);
// Create right node to be efficient about removing from arrays
NUTreeNode *newRightNode = [[NUTreeNode alloc] initWithParent:node.parent];
NSUInteger middle = node.data.count / 2;
NSUInteger childIndex = middle + 1;
id object = node.data[middle];
// Iterate through data & children from middle + 1 and add to new node
for (NSUInteger i = childIndex; i < node.data.count; ++i) {
[newRightNode.data addObject:node.data[i]];
}
for (NSUInteger i = childIndex; i < node.children.count; ++i) {
[newRightNode.children addObject:node.children[i]];
[node.children[i] setParent:newRightNode];
}
// Remove old items from left node, including middle item
[node.data removeObjectsInRange:
NSMakeRange(middle, node.data.count - middle)];
// Remove old children from left node if exists, including middle
if (node.children.count) {
[node.children removeObjectsInRange:
NSMakeRange(childIndex, node.children.count - childIndex)];
}
// Add to parent, if exists
if (node.parent) {
[self addObject:object withChild:newRightNode toNode:node.parent];
}
else if (node == self.root) // Root node, need to create new root
{
NUTreeNode *newRootNode = [NUTreeNode new];
// Set current node's new parent, add as child to new parent
node.parent = newRootNode;
[newRootNode.children addObject:node];
// Set new root
self.root = newRootNode;
// Add data and new right branch to new parent
[self addObject:object withChild:newRightNode toNode:newRootNode];
}
else {
// This shouldn't happen
NSLog(@"Warning! Rebalancing node that doesn't have a parent and isn't the root!");
}
}
// If node is below min capacity (and not the root), need to join
else if (node != self.root && node.data.count < self.nodeMinimum)
{
// NSLog(@"Rebalance Node with Min Capacity: %@", node);
// If right sibling has more than min elements, rotate left
if (node.next && node.next.parent == node.parent
&& node.next.data.count > self.nodeMinimum) {
[self rotateNode:node toRight:NO];
}
// If left sibling has more than min elements, rotate right
else if (node.previous && node.previous.parent == node.parent
&& node.previous.data.count > self.nodeMinimum) {
[self rotateNode:node toRight:YES];
}
// Otherwise, need to merge node with one of its siblings
else {
[self mergeSiblingWithNode:node];
}
}
// For debugging, check if has valid pointer structure
// if (![node hasValidPointerStructure]) {
// NSLog(@"Invalid pointer state on node: %@", node);
// }
// NSLog(@"Tree After operation on node: %@ \n%@", node, [self printTree]);
}
- (void)rotateNode:(NUTreeNode *)node toRight:(BOOL)direction
{
// NSLog(@"Rotate node %@ %@", node, (direction ? @"Right" : @"Left"));
// Can't rotate if no node, no siblings in direction to rotate,
// or no data in sibling, or siblings not from same parent
if (!node || !node.parent || !node.parent.data.count
|| (!direction && (!node.next
|| node.next.parent != node.parent
|| !node.next.data.count))
|| (direction && (!node.previous
|| node.previous.parent != node.parent
|| !node.previous.data.count))) {
NSLog(@"Warning! Rotating on node without sibling in right direction: %@", node);
return;
}
// Get index of node in children array of parent
NSUInteger indexOfChild = [node.parent indexOfChildNode:node];
if (indexOfChild == NSNotFound) {
NSLog(@"Warning! Could not find index of child in parent: %@", node);
return;
}
// Insert parent data that is next to the node
NSUInteger indexOfParentData = indexOfChild - direction;
NSUInteger indexOfInsert = (direction ? 0 : node.data.count);
[node.data insertObject:node.parent.data[indexOfParentData]
atIndex:indexOfInsert];
// Replace parent data with data from sibling
NUTreeNode *sibling = (direction ? node.previous : node.next);
NSUInteger indexOfRemove = (direction ? sibling.data.count - 1 : 0);
[node.parent.data replaceObjectAtIndex:indexOfParentData
withObject:sibling.data[indexOfRemove]];
[sibling.data removeObjectAtIndex:indexOfRemove];
// Also move corresponding child of sibling to node if needed
if (sibling.children.count)
{
indexOfRemove += (direction ? 1 : 0); // +1 if rotating right
NUTreeNode *child = sibling.children[indexOfRemove];
// Move to node
indexOfInsert += (direction ? 0 : 1); // +1 if rotating left
[node.children insertObject:child atIndex:indexOfInsert];
child.parent = node; // Change parents, but siblings are the same
// Remove from sibling
[sibling.children removeObjectAtIndex:indexOfRemove];
}
}
- (void)mergeSiblingWithNode:(NUTreeNode *)node
{
// NSLog(@"Merge on node: %@", node);
// Sanity checks: need siblings or node to exist
if (!node || (!node.previous && !node.next)) {
NSLog(@"Warning! Merge called on node with no siblings!");
NSLog(@"Tree: \n%@", [self printTree]);
return;
}
// Setup for merge
NUTreeNode *leftNode, *rightNode, *parent;
// Merge with right node if possible
if (node.next && node.next.parent == node.parent)
{
leftNode = node;
rightNode = node.next;
}
// If we can't merge with right node, merge left
else if (node.previous && node.previous.parent == node.parent)
{
leftNode = node.previous;
rightNode = node;
}
// This shouldn't happen
else {
NSLog(@"Warning! Reached end of merge with no siblings!");
NSLog(@"Tree: \n%@", [self printTree]);
return;
}
// Find index of separator object in parent
parent = leftNode.parent;
NSUInteger index = [parent indexOfChildNode:leftNode];
// Transfer data & children over from parent / right node
[leftNode.data addObject:parent.data[index]];
for (NSUInteger i = 0; i < rightNode.data.count; ++i) {
[leftNode.data addObject:rightNode.data[i]];
}
for (NSUInteger i = 0; i < rightNode.children.count; ++i) {
[leftNode.children addObject:rightNode.children[i]];
[rightNode.children[i] setParent:leftNode];
}
// Clean up parent / right node
[parent.data removeObjectAtIndex:index];
[parent.children removeObjectAtIndex:index + 1];
leftNode.next = rightNode.next;