将森林转化成二叉树并在iPhone上显示
来源:互联网 发布:oracle数据库试题 编辑:程序博客网 时间:2024/05/29 02:57
//
// TreeNode.h
// Huffman
//
// Created by zmx on 16/2/25.
// Copyright © 2016年 zmx. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface TreeNode : NSObject
@property (nonatomic,copy) NSString *data;
@property (nonatomic,strong) NSMutableArray *childs;
@end
//
// TreeNode.m
// Huffman
//
// Created by zmx on 16/2/25.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "TreeNode.h"
@implementation TreeNode
- (NSMutableArray *)childs {
if (_childs ==nil) {
_childs = [NSMutableArrayarray];
}
return_childs;
}
@end
//
// BTreeNode.h
// Huffman
//
// Created by zmx on 16/2/24.
// Copyright © 2016年 zmx. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface BTreeNode : NSObject
@property (nonatomic,copy) NSString *data;
@property (nonatomic,strong) BTreeNode *leftChild;
@property (nonatomic,strong) BTreeNode *rightChild;
@property (nonatomic,readonly, assign)int depth;
@property (nonatomic,readonly, assign)int maxBottomNodeCount;
@end
//
// BTreeNode.m
// Huffman
//
// Created by zmx on 16/2/24.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "BTreeNode.h"
@implementation BTreeNode
- (int)depth {
returnMAX(self.leftChild.depth,self.rightChild.depth) +1;
}
- (int)maxBottomNodeCount {
return pow(2,self.depth -1);
}
@end
//
// TreeView.h
// Huffman
//
// Created by zmx on 16/2/24.
// Copyright © 2016年 zmx. All rights reserved.
//
#import <UIKit/UIKit.h>
@class BTreeNode;
@interface TreeView : UIView
@property (nonatomic,strong) BTreeNode *tree;
@end
//
// TreeView.m
// Huffman
//
// Created by zmx on 16/2/24.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "TreeView.h"
#import "BTreeNode.h"
// UIView+ZMX.h文件详细请见https://github.com/zmx6999/ZMXView/tree/master
#import "UIView+ZMX.h"
#define margin 10
#define nodeW ((ScreenW - margin * (self.tree.maxBottomNodeCount + 1)) / self.tree.maxBottomNodeCount)
#define maxNodeCenterHorizontalDistance ((ScreenW - nodeW - margin * 2) * 0.5)
@implementation TreeView
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
[selfappendBTreeNode:self.treefloor:1center:CGPointMake(ScreenW *0.5, 60)];
}
- (void)setTree:(BTreeNode *)tree {
_tree = tree;
[selfsetNeedsDisplay];
}
- (void)appendBTreeNode:(BTreeNode *)treeNode floor:(int)floor center:(CGPoint)center {
UILabel *label = [[UILabelalloc] init];
label.text = treeNode.data;
label.textColor = [UIColorblackColor];
label.font = [UIFontsystemFontOfSize:12];
label.bounds = CGRectMake(0, 0, nodeW, nodeW);
label.center = center;
label.textAlignment =NSTextAlignmentCenter;
[self addSubview:label];
CGFloat nodeCenterHorizontalDistance = maxNodeCenterHorizontalDistance * pow(0.5, floor);
CGFloat nodeBorderHorizontalDistance = nodeCenterHorizontalDistance -nodeW;
if (treeNode.leftChild) {
UIBezierPath *path = [UIBezierPathbezierPath];
CGPoint pointA = [UIViewleftBottomPointWithCenter:center width:nodeW];
[path moveToPoint:pointA];
CGPoint pointB = [UIViewleftBottomPointWithPoint:pointA horizontalDistance:nodeBorderHorizontalDistance];
[path addLineToPoint:pointB];
[path stroke];
CGPoint leftCenter = [UIViewleftBottomPointWithPoint:center horizontalDistance:nodeCenterHorizontalDistance];
[self appendBTreeNode:treeNode.leftChildfloor:floor + 1center:leftCenter];
}
if (treeNode.rightChild) {
UIBezierPath *path = [UIBezierPathbezierPath];
CGPoint pointA = [UIViewrightBottomPointWithCenter:center width:nodeW];
[path moveToPoint:pointA];
CGPoint pointB = [UIViewrightBottomPointWithPoint:pointA horizontalDistance:nodeBorderHorizontalDistance];
[path addLineToPoint:pointB];
[path stroke];
CGPoint rightCenter = [UIViewrightBottomPointWithPoint:center horizontalDistance:nodeCenterHorizontalDistance];
[self appendBTreeNode:treeNode.rightChildfloor:floor + 1center:rightCenter];
}
}
@end
//
// ViewController.m
// Huffman
//
// Created by zmx on 16/2/24.
// Copyright © 2016年 zmx. All rights reserved.
//
#import "ViewController.h"
#import "TreeNode.h"
#import "TreeView.h"
#import "BTreeNode.h"
#define N 5
@interface ViewController ()
@property (weak, nonatomic) IBOutletTreeView *treeView;
@end
@implementation ViewController
- (void)viewDidLoad {
[superviewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.treeView.tree = [self convertToBTreeWithTrees:[selfcreateTree]];
}
- (NSArray *)createTree {
NSMutableArray *trees = [NSMutableArrayarray];
TreeNode *nodeB = [[TreeNodealloc] init];
nodeB.data = @"B";
TreeNode *nodeC = [[TreeNodealloc] init];
nodeC.data = @"C";
TreeNode *nodeD = [[TreeNodealloc] init];
nodeD.data = @"D";
TreeNode *nodeA = [[TreeNodealloc] init];
nodeA.data = @"A";
[nodeA.childs addObjectsFromArray:@[nodeB, nodeC, nodeD]];
[trees addObject:nodeA];
TreeNode *nodeF = [[TreeNodealloc] init];
nodeF.data = @"F";
TreeNode *nodeE = [[TreeNodealloc] init];
nodeE.data = @"E";
[nodeE.childs addObjectsFromArray:@[nodeF]];
[trees addObject:nodeE];
TreeNode *nodeJ = [[TreeNodealloc] init];
nodeJ.data = @"J";
TreeNode *nodeH = [[TreeNodealloc] init];
nodeH.data = @"H";
[nodeH.childs addObjectsFromArray:@[nodeJ]];
TreeNode *nodeI = [[TreeNodealloc] init];
nodeI.data = @"I";
TreeNode *nodeG = [[TreeNodealloc] init];
nodeG.data = @"G";
[nodeG.childs addObjectsFromArray:@[nodeH, nodeI]];
[trees addObject:nodeG];
return trees;
}
- (BTreeNode *)convertToBTreeWithTrees:(NSArray *)trees {
NSMutableArray *bTrees = [NSMutableArrayarray];
for (TreeNode *treein trees) {
BTreeNode *bTree = [selfconvertToBTreeWithTree:tree];
[bTrees addObject:bTree];
}
for (int i =0; i < bTrees.count; i++) {
if (i > 0) {
BTreeNode *bTree = [bTrees objectAtIndex:i];
BTreeNode *bLastTree = [bTrees objectAtIndex:i - 1];
bLastTree.rightChild = bTree;
}
}
return bTrees.firstObject;
}
- (BTreeNode *)convertToBTreeWithTree:(TreeNode *)tree {
BTreeNode *bTree = [[BTreeNodealloc] init];
bTree.data = tree.data;
NSMutableArray *bChildTrees = [NSMutableArrayarray];
for (TreeNode *childTreein tree.childs) {
BTreeNode *bChildTree = [selfconvertToBTreeWithTree:childTree];
[bChildTrees addObject:bChildTree];
}
for (int i =0; i < bChildTrees.count; i++) {
BTreeNode *bChildTree = [bChildTrees objectAtIndex:i];
if (i == 0) {
bTree.leftChild = bChildTree;
} else {
BTreeNode *bLastChildTree = [bChildTreesobjectAtIndex:i - 1];
bLastChildTree.rightChild = bChildTree;
}
}
return bTree;
}
@end
- 将森林转化成二叉树并在iPhone上显示
- 非二叉树转换成二叉树并在iPhone上展示
- 二叉树、树、森林之间转化
- 树、森林转化为二叉树
- 树、二叉树、森林之间的转化
- 二叉树、树、森林之间的转化
- 二叉树与森林的转化
- C++森林转化为二叉树代码
- 森林和二叉树的转化
- 在iPhone上展示哈夫曼二叉树
- 将二叉树转化成线索二叉树
- 解析XML文件,并将信息显示在窗体上
- 将HTML文本转换并显示在Label上
- 树、森林与二叉树的转化理解
- 树、森林与二叉树相互转化原理图
- 【基础算法】:树,森林,二叉树的转化
- 二叉树转换成森林&森林变成二叉树
- 森林转换成二叉树
- RFID+二维码扫描
- Unity3D Shader官方教程翻译(一)
- JDK5News
- 有一个桶,里面有白球、黑球各100个,人们必须按照以下的规则把球取出来:
- Cpp_动态内存分配
- 将森林转化成二叉树并在iPhone上显示
- 类和对象
- UVA 11627 - Slalom(二分)
- TQ2440,学习笔记之寻址方式
- Adroid listView 学习
- JAVA-计算器
- 【codevs2304】【BZOJ1875】HH去散步,第一次的矩阵加速DP
- Java8新特性--Interface中的default方法(接口默认方法)
- C++学习笔记(1)