Skip to content

Commit 7d96733

Browse files
author
wupeng10
committed
添加二叉树方法
1 parent 8996ce2 commit 7d96733

File tree

2 files changed

+158
-0
lines changed

2 files changed

+158
-0
lines changed

iOSInterView/iOSInterView/BinarySortTree.h

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,4 +175,41 @@
175175
*/
176176
+ (void)deleteNodeInTree:(BinarySortTree *)root deleteNode:(NSInteger)value;
177177

178+
/**
179+
* desc: 判断一棵树是否是二叉搜索树,二叉搜索树特点:父节点的值大于所有左子树的值,小于所有右子树的值,前提是假设所有值没有相同的
180+
*
181+
* params:根结点,插入的值
182+
*
183+
*/
184+
+ (BOOL)isBinarySearchTree:(BinarySortTree *)root;
185+
186+
/**
187+
* desc: 判断一棵树是否是另一棵树的子树
188+
*
189+
* params:root 父树根节点,childNode 子树根节点
190+
*
191+
*/
192+
+ (BOOL)isContainTree:(BinarySortTree *)root childNode:(BinarySortTree *)childNode;
193+
194+
/**
195+
* desc: 找出一棵二叉树中所有路径,并且其路径之和等于一个给定的值
196+
*
197+
* params: root 根节点,path 暂时存储路径的数组,vector 返回最终符合要求的数组,sum 给定值
198+
*/
199+
+ (void)findAllPathInTree:(BinarySortTree *)root pathArray:(NSMutableArray *)path vectore:(NSMutableArray *)vectore sum:(NSInteger)sum;
200+
201+
/**
202+
* desc: 找出二叉树中一个节点的后继
203+
*
204+
* params: root 根节点,node 目标节点
205+
*/
206+
+ (BinarySortTree *)findSuccessor:(BinarySortTree *)root searchNode:(BinarySortTree *)node;
207+
208+
/**
209+
* desc: 找出二叉树中一个节点的前驱
210+
*
211+
* params: root 根节点,node 目标节点
212+
*/
213+
+ (BinarySortTree *)findPrecursor:(BinarySortTree *)root searchNode:(BinarySortTree *)node;
214+
178215
@end

iOSInterView/iOSInterView/BinarySortTree.m

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,125 @@ + (BinarySortTree *)getMiddleSortNode:(BinarySortTree *)node {
533533
return middleNode;
534534
}
535535

536+
// 判断二叉树是否是二叉搜索树
537+
+ (BOOL)isBinarySearchTree:(BinarySortTree *)root {
538+
539+
if (nil == root) {
540+
return YES;
541+
}
542+
543+
if ((root.leftNode && root.value < root.leftNode.value) || (root.rightNode && root.value > root.rightNode.value)) {
544+
return NO;
545+
}
546+
[self isBinarySearchTree:root.leftNode];
547+
[self isBinarySearchTree:root.rightNode];
548+
549+
return YES;
550+
}
551+
552+
// 判断一棵树是否是另一棵树的子树
553+
+ (BOOL)isContainTree:(BinarySortTree *)root childNode:(BinarySortTree *)childNode {
554+
if (nil == childNode) {
555+
return YES;
556+
}
557+
558+
if (nil == root) {
559+
return NO;
560+
}
561+
562+
if (root.value == childNode.value) {
563+
if ([self isMatch:root childNode:childNode]) {
564+
return YES;
565+
}
566+
}
567+
568+
return [self isContainTree:root.leftNode childNode:childNode] || [self isContainTree:root.rightNode childNode:childNode];
569+
}
570+
571+
+ (BOOL)isMatch:(BinarySortTree *)root childNode:(BinarySortTree *)childNode {
572+
if (nil == root && nil == childNode) {
573+
return YES;
574+
}
575+
if (nil == root || nil == childNode) {
576+
return NO;
577+
}
578+
if (root.value != childNode.value) {
579+
return NO;
580+
}
581+
return [self isMatch:root.leftNode childNode:childNode.leftNode] && [self isMatch:root.rightNode childNode:childNode.rightNode];
582+
}
583+
584+
// 找出一棵二叉树中所有路径,并且其路径之和等于一个给定的值
585+
+ (void)findAllPathInTree:(BinarySortTree *)root pathArray:(NSMutableArray *)path vectore:(NSMutableArray *)vectore sum:(NSInteger)sum {
586+
if (nil == root) {
587+
return;
588+
}
589+
if (nil != root && nil == root.leftNode && nil == root.rightNode && sum - root.value > 0) {
590+
[path removeLastObject];
591+
}
592+
593+
[path addObject:[NSNumber numberWithInteger:root.value]];
594+
if (sum == root.value) {
595+
[vectore addObject:[[NSMutableArray alloc] initWithArray:path]];
596+
}
597+
598+
[self findAllPathInTree:root.leftNode pathArray:path vectore:vectore sum:(sum - root.value)];
599+
[self findAllPathInTree:root.rightNode pathArray:path vectore:vectore sum:(sum - root.value)];
600+
}
601+
602+
// 中序遍历不带父链接的二叉树,找到给定节点的下一个节点,后继为比当前节点大的所有节点中值最小的那个
603+
+ (BinarySortTree *)findSuccessor:(BinarySortTree *)root searchNode:(BinarySortTree *)node {
604+
if (nil == root) {
605+
return nil;
606+
}
607+
608+
if (nil != node.rightNode) {
609+
BinarySortTree *left = node.rightNode;
610+
while (left.leftNode) {
611+
left = left.leftNode;
612+
}
613+
return left;
614+
}
615+
616+
BinarySortTree *parentNode = nil;
617+
while (root) {
618+
if (root.value > node.value) {
619+
parentNode = root;
620+
root = root.leftNode;
621+
}
622+
else {
623+
root = root.rightNode;
624+
}
625+
}
626+
627+
return parentNode;
628+
}
629+
630+
// 中序遍历不带父链接的二叉树,找到给定节点的下一个节点,后继为比当前节点小的所有节点中值最大的那个
631+
+ (BinarySortTree *)findPrecursor:(BinarySortTree *)root searchNode:(BinarySortTree *)node {
632+
if (nil == root) {
633+
return nil;
634+
}
635+
636+
if (node.leftNode) {
637+
BinarySortTree *right = node.leftNode;
638+
while (right.rightNode) {
639+
right = right.rightNode;
640+
}
641+
return right;
642+
}
643+
644+
BinarySortTree *parentNode = nil;
645+
while (root) {
646+
if (root.value >= node.value) {
647+
root = root.leftNode;
648+
}
649+
else {
650+
parentNode = root;
651+
root = root.rightNode;
652+
}
653+
}
654+
return parentNode;
655+
}
656+
536657
@end

0 commit comments

Comments
 (0)