Created
May 16, 2019 22:33
-
-
Save Mintri1199/d405d45a4d1cee431ca2dbe827ea9eae to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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