Skip to content

Instantly share code, notes, and snippets.

@Mintri1199
Created May 16, 2019 22:33
Show Gist options
  • Save Mintri1199/d405d45a4d1cee431ca2dbe827ea9eae to your computer and use it in GitHub Desktop.
Save Mintri1199/d405d45a4d1cee431ca2dbe827ea9eae to your computer and use it in GitHub Desktop.
class DecimalSearchTree{
...
func insert(number: String, data: Any) {
// Insert the data in order of the number to the Decimal Search Tree
// recursively.
insertNodeRecursive(number: number, data: data, node: self.root)
}
func insertNodeRecursive(number: String, data: Any, node: DecimalTreeNode) {
// Helper function for insert
// Check if the program has done traversing
if number.count == 0 {
// Insert the data is there aren't any
if node.data == nil {
node.data = data
self.usedSize += 1
}
return
}
let nextIndex = Int(String(number.prefix(1)))
let remainder = String(number.dropFirst())
if node.next[nextIndex!] == nil {
node.next[nextIndex!] = DecimalTreeNode(data: nil)
self.size += 10
}
insertNodeRecursive(number: remainder, data: data, node: node.next[nextIndex!]!)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment