-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathBSTSet.py
42 lines (30 loc) · 925 Bytes
/
BSTSet.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
from base import SetBase
from BST import BST
class BSTSet(SetBase):
def __init__(self):
self._bst = BST()
def get_size(self):
return self._bst.size()
def is_empty(self):
return self._bst.is_empty()
def add(self, e):
return self._bst.add(e)
def contains(self, e):
return self._bst.contains(e)
def remove(self, e):
return self._bst.remove(e)
if __name__ == '__main__':
words = ''
with open('./shakespeare.txt', 'r') as f:
words = f.read()
words = words.split()
from time import time
start_time = time()
bst_set = BSTSet()
for word in words:
bst_set.add(word)
print('Total words: ', len(words))
print('Unique words: ', bst_set.get_size())
print('Contains word "they": ', bst_set.contains('they'))
## 耗时0.58秒左右
print('Total time: {} seconds'.format(time() - start_time))