-
Notifications
You must be signed in to change notification settings - Fork 0
/
348.py
127 lines (104 loc) · 3.45 KB
/
348.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
"""
Problem:
A ternary search tree is a trie-like data structure where each node may have up to
three children. Here is an example which represents the words code, cob, be, ax, war
and we.
c
/ | \
b o w
/ | | |
a e d a
| / | | \
x b e r e
The tree is structured according to the following rules:
left child nodes link to words lexicographically earlier than the parent prefix
right child nodes link to words lexicographically later than the parent prefix
middle child nodes continue the current word
For instance, since code is the first word inserted in the tree, and cob
lexicographically precedes cod, cob is represented as a left child extending from cod.
Implement insertion and search functions for a ternary search tree.
"""
from random import shuffle, random
from typing import Optional
class Node:
def __init__(self, val: Optional[str] = None) -> None:
self.val = val
self.left = None
self.mid = None
self.right = None
def __bool__(self) -> bool:
return bool(self.val)
def insert_helper(self, string: str) -> None:
if not string:
return
if self.left is None:
self.left = Node()
if self.mid is None:
self.mid = Node()
if self.right is None:
self.right = Node()
char = string[0]
if not self:
self.val = char
self.mid.insert_helper(string[1:])
if self.val == char:
self.mid.insert_helper(string[1:])
elif self.val > char:
self.left.insert_helper(string)
else:
self.right.insert_helper(string)
def search_helper(self, string: str) -> bool:
if not string:
return True
char = string[0]
length = len(string)
if char == self.val:
if self.mid:
return self.mid.search_helper(string[1:])
elif length == 1:
return True
return False
elif char < self.val:
if self.left:
return self.left.search_helper(string)
return False
else:
if self.right:
return self.right.search_helper(string)
return False
class TernarySearchTree:
def __init__(self) -> None:
self.root = None
def insert(self, string: str) -> None:
if not string:
return
if not self.root:
self.root = Node(string[0])
string = string[1:]
curr = self.root
for char in string:
curr.mid = Node(char)
curr = curr.mid
else:
self.root.insert_helper(string)
def search(self, string: str) -> bool:
if not string:
return True
if not self.root:
return False
return self.root.search_helper(string)
if __name__ == "__main__":
words_present = ["ax", "be", "cob", "code", "war", "we"]
words_absent = ["axe", "bee", "creed", "hi", "see", "wax"]
chosen_words = words_absent + words_present
shuffle(chosen_words)
chosen_words = [word for word in chosen_words if random() > 0.5]
shuffle(chosen_words)
tree = TernarySearchTree()
for word in words_present:
tree.insert(word)
for word in chosen_words:
if tree.search(word):
print(f"'{word}' is PRESENT in the tree")
else:
print(f"'{word}' is NOT PRESENT in the tree")