Tree

1.0.0

A hierarchical tree structure for Swift
mattcox/Tree

What's New

Release v1.0.0

2023-08-23T16:13:21Z

The initial release of the Tree package.

Tree

Swift Swift Package Manager

Welcome to Tree, a Swift package implementing a hierarchical tree structure constructed of interconnected nodes.

An example of a basic tree hierarchy

Usage

Tree's store a value associated with each node. This can be any identifiable type. The identifier is used for tracking the identity of a node within the tree.

Building a tree is simple; you create a root node and add child nodes.

// Create a root node.
//
let root = Node("root")

// Create two nodes as children of the root node.
//
let A = root.append(child: "A")
let B = root.append(child" "B")

// Create some leaf nodes as children of node A.
//
let C = A.append(child: "C")
let D = A.append(child: "D")

Building a tree is even easier with the declarative tree builder.

let root = Root("root") {
    Branch("A") {
        "C"
        "D"
    }
    
    "B"
}

The tree can then be enumerated of inspected for properties.

print(root.isRoot)
// "true"

print(root.isLeaf)
// "false"

if let A = root.node(identifiedBy: "A") {
    print(A.reduce("") {
        $0 + "\($1.element), "
    })
    // "C, D, "
}

Documentation

For more information on usage, the Tree documentation can be found at: https://mattcox.github.io/Tree/.

Installation

Tree is distributed using the Swift Package Manager. To install it within another Swift package, add it as a dependency within your Package.swift manifest:

let package = Package(
    // . . .
    dependencies: [
        .package(url: "https://github.com/mattcox/Tree.git", branch: "main")
    ],
    // . . .
)

If you’d like to use Tree within an iOS, macOS, watchOS or tvOS app, then use Xcode’s File > Add Packages... menu command to add it to your project.

Import Tree wherever you’d like to use it:

import Tree

Description

  • Swift Tools 5.7.0
View More Packages from this Author

Dependencies

Last updated: Thu Mar 14 2024 19:31:52 GMT-0900 (Hawaii-Aleutian Daylight Time)