Code Monkey home page Code Monkey logo

nodes's Introduction

Nodes

Nodes is a class protocol for tree data structures. A class which conforms the Node-Protocol, will gain useful properties to easily navigate in the tree.

Usage

Create a new class and conform it to the Node-Protocol:

final class SimpleNode: Node {
    typealias Value = String
    var value: Value
    weak var parent: SimpleNode?
    var children: [SimpleNode]
    
    init(value: Value) {
        self.value = value
        self.children = []
    }
}

extension SimpleNode: Equatable, CustomStringConvertible {
    static func == (lhs: SimpleNode, rhs: SimpleNode) -> Bool {
        return lhs.value == rhs.value && lhs.parent == rhs.parent
    }

    public var description: String {
        return "\(value)"
    }
}

Create a root node:

let root = SimpleNode(value: "Hand")

Add children with addChild(node: Node):

root.addChild(node: SimpleNode(value: "Thumb"))
root.addChild(node: SimpleNode(value: "Index finger"))
root.addChild(node: SimpleNode(value: "Middle finger"))
root.addChild(node: SimpleNode(value: "Ring finger"))
root.addChild(node: SimpleNode(value: "Little finger"))

Print tree to console:

print(root.lineBasedDescription)

Result:

Hand
β”œβ”€β”€ Thumb
β”œβ”€β”€ Index finger
β”œβ”€β”€ Middle finger
β”œβ”€β”€ Ring finger
└── Little finger

Features

Ancestors

/// Returns all parent nodes.
var ancestors: [Node]
/// Returns all parent nodes, including the current node.
var ancestorsIncludingSelf: [Node]
/// A Boolean value indicating whether the current node is the top node.
var isRoot: Bool
/// Returns the top node.
var root: Node

Descendants

/// Adds a sub-node.
func addChild(node: Node)
/// Returns the number of children.
var degree: Int
/// Returns all descendants, traversing the entire tree.
var descendants: [Node]

Leaves

/// A Boolean value indicating whether the node is without children.
var isLeaf: Bool
/// Returns all nodes with no children.
var leaves: [Node]
/// Returns the number of leaves.
var breadth: Int

Branches

/// A Boolean value indicating whether the node has children.
var isBranch: Bool
/// Returns all nodes with at least one child.
var branches: [Node]

Siblings

/// Returns all other nodes with the same parent.
var siblings: [Node]
/// Returns all nodes (including the current node) with the same parent.
var siblingsIncludingSelf: [Node]

Position

/// Returns the distance between a node and the root.
var depth: Int
/// The number of edges between the current node and the root.
var level: Int

Textual representation

var lineBasedDescription: String

Example

let root = SimpleNode(value: "Apple")

let desktops = SimpleNode(value: "Desktops")
root.addChild(node: desktops)

let macPro = SimpleNode(value: "Mac Pro")
desktops.addChild(node: macPro)

let macMini = SimpleNode(value: "Mac Mini")
desktops.addChild(node: macMini)

let iMac = SimpleNode(value: "iMac")
desktops.addChild(node: iMac)

let notebooks = SimpleNode(value: "Notebooks")
root.addChild(node: notebooks)

let macBookPro = SimpleNode(value: "MacBook Pro")
notebooks.addChild(node: macBookPro)

let devices = SimpleNode(value: "Devices")
root.addChild(node: devices)

let handhelds = SimpleNode(value: "Handhelds")
devices.addChild(node: handhelds)

let ipod = SimpleNode(value: "iPod")
handhelds.addChild(node: ipod)

let iphone = SimpleNode(value: "iPhone")
handhelds.addChild(node: iphone)

let newton = SimpleNode(value: "Newton")
handhelds.addChild(node: newton)

let setTopBoxes = SimpleNode(value: "Set-top boxes")
devices.addChild(node: setTopBoxes)

let appleTV = SimpleNode(value: "Apple TV")
setTopBoxes.addChild(node: appleTV)


print(root.lineBasedDescription)

Output:

Apple
β”œβ”€β”€ Desktops
β”‚   β”œβ”€β”€ Mac Pro
β”‚   β”œβ”€β”€ Mac Mini
β”‚   └── iMac
β”œβ”€β”€ Notebooks
β”‚   └── MacBook Pro
└── Devices
    β”œβ”€β”€ Handhelds
    β”‚   β”œβ”€β”€ iPod
    β”‚   β”œβ”€β”€ iPhone
    β”‚   └── Newton
    └── Set-top boxes
        └── Apple TV

Other Projects

  • BinaryKit β€” BinaryKit helps you to break down binary data into bits and bytes and easily access specific parts.
  • Clippy β€” Clippy from Microsoft Office is back and runs on macOS! Written in Swift.
  • GrammaticalNumber β€” Turns singular words to the plural and vice-versa in Swift.
  • HackMan β€” Stop writing boilerplate code yourself. Let hackman do it for you via the command line.
  • ISO8859 β€” Convert ISO8859 1-16 Encoded Text to String in Swift. Supports iOS, tvOS, watchOS and macOS.
  • SpriteMap β€” SpriteMap helps you to extract sprites out of a sprite map. Written in Swift.
  • StringCase β€” Converts String to lowerCamelCase, UpperCamelCase and snake_case. Tested and written in Swift.
  • TinyConsole β€” TinyConsole is a micro-console that can help you log and display information inside an iOS application, where having a connection to a development computer is not possible.

License

Nodes is released under the MIT License.

nodes's People

Contributors

cosmo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.