Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3184da9

Browse files
update demo
1 parent 964087b commit 3184da9

File tree

6 files changed

+55
-22
lines changed

6 files changed

+55
-22
lines changed

‎BinaryTreePrinter/src/com/mj/printer/LevelOrderPrinter.java‎

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ public class LevelOrderPrinter extends Printer {
3030
private Node root;
3131
private int minX;
3232
private int maxWidth;
33+
private List<List<Node>> nodes;
3334

3435
public LevelOrderPrinter(BinaryTreeInfo tree) {
3536
super(tree);
@@ -41,11 +42,12 @@ public LevelOrderPrinter(BinaryTreeInfo tree) {
4142
@Override
4243
public String printString() {
4344
// nodes用来存放所有的节点
44-
List<List<Node>> nodes = new ArrayList<>();
45-
fillNodes(nodes);
46-
cleanNodes(nodes);
47-
compressNodes(nodes);
48-
addLineNodes(nodes);
45+
nodes = new ArrayList<>();
46+
47+
fillNodes();
48+
cleanNodes();
49+
compressNodes();
50+
addLineNodes();
4951

5052
int rowCount = nodes.size();
5153

@@ -88,8 +90,7 @@ private Node addNode(List<Node> nodes, Object btNode) {
8890
/**
8991
* 以满二叉树的形式填充节点
9092
*/
91-
private void fillNodes(List<List<Node>> nodes) {
92-
if (nodes == null) return;
93+
private void fillNodes() {
9394
// 第一行
9495
List<Node> firstRowNodes = new ArrayList<>();
9596
firstRowNodes.add(root);
@@ -131,9 +132,7 @@ private void fillNodes(List<List<Node>> nodes) {
131132
/**
132133
* 删除全部null、更新节点的坐标
133134
*/
134-
private void cleanNodes(List<List<Node>> nodes) {
135-
if (nodes == null) return;
136-
135+
private void cleanNodes() {
137136
int rowCount = nodes.size();
138137
if (rowCount < 2) return;
139138

@@ -184,9 +183,7 @@ private void cleanNodes(List<List<Node>> nodes) {
184183
/**
185184
* 压缩空格
186185
*/
187-
private void compressNodes(List<List<Node>> nodes) {
188-
if (nodes == null) return;
189-
186+
private void compressNodes() {
190187
int rowCount = nodes.size();
191188
if (rowCount < 2) return;
192189

@@ -282,7 +279,7 @@ private Node addLineNode(List<Node> curRow, List<Node> nextRow, Node parent, Nod
282279
return top;
283280
}
284281

285-
private void addLineNodes(List<List<Node>> nodes) {
282+
private void addLineNodes() {
286283
List<List<Node>> newNodes = new ArrayList<>();
287284

288285
int rowCount = nodes.size();

‎BinaryTreePrinterOC/BinaryTreePrinterOC/MJBinaryTrees/MJBinaryTreeInfo.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
NS_ASSUME_NONNULL_BEGIN
1212

13-
@protocol MJBinaryTreeInfo
13+
@protocol MJBinaryTreeInfo <NSObject>
1414
@required
1515
/**
1616
* who is the root node

‎BinaryTreePrinterOC/BinaryTreePrinterOC/Model/MJBinarySearchTree.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,21 @@
1111

1212
NS_ASSUME_NONNULL_BEGIN
1313

14-
typedef int (^MJBSTComparator)(id e1, id e2);
14+
@protocol MJBSTComparator
15+
@required
16+
- (int)compare:(id)e1 another:(id)e2;
17+
@end
18+
19+
typedef int (^MJBSTComparatorBlock)(id e1, id e2);
1520

1621
@interface MJBinarySearchTree : NSObject <MJBinaryTreeInfo>
1722
- (NSUInteger)size;
1823
- (BOOL)isEmpty;
1924
- (void)add:(id)element;
2025

2126
+ (instancetype)tree;
22-
+ (instancetype)treeWithComparator:(_Nullable MJBSTComparator)comparator;
27+
+ (instancetype)treeWithComparatorBlock:(_Nullable MJBSTComparatorBlock)comparator;
28+
+ (instancetype)treeWithComparator:(_Nullable id<MJBSTComparator>)comparator;
2329
@end
2430

2531
NS_ASSUME_NONNULL_END

‎BinaryTreePrinterOC/BinaryTreePrinterOC/Model/MJBinarySearchTree.m‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ @interface MJBinarySearchTree()
3131
{
3232
NSUInteger _size;
3333
MJBSTNode *_root;
34-
MJBSTComparator _comparator;
34+
MJBSTComparatorBlock _comparatorBlock;
35+
id<MJBSTComparator> _comparator;
3536
}
3637
@end
3738

@@ -41,7 +42,13 @@ + (instancetype)tree {
4142
return [self treeWithComparator:nil];
4243
}
4344

44-
+ (instancetype)treeWithComparator:(_Nullable MJBSTComparator)comparator {
45+
+ (instancetype)treeWithComparatorBlock:(_Nullable MJBSTComparatorBlock)comparatorBlock {
46+
MJBinarySearchTree *bst = [[self alloc] init];
47+
bst->_comparatorBlock = comparatorBlock;
48+
return bst;
49+
}
50+
51+
+ (instancetype)treeWithComparator:(id<MJBSTComparator>)comparator {
4552
MJBinarySearchTree *bst = [[self alloc] init];
4653
bst->_comparator = comparator;
4754
return bst;
@@ -91,7 +98,8 @@ - (void)add:(id)element {
9198

9299
#pragma mark - private methods
93100
- (int)_compare:(id)e1 e2:(id)e2 {
94-
return _comparator ? _comparator(e1, e2) : [e1 compare:e2];
101+
return _comparatorBlock ? _comparatorBlock(e1, e2) :
102+
(_comparator ? [_comparator compare:e1 another:e2] : [e1 compare:e2]);
95103
}
96104

97105
#pragma mark - MJBinaryTreeInfo

‎BinaryTreePrinterOC/BinaryTreePrinterOC/main.m‎

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,30 +14,52 @@ void test1() {
1414
int data[] = { 38, 18, 4, 69, 85, 71, 34, 36, 29, 100 };
1515
int len = sizeof(data) / sizeof(int);
1616

17-
MJBinarySearchTree *bst = [[MJBinarySearchTree alloc] init];
17+
MJBinarySearchTree *bst = [MJBinarySearchTree tree];
1818
for (int i = 0; i < len; i++) {
1919
[bst add:@(data[i])];
2020
}
2121

2222
[MJBinaryTrees println:bst];
2323
printf("---------------------------------\n");
2424
[MJBinaryTrees println:bst style:MJPrintStyleInorder];
25+
printf("---------------------------------\n");
2526
}
2627

2728
void test2() {
28-
MJBinarySearchTree *bst = [[MJBinarySearchTree alloc] init];
29+
int data[] = { 38, 18, 4, 69, 85, 71, 34, 36, 29, 100 };
30+
int len = sizeof(data) / sizeof(int);
31+
32+
MJBinarySearchTree *bst = [MJBinarySearchTree
33+
treeWithComparatorBlock:^int(id _Nonnull e1, id _Nonnull e2) {
34+
return [e2 compare:e1];
35+
}];
36+
for (int i = 0; i < len; i++) {
37+
[bst add:@(data[i])];
38+
}
39+
40+
[MJBinaryTrees println:bst];
41+
printf("---------------------------------\n");
42+
[MJBinaryTrees println:bst style:MJPrintStyleInorder];
43+
printf("---------------------------------\n");
44+
}
45+
46+
void test3() {
47+
MJBinarySearchTree *bst = [MJBinarySearchTree tree];
2948
for (int i = 0; i < 20; i++) {
3049
[bst add:@((arc4random() % 666) + 1)];
3150
}
3251

3352
[MJBinaryTrees println:bst];
3453
printf("---------------------------------\n");
3554
[MJBinaryTrees println:bst style:MJPrintStyleInorder];
55+
printf("---------------------------------\n");
3656
}
3757

3858
int main(int argc, const char * argv[]) {
3959
@autoreleasepool {
60+
test1();
4061
test2();
62+
test3();
4163
}
4264
return 0;
4365
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /