iOS实现二叉树数据结构
来源:互联网 发布:璟璞网络福利员 编辑:程序博客网 时间:2024/06/06 02:01
#头文件
@interface YFTree<__covariant ObjectType> : NSObject
/**
根节点的值
*/
@property (nonatomic, strong) ObjectType rootData;
@property (nonatomic, strong) YFTree *leftTree;
@property (nonatomic, strong) YFTree *rightTree;
+ (instancetype)treeWithRootData:(ObjectType)rootData;
+ (instancetype)treeWithRootDate:(ObjectType)rootData leftTree:(YFTree *)leftTree rightTree:(YFTree *)rightTree;
/**
返回前序遍历的数据组
@return 遍历的数据
*/
- (NSArray<ObjectType> *)preOrder;
/**
返回中序遍历的数据组
@return 遍历的数据
*/
- (NSArray<ObjectType> *)inOrder;
/**
返回后序遍历的数据组
@return 遍历的数据
*/
- (NSArray<ObjectType> *)postOrder;
/**
逐层遍历
@return 遍历的数据
*/
- (NSArray<ObjectType> *)levelOrder;
@end
#实现文件
@implementation YFTree
+ (instancetype)treeWithRootData:(id)rootData {
YFTree *newObj = [YFTree new];
newObj.rootData = rootData;
return newObj;
}
+ (instancetype)treeWithRootDate:(id)rootData leftTree:(YFTree *)leftTree rightTree:(YFTree *)rightTree {
YFTree *newObj = [YFTree new];
newObj.rootData = rootData;
newObj.leftTree = leftTree;
newObj.rightTree = rightTree;
return newObj;
}
- (NSArray *)preOrder {
NSMutableArray *objects = [[NSMutableArray alloc] init];
if (_rootData) {
[objects addObject:_rootData];
}
if (_leftTree) {
[objects addObjectsFromArray:[_leftTree preOrder]];
}
if (_rightTree) {
[objects addObjectsFromArray:[_rightTree preOrder]];
}
return [objects copy];
}
- (NSArray *)inOrder {
NSMutableArray *objects = [[NSMutableArray alloc] init];
if (_leftTree) {
[objects addObjectsFromArray:[_leftTree preOrder]];
}
if (_rootData) {
[objects addObject:_rootData];
}
if (_rightTree) {
[objects addObjectsFromArray:[_rightTree preOrder]];
}
return [objects copy];
}
- (NSArray *)postOrder {
NSMutableArray *objects = [[NSMutableArray alloc] init];
if (_leftTree) {
[objects addObjectsFromArray:[_leftTree preOrder]];
}
if (_rightTree) {
[objects addObjectsFromArray:[_rightTree preOrder]];
}
if (_rootData) {
[objects addObject:_rootData];
}
return [objects copy];
}
- (NSArray *)levelOrder {
//添加首个元素
NSMutableArray *objects = [[NSMutableArray alloc] init];
[objects addObject:_rootData];
NSMutableArray *bufferQueue = [[NSMutableArray alloc] init];
[bufferQueue addObject:_leftTree];
[bufferQueue addObject:_rightTree];
YFTree *markTree;
while ([bufferQueue count] > 0) {
markTree = [bufferQueue firstObject];
[objects addObject:markTree.rootData];
if (markTree.leftTree) {
[bufferQueue addObject:markTree.leftTree];
}
if (markTree.rightTree) {
[bufferQueue addObject:markTree.rightTree];
}
[bufferQueue removeObject:markTree];
}
return [objects copy];
}
@end
- iOS实现二叉树数据结构
- 数据结构-二叉树实现
- 数据结构 二叉树实现
- 二叉树数据结构实现
- 【数据结构】 二叉树的实现
- 数据结构之二叉树实现
- 【DS】数据结构--二叉树实现
- C++ 数据结构-------二叉树实现
- 数据结构 二叉搜索树实现
- 【数据结构】二叉树的实现
- <数据结构>二叉树的实现
- java实现二叉树数据结构
- 数据结构-递归实现二叉树
- 二叉树的实现数据结构
- 《C++实现数据结构》:二叉树
- 数据结构与算法(C#实现)---二叉树
- 数据结构与算法(C#实现)---二叉树
- 简单数据结构的实现之二叉树
- URL
- 全志R16平台的android6.0.1系统下的RTL8188EU的配置(分色排版)V1.0
- "微信裂变"软件出现:扫码后自动给好友群发广告
- 博客转移
- HttpURLConnection中文乱码解决
- iOS实现二叉树数据结构
- POJ
- 【CSS 基础】11 京东首页DIV分块布局
- iPhone建立热点-通过USB连接
- 跨域问题:解决跨域的三种方案
- mysql当插入重复数据的时候
- mysql5.5以后的版本安装使用cmake
- 博客初体验
- HDU 6070 Dirt Ratio