将森林转化成二叉树并在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



0 0
原创粉丝点击