博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
编写最简单的二叉树
阅读量:6227 次
发布时间:2019-06-21

本文共 4516 字,大约阅读时间需要 15 分钟。

编写最简单的二叉树

 

二叉树结构

 

源码

-swift-

////  Node.swift//  swift-TreeStructure////  Created by YouXianMing on 15/10/19.//  Copyright © 2015年 ZiPeiYi. All rights reserved.//import UIKitclass Node: NSObject {        /// 节点名字    var nodeName  : String?        /// 左节点    var leftNode  : Node?        /// 右节点    var rightNode : Node?        /**    便利构造器方法        - parameter nodeWithName: 节点名字        - returns: 节点    */    init(withName : String?) {            super.init()        nodeName = withName    }}
////  ViewController.swift//  swift-TreeStructure////  Created by YouXianMing on 15/10/19.//  Copyright © 2015年 ZiPeiYi. All rights reserved.//import UIKitclass ViewController: UIViewController {    let rootNode : Node = Node(withName : "A")        override func viewDidLoad() {                super.viewDidLoad()                // 插入节点        insertNode(rootNode, node: Node(withName : "B"))        insertNode(rootNode, node: Node(withName : "C"))        insertNode(rootNode, node: Node(withName : "D"))        insertNode(rootNode, node: Node(withName : "E"))        insertNode(rootNode, node: Node(withName : "F"))                // 便利节点        treeInfomationWith(rootNode)    }        /**    插入节点        - parameter tree: 根节点    - parameter node: 被插入节点    */    func insertNode(tree : Node, node : Node) {            if tree.leftNode == nil {                    tree.leftNode = node            return        }                if tree.rightNode == nil {                    tree.rightNode = node            return        }                insertNode(tree.leftNode!, node: node)    }        /**    遍历节点        - parameter node: 节点    */    func treeInfomationWith(node : Node) {            if node.leftNode != nil {                    treeInfomationWith(node.leftNode!)        }                print(node.nodeName)                if node.rightNode != nil {                    treeInfomationWith(node.rightNode!)        }    }}
-objective-c-
////  Node.h//  TreeStructure////  Created by YouXianMing on 15/10/19.//  Copyright © 2015年 ZiPeiYi. All rights reserved.//#import 
@interface Node : NSObject/** * 节点名字 */@property (nonatomic, strong) NSString *nodeName;/** * 左节点 */@property (nonatomic, strong) Node *leftNode;/** * 右节点 */@property (nonatomic, strong) Node *rightNode;/** * 便利构造器方法 * * @param nodeName 节点名字 * * @return 节点 */+ (instancetype)nodeWithName:(NSString *)nodeName;@end
////  Node.m//  TreeStructure////  Created by YouXianMing on 15/10/19.//  Copyright © 2015年 ZiPeiYi. All rights reserved.//#import "Node.h"@implementation Node+ (instancetype)nodeWithName:(NSString *)nodeName {    Node *node     = [[[self class] alloc] init];    node.nodeName  = nodeName;        return node;}@end
////  ViewController.m//  TreeStructure////  Created by YouXianMing on 15/10/19.//  Copyright © 2015年 ZiPeiYi. All rights reserved.//#import "ViewController.h"#import "Node.h"@interface ViewController ()@property (nonatomic, strong) Node *rootNode;@end@implementation ViewController- (void)viewDidLoad {        [super viewDidLoad];        // 根节点    self.rootNode = [Node nodeWithName:@"A"];        // 插入节点    [self insertNodeTree:self.rootNode node:[Node nodeWithName:@"B"]];    [self insertNodeTree:self.rootNode node:[Node nodeWithName:@"C"]];    [self insertNodeTree:self.rootNode node:[Node nodeWithName:@"D"]];    [self insertNodeTree:self.rootNode node:[Node nodeWithName:@"E"]];    [self insertNodeTree:self.rootNode node:[Node nodeWithName:@"F"]];        // 遍历节点    [self treeInfomationWithNode:self.rootNode];}/** *  插入节点 * *  @param tree 根节点 *  @param node 被插入节点 */- (void)insertNodeTree:(Node *)tree node:(Node *)node {        if (tree.leftNode == nil) {                tree.leftNode = node;        return;    }        if (tree.rightNode == nil) {                tree.rightNode = node;        return;    }        [self insertNodeTree:tree.leftNode node:node];}/** *  遍历节点 * *  @param node 节点 */- (void)treeInfomationWithNode:(Node *)node {    if (node.leftNode) {                [self treeInfomationWithNode:node.leftNode];    }        NSLog(@"%@", node.nodeName);        if (node.rightNode) {                [self treeInfomationWithNode:node.rightNode];    }}@end

打印结果

2015-10-19 20:05:24.493 TreeStructure[33002:267671] F

2015-10-19 20:05:24.494 TreeStructure[33002:267671] D

2015-10-19 20:05:24.494 TreeStructure[33002:267671] B

2015-10-19 20:05:24.494 TreeStructure[33002:267671] E

2015-10-19 20:05:24.494 TreeStructure[33002:267671] A

2015-10-19 20:05:24.494 TreeStructure[33002:267671] C

转载地址:http://mlfna.baihongyu.com/

你可能感兴趣的文章
cacti和nagios监控web平台搭建
查看>>
Ipython安装
查看>>
Linux下查看tomcat控制台输出信息
查看>>
Linux direct io使用例子
查看>>
jmeter常用插件
查看>>
C# Winform打包部署时添加注册表信息实现开机自启动
查看>>
OC基础知识
查看>>
共享开发者账号
查看>>
python pandas 对各种文件的读写 IO tools
查看>>
团队工作总结
查看>>
用一个固定的数组实现栈和队列
查看>>
IOS中的属性列表----Property List
查看>>
IOS 生成本地验证码
查看>>
经典多普勒效应,相对论多普勒效应,以及质能方程
查看>>
站立会议1
查看>>
客户端定时自动打印页面的例子
查看>>
WPF DatePicker 默认显示当前时间
查看>>
学生考试系统
查看>>
自己动手写一个单链表
查看>>
[转]VirtualBox添加新硬盘
查看>>