Guest User

Untitled

a guest
Feb 22nd, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. class Node():
  2. def __init__(self):
  3. self.children = {} # mapping from character ==> Node
  4. self.value = None
  5.  
  6. def find(node, key):
  7. for char in key:
  8. if char in node.children:
  9. node = node.children[char]
  10. else:
  11. return None
  12. return node.value
  13.  
  14. def insert(root, string, value):
  15. node = root
  16. i = 0
  17. while i < len(string):
  18. if string[i] in node.children:
  19. node = node.children[string[i]]
  20. i += 1
  21. else:
  22. break
  23.  
  24. # append new nodes for the remaining characters, if any
  25. while i < len(string):
  26. node.children[string[i]] = Node()
  27. node = node.children[string[i]]
  28. i += 1
  29.  
  30. # store value in the terminal node
  31. node.value = value
Add Comment
Please, Sign In to add comment