/
CWAVLTree.m
295 lines (254 loc) · 7.16 KB
/
CWAVLTree.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
/*
// CWAVLTree.m
// Zangetsu
//
// Created by Colin Wheeler on 7/31/12.
//
//
Copyright (c) 2012 Colin Wheeler
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import "CWAVLTree.h"
#define AVLDebug 1
//=============================================
// AVLNode
//=============================================
@interface AVLNode : NSObject
@property(retain) id var;
@property(retain) AVLNode *left;
@property(retain) AVLNode *right;
@property(weak) AVLNode *parent;
@end
@implementation AVLNode
-(id)init
{
self = [super init];
if (self) {
_var = nil;
_left = nil;
_right = nil;
_parent = nil;
}
return self;
}
@end
//=============================================
// AVLTree
//=============================================
#define kLeftDirection 1
#define kRightDirection -1
//Node Information
NSInteger _levelCountOfDescendentsFromNode(AVLNode *node);
NSInteger weightOfNode(AVLNode *node);
NSComparisonResult compareObjects(id obj1,id obj2, NSComparator comparitor);
//Rotations
AVLNode *rotateLeft(AVLNode *node);
AVLNode *rotateRight(AVLNode *node);
AVLNode *doubleRotateLeftRight(AVLNode *node);
AVLNode *doubleRotateRightLeft(AVLNode *node);
@interface CWAVLTree()
@property(readwrite, assign) NSUInteger count;
@property(retain) AVLNode *root;
-(void)balanceNode:(AVLNode *)node withWeight:(NSInteger)weight;
@end
@implementation CWAVLTree
- (id)init
{
self = [super init];
if (self) {
_root = nil;
_count = 0;
_comparitor = [^(id obj1, id obj2){
return [obj1 compare:obj2];
} copy];
}
return self;
}
NSComparisonResult compareObjects(id obj1,id obj2, NSComparator comparitor)
{
SEL compareSelector = @selector(compare:);
if ([obj1 respondsToSelector:compareSelector] &&
[obj2 respondsToSelector:compareSelector]) {
return [obj1 compare:obj2];
}
if (comparitor) {
return comparitor(obj1,obj2);
}
return NSOrderedSame;
}
NSInteger weightOfNode(AVLNode *node)
{
NSInteger leftWeight = _levelCountOfDescendentsFromNode(node.left);
NSInteger rightWeight = _levelCountOfDescendentsFromNode(node.right);
return (leftWeight - rightWeight);
}
NSInteger _levelCountOfDescendentsFromNode(AVLNode *node)
{
if(node == nil) { return 0; }
NSInteger leftCount = _levelCountOfDescendentsFromNode(node.left);
NSInteger rightCount = _levelCountOfDescendentsFromNode(node.right);
NSInteger maxSubDepth = MAX(leftCount,rightCount);
return ++maxSubDepth; //account for the node we are currently on...
}
AVLNode *rotateLeft(AVLNode *node)
{
CWConditionalLog(AVLDebug, @"rotating left");
AVLNode *aNode = node.right;
node.right = aNode.left;
aNode.left = node;
return aNode;
}
AVLNode *rotateRight(AVLNode *node)
{
CWConditionalLog(AVLDebug, @"rotating right");
AVLNode *aNode = node.left;
node.left = aNode.right;
aNode.right = node;
return aNode;
}
AVLNode *doubleRotateLeftRight(AVLNode *node)
{
CWConditionalLog(AVLDebug, @"rotating left right");
node.left = rotateLeft(node.left);
AVLNode *aNode = rotateRight(node);
return aNode;
}
AVLNode *doubleRotateRightLeft(AVLNode *node)
{
CWConditionalLog(AVLDebug, @"rotating right left");
node.right = rotateRight(node.left);
AVLNode *aNode = rotateLeft(node);
return aNode;
}
-(void)balanceNode:(AVLNode *)node withWeight:(NSInteger)weight
{
NSParameterAssert(node);
if (weight >= 2) {
//balance left node
NSInteger leftWeight = weightOfNode(node.left);
if (leftWeight == 1) {
rotateRight(node);
} else if(leftWeight == -1) {
doubleRotateLeftRight(node);
}
} else if(weight <= -2) {
//balance right node
NSInteger rightWeight = weightOfNode(node.right);
if (rightWeight == 1) {
rotateLeft(node);
} else if(rightWeight == -1) {
doubleRotateRightLeft(node);
}
}
}
-(void)addObject:(id)object
{
CWConditionalLog(AVLDebug, @"Inserting %@...",object);
if (self.root == nil) {
AVLNode *node = [[AVLNode alloc] init];
node.var = object;
self.root = node;
CWConditionalLog(AVLDebug, @"Set Root ");
return;
}
AVLNode *currentNode = self.root;
AVLNode *currentParentNode = nil;
NSInteger direction = 0;
while (currentNode) {
NSComparisonResult compare = compareObjects(currentNode.var, object, self.comparitor);
if (compare == NSOrderedAscending) {
currentParentNode = currentNode;
currentNode = currentNode.right;
direction = kRightDirection;
CWConditionalLog(AVLDebug, @"Going Right -->>");
continue;
} else if (compare == NSOrderedDescending) {
currentParentNode = currentNode;
currentNode = currentNode.left;
direction = kLeftDirection;
CWConditionalLog(AVLDebug, @"<<-- Going Left");
continue;
} else if (compare == NSOrderedSame) {
CWConditionalLog(AVLDebug, @"Items are the same");
return;
}
}
AVLNode *node = [[AVLNode alloc] init];
node.var = object;
node.parent = currentParentNode;
if (direction == kRightDirection) {
currentParentNode.right = node;
} else if (direction == kLeftDirection) {
currentParentNode.left = node;
}
currentNode = node;
self.count++;
while (currentNode) {
NSInteger weight = weightOfNode(currentNode);
CWConditionalLog(AVLDebug,@"Node Weight: %li",weight);
if (weight > 1 || weight < -1) {
[self balanceNode:currentNode
withWeight:weight];
}
currentNode = currentNode.parent;
}
}
-(void)removeObject:(id)object
{
CWDebugLog(@"Not Implemented yet");
}
-(BOOL)objectIsInTree:(id)object
{
if(!self.root) { return NO; }
if ([self.root.var isEqual:object]) {
return YES;
}
AVLNode *currentNode = self.root;
BOOL result = NO;
while (currentNode) {
NSComparisonResult compare = compareObjects(currentNode.var, object, self.comparitor);
if(compare == NSOrderedAscending){
currentNode = currentNode.right;
continue;
} else if(compare == NSOrderedDescending) {
currentNode = currentNode.left;
continue;
} else if (compare == NSOrderedSame) {
result = YES;
break;
}
}
return result;
}
-(void)enumerateNodesInTreeWithBlock:(void(^)(id obj))block
{
if (!self.root) { return; }
AVLNode *node = self.root;
CWQueue *stack = [[CWQueue alloc] init];
[stack enqueue:node];
while (![stack isEmpty]) {
AVLNode *aNode = [stack dequeue];
block(aNode.var);
if (aNode.left) {
[stack enqueue:aNode.left];
}
if (aNode.right) {
[stack enqueue:aNode.right];
}
}
}
@end