{"id":17996718,"url":"https://github.com/cosmo/nodes","last_synced_at":"2025-03-26T04:30:45.530Z","repository":{"id":63907239,"uuid":"218772733","full_name":"Cosmo/Nodes","owner":"Cosmo","description":"🌲🌿🌳 Nodes is a class protocol for tree data structures with multiple children. Written in Swift.","archived":false,"fork":false,"pushed_at":"2023-06-03T16:41:41.000Z","size":21,"stargazers_count":17,"open_issues_count":1,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-21T05:22:05.390Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Swift","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/Cosmo.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-31T13:29:13.000Z","updated_at":"2024-07-02T02:31:15.000Z","dependencies_parsed_at":"2022-11-28T22:53:41.492Z","dependency_job_id":null,"html_url":"https://github.com/Cosmo/Nodes","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cosmo%2FNodes","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cosmo%2FNodes/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cosmo%2FNodes/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Cosmo%2FNodes/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Cosmo","download_url":"https://codeload.github.com/Cosmo/Nodes/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245589127,"owners_count":20640229,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-10-29T21:15:40.241Z","updated_at":"2025-03-26T04:30:45.257Z","avatar_url":"https://github.com/Cosmo.png","language":"Swift","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Nodes\n\nNodes is a class protocol for tree data structures.\nA `class` which conforms the `Node`-Protocol, will gain useful properties to easily navigate in the tree. \n\n## Usage\n\nCreate a new `class` and conform it to the `Node`-Protocol:\n\n```swift\nfinal class SimpleNode: Node {\n    typealias Value = String\n    var value: Value\n    weak var parent: SimpleNode?\n    var children: [SimpleNode]\n    \n    init(value: Value) {\n        self.value = value\n        self.children = []\n    }\n}\n\nextension SimpleNode: Equatable, CustomStringConvertible {\n    static func == (lhs: SimpleNode, rhs: SimpleNode) -\u003e Bool {\n        return lhs.value == rhs.value \u0026\u0026 lhs.parent == rhs.parent\n    }\n\n    public var description: String {\n        return \"\\(value)\"\n    }\n}\n```\n\nCreate a root node:\n\n```swift\nlet root = SimpleNode(value: \"Hand\")\n```\n\nAdd children with `addChild(node: Node)`:\n\n```swift\nroot.addChild(node: SimpleNode(value: \"Thumb\"))\nroot.addChild(node: SimpleNode(value: \"Index finger\"))\nroot.addChild(node: SimpleNode(value: \"Middle finger\"))\nroot.addChild(node: SimpleNode(value: \"Ring finger\"))\nroot.addChild(node: SimpleNode(value: \"Little finger\"))\n```\n\nPrint tree to console:\n\n```swift\nprint(root.lineBasedDescription)\n```\n\nResult:\n\n```\nHand\n├── Thumb\n├── Index finger\n├── Middle finger\n├── Ring finger\n└── Little finger\n```\n\n## Features\n\n### Ancestors\n\n```swift\n/// Returns all parent nodes.\nvar ancestors: [Node]\n```\n\n```swift\n/// Returns all parent nodes, including the current node.\nvar ancestorsIncludingSelf: [Node]\n```\n\n```swift\n/// A Boolean value indicating whether the current node is the top node.\nvar isRoot: Bool\n```\n\n```swift\n/// Returns the top node.\nvar root: Node\n```\n\n### Descendants\n\n```swift\n/// Adds a sub-node.\nfunc addChild(node: Node)\n```\n\n```swift\n/// Returns the number of children.\nvar degree: Int\n```\n\n```swift\n/// Returns all descendants, traversing the entire tree.\nvar descendants: [Node]\n```\n\n### Leaves\n\n```swift\n/// A Boolean value indicating whether the node is without children.\nvar isLeaf: Bool\n```\n\n```swift\n/// Returns all nodes with no children.\nvar leaves: [Node]\n```\n\n```swift\n/// Returns the number of leaves.\nvar breadth: Int\n```\n\n### Branches\n\n```swift\n/// A Boolean value indicating whether the node has children.\nvar isBranch: Bool\n```\n\n```swift\n/// Returns all nodes with at least one child.\nvar branches: [Node]\n```\n\n### Siblings\n\n```swift\n/// Returns all other nodes with the same parent.\nvar siblings: [Node]\n```\n\n```swift\n/// Returns all nodes (including the current node) with the same parent.\nvar siblingsIncludingSelf: [Node]\n```\n\n### Position\n\n```swift\n/// Returns the distance between a node and the root.\nvar depth: Int\n```\n\n```swift\n/// The number of edges between the current node and the root.\nvar level: Int\n```\n\n### Textual representation\n\n```swift\nvar lineBasedDescription: String\n```\n\n\n## Example\n\n```swift\nlet root = SimpleNode(value: \"Apple\")\n\nlet desktops = SimpleNode(value: \"Desktops\")\nroot.addChild(node: desktops)\n\nlet macPro = SimpleNode(value: \"Mac Pro\")\ndesktops.addChild(node: macPro)\n\nlet macMini = SimpleNode(value: \"Mac Mini\")\ndesktops.addChild(node: macMini)\n\nlet iMac = SimpleNode(value: \"iMac\")\ndesktops.addChild(node: iMac)\n\nlet notebooks = SimpleNode(value: \"Notebooks\")\nroot.addChild(node: notebooks)\n\nlet macBookPro = SimpleNode(value: \"MacBook Pro\")\nnotebooks.addChild(node: macBookPro)\n\nlet devices = SimpleNode(value: \"Devices\")\nroot.addChild(node: devices)\n\nlet handhelds = SimpleNode(value: \"Handhelds\")\ndevices.addChild(node: handhelds)\n\nlet ipod = SimpleNode(value: \"iPod\")\nhandhelds.addChild(node: ipod)\n\nlet iphone = SimpleNode(value: \"iPhone\")\nhandhelds.addChild(node: iphone)\n\nlet newton = SimpleNode(value: \"Newton\")\nhandhelds.addChild(node: newton)\n\nlet setTopBoxes = SimpleNode(value: \"Set-top boxes\")\ndevices.addChild(node: setTopBoxes)\n\nlet appleTV = SimpleNode(value: \"Apple TV\")\nsetTopBoxes.addChild(node: appleTV)\n\n\nprint(root.lineBasedDescription)\n```\n\nOutput:\n\n```\nApple\n├── Desktops\n│   ├── Mac Pro\n│   ├── Mac Mini\n│   └── iMac\n├── Notebooks\n│   └── MacBook Pro\n└── Devices\n    ├── Handhelds\n    │   ├── iPod\n    │   ├── iPhone\n    │   └── Newton\n    └── Set-top boxes\n        └── Apple TV\n```\n\n\n## Other Projects\n\n* [BinaryKit](https://github.com/Cosmo/BinaryKit) — BinaryKit helps you to break down binary data into bits and bytes and easily access specific parts.\n* [Clippy](https://github.com/Cosmo/Clippy) — Clippy from Microsoft Office is back and runs on macOS! Written in Swift.\n* [GrammaticalNumber](https://github.com/Cosmo/GrammaticalNumber) — Turns singular words to the plural and vice-versa in Swift.\n* [HackMan](https://github.com/Cosmo/HackMan) — Stop writing boilerplate code yourself. Let hackman do it for you via the command line.\n* [ISO8859](https://github.com/Cosmo/ISO8859) — Convert ISO8859 1-16 Encoded Text to String in Swift. Supports iOS, tvOS, watchOS and macOS.\n* [SpriteMap](https://github.com/Cosmo/SpriteMap) — SpriteMap helps you to extract sprites out of a sprite map. Written in Swift.\n* [StringCase](https://github.com/Cosmo/StringCase) — Converts String to lowerCamelCase, UpperCamelCase and snake_case. Tested and written in Swift.\n* [TinyConsole](https://github.com/Cosmo/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.\n\n## License\n\nNodes is released under the [MIT License](http://www.opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcosmo%2Fnodes","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcosmo%2Fnodes","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcosmo%2Fnodes/lists"}