Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#16 from gustavotiecker/feature/tree
Browse files Browse the repository at this point in the history
add basic tree implementation
  • Loading branch information
vbrazo authored May 10, 2021
2 parents 4876f96 + 19829d8 commit fb4d67d
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions trees/tree.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import Foundation

public class TreeNode<T> {
public var value: T

public weak var parent: TreeNode?
public var children = [TreeNode<T>]()

public init(value: T) {
self.value = value
}

public func addChild(_ node: TreeNode<T>) {
children.append(node)
node.parent = self
}
}

/* Checks the node's value property, if there is no match, check the child nodes.
Repeat the same process recursively */
extension TreeNode where T: Equatable {
func search(_ value: T) -> TreeNode? {
if value == self.value {
return self
}
for child in children {
if let found = child.search(value) {
return found
}
}
return nil
}
}

// The code below can be used for testing
let tree = TreeNode<String>(value: "animals")

let reptilesNode = TreeNode<String>(value: "reptiles")
let mammalsNode = TreeNode<String>(value: "mammals")

let lizardsNode = TreeNode<String>(value: "lizards")
let snakesNode = TreeNode<String>(value: "snakes")

let dogsNode = TreeNode<String>(value: "dogs")
let humansNode = TreeNode<String>(value: "humans")

tree.addChild(reptilesNode)
tree.addChild(mammalsNode)

reptilesNode.addChild(lizardsNode)
reptilesNode.addChild(snakesNode)

mammalsNode.addChild(dogsNode)
mammalsNode.addChild(humansNode)

print(tree.search("humans")?.value)
print(tree.search("lizards")?.value)
print(tree.search("dragons")?.value)

0 comments on commit fb4d67d

Please sign in to comment.