Skip to content

Commit 8a29b3c

Browse files
committed
Added __getitem__
1 parent a39c73f commit 8a29b3c

File tree

2 files changed

+42
-12
lines changed

2 files changed

+42
-12
lines changed

binary_search_tree/binary_serch_tree.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,17 @@ def __len__(self):
2121
"""
2222
return self._count
2323

24+
def __getitem__(self, key):
25+
"""Return value bu key.
26+
27+
Args:
28+
key: key for find value
29+
30+
Returns:
31+
value: value if key exist else None
32+
"""
33+
return self.find(key)
34+
2435
def is_empty(self):
2536
"""Return True if tree is empty.
2637

tests/test_binary_search_tree.py

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -54,17 +54,24 @@ def test_add_node3():
5454
def test_find_value1():
5555
bst = BinaryTree()
5656

57-
result = bst.find('Key')
58-
assert result is None
57+
result1 = bst.find('Key')
58+
result2 = bst['key']
59+
60+
assert result1 is None
61+
assert result2 is None
62+
5963

6064

6165
def test_find_value2():
6266
bst = BinaryTree()
6367

6468
bst.add(10, 'Denis')
6569

66-
result = bst.find(10)
67-
assert result == 'Denis'
70+
result1 = bst.find(10)
71+
result2 = bst.find(10)
72+
73+
assert result1 == 'Denis'
74+
assert result2 == 'Denis'
6875

6976

7077
def test_find_value3():
@@ -74,8 +81,11 @@ def test_find_value3():
7481
bst.add(11, 'Dima')
7582
bst.add(9, 'Egor')
7683

77-
result = bst.find(11)
78-
assert result == 'Dima'
84+
result1 = bst.find(11)
85+
result2 = bst[11]
86+
87+
assert result1 == 'Dima'
88+
assert result2 == 'Dima'
7989

8090

8191
def test_find_value4():
@@ -85,8 +95,11 @@ def test_find_value4():
8595
bst.add(11, 'Dima')
8696
bst.add(9, 'Egor')
8797

88-
result = bst.find(9)
89-
assert result == 'Egor'
98+
result1 = bst.find(9)
99+
result2 = bst[9]
100+
101+
assert result1 == 'Egor'
102+
assert result2 == 'Egor'
90103

91104

92105
def test_find_value5():
@@ -96,8 +109,11 @@ def test_find_value5():
96109
bst.add(11, 'Dima')
97110
bst.add(9, 'Egor')
98111

99-
result = bst.find(11)
100-
assert result == 'Dima'
112+
result1 = bst.find(11)
113+
result2 = bst[11]
114+
115+
assert result1 == 'Dima'
116+
assert result2 == 'Dima'
101117

102118

103119
def test_find_value6():
@@ -107,8 +123,11 @@ def test_find_value6():
107123
bst.add(11, 'Dima')
108124
bst.add(9, 'Egor')
109125

110-
result = bst.find(13)
111-
assert result is None
126+
result1 = bst.find(13)
127+
result2 = bst[13]
128+
129+
assert result1 is None
130+
assert result2 is None
112131

113132

114133
def test_min1():

0 commit comments

Comments
 (0)