编写最简单的二叉树
二叉树结构
源码
-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 <Foundation/Foundation.h>
@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
时间: 2025-01-21 06:28:23