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 bb5005b

Browse files
author
penghe
committed
day15二叉树bug fix
1 parent 21de3d7 commit bb5005b

File tree

3 files changed

+20
-12
lines changed

3 files changed

+20
-12
lines changed

‎Day15/DSBinaryTree/DSBinaryTree/DSBinaryTree.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
1515
@property (nonatomic, strong) DSTreeNode *root;
1616

1717
- (instancetype)initWithObject:(NSObject *)object;
18-
- (BOOL)insertNode:(DSTreeNode *)node parent:(DSTreeNode *)parent isLeftChild:(BOOL)value;
18+
- (BOOL)insertNode:(NSObject *)node parent:(NSObject *)parent isLeftChild:(BOOL)value;
1919
- (DSTreeNode *)find:(NSObject *)object;
2020

2121
- (void)preOrderTraversal;

‎Day15/DSBinaryTree/DSBinaryTree/DSBinaryTree.m‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,23 +25,26 @@ - (instancetype)initWithObject:(NSObject *)object
2525
}
2626

2727
//插入结点
28-
- (BOOL)insertNode:(DSTreeNode *)node parent:(DSTreeNode *)parent isLeftChild:(BOOL)value
28+
- (BOOL)insertNode:(NSObject *)node parent:(NSObject *)parent isLeftChild:(BOOL)value
2929
{
30+
DSTreeNode *treeNode = [[DSTreeNode alloc] init];
31+
treeNode.object = node;
32+
DSTreeNode *parentNode = [self find:parent];
3033
//如果插入的是左孩子结点并且左孩子结点不存在可以插入
31-
if (value == true && parent.leftChild == nil) {
32-
//插入的结点父结点是parent
33-
node.parent = parent;
34+
if (value == true && parentNode.leftChild == nil) {
35+
//插入的结点父结点是parent
36+
treeNode.parent = parentNode;
3437
//父结点的左孩子结点是当前结点
35-
parent.leftChild = node;
38+
parentNode.leftChild = treeNode;
3639
}
3740
//否则插入的是右孩子结点
38-
else if (parent.rightChild == nil) {
39-
node.parent = parent;
40-
parent.rightChild = node;
41+
else if (parentNode.rightChild == nil) {
42+
treeNode.parent = parentNode;
43+
parentNode.rightChild = treeNode;
4144
}
4245
//如果某个结点的左右孩子结点都存在结束 并提示错误
4346
else {
44-
NSAssert(parent.leftChild != nil || parent.rightChild != nil, @"Can't insert into parent node!");
47+
NSAssert(parentNode.leftChild != nil || parentNode.rightChild != nil, @"Can't insert into parent node!");
4548
return false;
4649
}
4750
return true;

‎Day15/DSBinaryTree/DSBinaryTree/ViewController.m‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//
88

99
#import "ViewController.h"
10-
10+
#import"DSBinaryTree.h"
1111
@interface ViewController ()
1212

1313
@end
@@ -16,7 +16,12 @@ @implementation ViewController
1616

1717
- (void)viewDidLoad {
1818
[super viewDidLoad];
19-
// Do any additional setup after loading the view, typically from a nib.
19+
20+
DSBinaryTree *tree = [[DSBinaryTree alloc] initWithObject:@1];
21+
[tree insertNode:@2 parent:@1 isLeftChild:YES];
22+
[tree insertNode:@3 parent:@1 isLeftChild:NO];
23+
[tree insertNode:@5 parent:@2 isLeftChild:NO];
24+
2025
}
2126

2227

0 commit comments

Comments
(0)

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