-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeset.py
109 lines (89 loc) · 2.63 KB
/
treeset.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
# ------------------------------------------------------------------------------
# treeset.py
#
#
# Copyright (C) 2016, Ryosuke Fukatani
# License: Apache 2.0
# ------------------------------------------------------------------------------
import bisect
class TreeSet(object):
"""
Binary-tree set like java Treeset.
Duplicate elements will not be added.
When added new element, TreeSet will be sorted automatically.
"""
def __init__(self, elements):
self._treeset = []
self.addAll(elements)
def addAll(self, elements):
for element in elements:
if element in self:
continue
self.add(element)
def add(self, element):
if element not in self:
bisect.insort(self._treeset, element)
def ceiling(self, e):
index = bisect.bisect_right(self._treeset, e)
if self[index - 1] == e:
return e
return self._treeset[bisect.bisect_right(self._treeset, e)]
def floor(self, e):
index = bisect.bisect_left(self._treeset, e)
if self[index] == e:
return e
else:
return self._treeset[bisect.bisect_left(self._treeset, e) - 1]
def __getitem__(self, num):
return self._treeset[num]
def __len__(self):
return len(self._treeset)
def clear(self):
"""
Delete all elements in TreeSet.
"""
self._treeset = []
def clone(self):
"""
Return shallow copy of self.
"""
return TreeSet(self._treeset)
def remove(self, element):
"""
Remove element if element in TreeSet.
"""
try:
self._treeset.remove(element)
except ValueError:
return False
return True
def __iter__(self):
"""
Do ascending iteration for TreeSet
"""
for element in self._treeset:
yield element
def pop(self, index):
return self._treeset.pop(index)
def __str__(self):
return str(self._treeset)
def __eq__(self, target):
if isinstance(target, TreeSet):
return self._treeset == target.treeset
elif isinstance(target, list):
return self._treeset == target
def __contains__(self, e):
"""
Fast attribution judgment by bisect
"""
try:
return e == self._treeset[bisect.bisect_left(self._treeset, e)]
except:
return False
if __name__ == '__main__':
ts = TreeSet([3, 7, 7, 1, 3])
print(ts.floor(4))
print(ts.ceiling(4))
print(ts.floor(3))
print(ts.ceiling(3))
print(ts)