https://github.com/dnadoba/tree
general tree data structure, tree diffing, NSOutlineView and SwiftUI support
https://github.com/dnadoba/tree
genreal-tree nsoutlineview tree tree-diffing
Last synced: 10 months ago
JSON representation
general tree data structure, tree diffing, NSOutlineView and SwiftUI support
- Host: GitHub
- URL: https://github.com/dnadoba/tree
- Owner: dnadoba
- Created: 2020-12-09T17:52:18.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2021-09-04T06:07:13.000Z (over 4 years ago)
- Last Synced: 2025-03-24T14:44:15.927Z (10 months ago)
- Topics: genreal-tree, nsoutlineview, tree, tree-diffing
- Language: Swift
- Homepage:
- Size: 312 KB
- Stars: 36
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Tree - general tree data structure, tree diffing, NSOutlineView and SwiftUI support [](https://dnadoba.github.io/Tree/)
This package primarily includes a general tree data structure.
It also includes a tree diffing algorithm and `OutlineViewTreeDataSource` that allows to use Swift value types with `NSOutlineView`
A demo App can be found in the `Tree macOS` folder and the `NSOutlineView` Playground with support for drag and drop, undo/redo and animations.
`TreeNode` and `TreeList` can also be used as a model for `OutlineGroup` in SwiftUI applications. To see it in action, take a look at the `SwiftUI-iOS` or `SwiftUI-macOS` Playground.
### `TreeNode`
`TreeNode` is a general tree data structure implementation as a value type.
Each `TreeNode` has a `value` and 0 or more `children`. It is actually just a struct which holds a value and an array of child nodes:
```swift
struct TreeNode {
var value: Value
var children: [TreeNode]
}
```
With the help of `@resultBuilder` you can create a `TreeNode` like this:
> Note: `@resultBuilder` requires Swift 5.4 or higher (e.g. Xcode 12.5)
```swift
let treeNode = TreeNode("root node") {
"child 1"
"child 2"
TreeNode("child 3") {
"child of child 3"
}
"child 4"
}
print(treeNode)
// - root node
// - child 1
// - child 2
// - child 3
// - child of child 3
// - child 4
```
### `TreeList` - a list of `TreeNode`s
`TreeList` is a list of `TreeNode`s, which enables multiple nodes to be at level 0.
In addition, `TreeList` conforms to `MutableCollection` and `BidirectionalCollection` which enables a ton of useful algorithms.
```swift
let treeList = TreeList {
"root 1"
"root 2"
TreeNode("root 3") {
"child of root 3"
}
"root 4"
}
print(treeList)
// - root 1
// - root 2
// - root 3
// - child of root 3
// - root 4
```
### additional methods
`TreeNode` and `TreeList` have a similar API.
They both support special map, compactMap and filter operations:
```swift
extension TreeNode {
func mapValues(_ transform: (Value) -> NewValue) -> TreeNode
func mapValuesWithNode(_ transform: (TreeNode) -> NewValue) -> TreeNode
func mapValuesWithParents(_ transform: ([Value], Value) -> NewValue) -> TreeNode
func mapChildrenWithParents(_ transform: ([Value], [Value]) -> NewValue) -> [NewValue]
func compactMapValues(_ transform: (Value) -> NewValue?) -> TreeNode?
func filterValues(_ isIncluded: (Value) -> Bool) -> TreeNode
}
```
In addition, they support moving nodes which is useful for implementing drag and drop:
```swift
extension TreeList where Value : Hashable {
/// Moves the TreeNodes at the given `sourceIndices` to `insertIndex`.
/// The `sourceIndices` and `insertIndex` are both specified in the before state of the tree.
/// If elements are removed before the `insertIndex`, the `insertIndex` will be adjusted.
///
/// The move operation will fail and return nil, if the `insertIndex` is a child of one of the moved TreeNodes.
/// - Parameters:
/// - sourceIndices: the indices of the elements that should be moved
/// - insertIndex: the insertion index to move the elements to
/// - Returns: A new Tree with the specified changes or nil if the the move was not possible
public func move(indices sourceIndices: [TreeIndex], to insertIndex: TreeIndex) -> TreeList?
}
```
### `NSOutlineView`
> Note: `OutlineViewTreeDataSource` is defined in a seperate module called `TreeUI`. You need to `import TreeUI` to use it.
`TreeList` and `TreeNode` were originaly created to be used as data model for NSOutlineView. Therefore, this package includes a tree diffing algorithm and `OutlineViewTreeDataSource` that allows to use Swift value types with `NSOutlineView`. Take a look at NSOutlineView Playground or the Tree macOS example Xcode project which are part of this repository.
The diffing algorithm can also be used to efficiently send only what has changed over the network to another peer/server.
### SwiftUI
`TreeNode` and `TreeList` can be used with SwiftUI as well.
```swift
import SwiftUI
extension TreeNode {
var optionalChildren: [TreeNode]? {
children.isEmpty ? nil : children
}
}
struct ContentView: View {
@State var tree: TreeList
var body: some View {
List(tree.nodes, id: \.value, children: \.optionalChildren) { node in
Text(node.value)
}
}
}
```
For a full example take a look into the SwiftUI-iOS or SwiftUI-macOS Playground.