编写最简单的二叉树
二叉树结构
源码
-swift-
//
// Node.swift
// swift-TreeStructure
//
// Created by YouXianMing on 15/10/19.
// Copyright © 2015年 ZiPeiYi. All rights reserved.
//
import UIKit
class 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 UIKit
class 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
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