Skip to content

Trie tree implementation in Swift

License

Notifications You must be signed in to change notification settings

morishin/trie-swift

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

TrieSwift

Swift 5.1 SPM

Trie tree implementation in Swift.

Installation

https://developer.apple.com/documentation/xcode/adding_package_dependencies_to_your_app

Example

  1. Make tree

    let tree = TrieTree<Character, String>()
    tree.insertValue(for: "sea", value: "🌊")
    tree.insertValue(for: "seat", value: "💺")
    tree.insertValue(for: "sheep", value: "🐑")
    tree.insertValue(for: "she", value: "👩")
    tree.insertValue(for: "tea", value: "🍵")
    tree.insertValue(for: "triangle", value: "🔺")
    tree.insertValue(for: "tree", value: "🌳")
    tree.insertValue(for: "apple", value: "🍎")
  2. Tree traversal

    print(tree.nextState(key: "a")).value // => nil
    print(tree.nextState(key: "p")).value // => nil
    print(tree.nextState(key: "p")).value // => nil
    print(tree.nextState(key: "l")).value // => nil
    print(tree.nextState(key: "e")).value // => "🍎"

Visualize

TrieTree can generate PlantUML format text for debugging.

print(tree.generatePlantUMLString()) // => "@startuml ... @enduml"

The generated text can be rendered like the figure below.

plantuml

License

MIT

About

Trie tree implementation in Swift

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages