https://github.com/mattcox/tree
A hierarchical tree structure for Swift
https://github.com/mattcox/tree
hierarchy ios macos sequence structure swift tree
Last synced: 3 months ago
JSON representation
A hierarchical tree structure for Swift
- Host: GitHub
- URL: https://github.com/mattcox/tree
- Owner: mattcox
- License: mit
- Created: 2023-08-17T15:40:53.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-12-28T10:53:50.000Z (about 1 year ago)
- Last Synced: 2025-04-29T17:54:51.986Z (9 months ago)
- Topics: hierarchy, ios, macos, sequence, structure, swift, tree
- Language: Swift
- Homepage: https://mattcox.github.io/Tree/
- Size: 1.17 MB
- Stars: 14
- Watchers: 2
- Forks: 6
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Tree
Welcome to **Tree**, a Swift package implementing a hierarchical tree structure
constructed of interconnected nodes.
## 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.
```swift
// 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.
```swift
let root = Root("root") {
Branch("A") {
"C"
"D"
}
"B"
}
```
The tree can then be enumerated of inspected for properties.
```swift
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](https://swift.org/package-manager). To install it within another Swift package, add it as a dependency within your `Package.swift` manifest:
```swift
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:
```swift
import Tree
```