-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDCBinaryTree.m
More file actions
91 lines (68 loc) · 2 KB
/
DCBinaryTree.m
File metadata and controls
91 lines (68 loc) · 2 KB
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
//
// DCBinaryTree.m
//
// Created by Diogo do Carmo on 24/10/13.
//
//
#import "DCBinaryTree.h"
@interface DCBinaryTree ()
@end
@implementation DCBinaryTree
@synthesize left;
@synthesize right;
- (id)initWithContent:(id)content andParent:(DCBinaryTree *)parent {
self = [super init];
self.content = content;
self.parent = parent;
self.left = nil;
self.right = nil;
return self;
}
- (void)releaseTreeUnderAndIncluding:(DCBinaryTree *)node {
if (!node) {
return;
}
[self releaseTreeUnderAndIncluding:node.left];
[self releaseTreeUnderAndIncluding:node.right];
[node release];
}
- (NSInteger)createTreeFromArray:(NSArray *)array nullPointerAs:(NSString *)nullPointer startingFromIndex:(NSInteger)index {
if (index < array.count && ![[array objectAtIndex:index] isEqualToString:nullPointer]) {
self.content = [array objectAtIndex:index];
} else {
return index;
}
NSInteger leftIndex, rightIndex;
self.left = [[DCBinaryTree alloc] initWithContent:nil andParent:self];
leftIndex = [self.left createTreeFromArray:array nullPointerAs:nullPointer startingFromIndex:index + 1];
if (index + 1 == leftIndex) {
[self.left release];
self.left = nil;
}
self.right = [[DCBinaryTree alloc] initWithContent:nil andParent:self];
rightIndex = [self.right createTreeFromArray:array nullPointerAs:nullPointer startingFromIndex:leftIndex + 1];
if (leftIndex + 1 == rightIndex) {
[self.right release];
self.right = nil;
}
return rightIndex;
}
- (void)insertLeft:(id)object {
self.left = [[DCBinaryTree alloc] initWithContent:object andParent:self];
}
- (void)insertRight:(id)object {
self.right = [[DCBinaryTree alloc] initWithContent:object andParent:self];
}
- (BOOL)isLeafNode {
if (!self.left && !self.right) {
return YES;
}
return NO;
}
- (BOOL)isRootNode {
if (!self.parent) {
return YES;
}
return NO;
}
@end