Skip to content

Commit 73de706

Browse files
committed
Added find function
1 parent ef5f3cc commit 73de706

File tree

2 files changed

+109
-3
lines changed

2 files changed

+109
-3
lines changed

binary_search_tree/binary_serch_tree.py

+34
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,28 @@ def add(self, key, value):
4242
else:
4343
self._add(key, value)
4444

45+
def find(self, key):
46+
"""Return value by key.
47+
48+
Args:
49+
key: key for find value
50+
51+
Returns:
52+
value: value if key exist else None
53+
"""
54+
if self.is_empty():
55+
return None
56+
else:
57+
return self._find(key)
58+
4559
def _add(self, key, value):
4660
"""Add the given key and object to tree(iterative version).
4761
4862
Args:
4963
key: key for value
5064
value: value by key
5165
"""
66+
parent = self._root
5267
cur = self._root
5368
while cur:
5469
parent = cur
@@ -64,3 +79,22 @@ def _add(self, key, value):
6479
else:
6580
parent.right = new_node
6681
self._count += 1
82+
83+
def _find(self, key):
84+
"""Return value by key.
85+
86+
Args:
87+
key: key for find value
88+
89+
Returns:
90+
value: value if key exist else None
91+
"""
92+
cur = self._root
93+
while cur:
94+
if key > cur.key:
95+
cur = cur.right
96+
elif key < cur.key:
97+
cur = cur.left
98+
else:
99+
return cur.value
100+
return None

tests/test_binary_search_tree.py

+75-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,81 @@ def test_add_node1():
3131
def test_add_node2():
3232
bst = BinaryTree()
3333

34-
bst.add(29, 'Denis')
35-
bst.add(35, 'Dima')
36-
bst.add(28, 'Egor')
34+
bst.add(29, "Denis")
35+
bst.add(35, "Dima")
36+
bst.add(28, "Egor")
3737

3838
assert bst.is_empty() is False
3939
assert len(bst) == 3
40+
41+
42+
def test_add_node3():
43+
bst = BinaryTree()
44+
45+
bst.add(29, "Denis")
46+
bst.add(35, "Dima")
47+
bst.add(28, "Egor")
48+
bst.add(28, "E")
49+
50+
assert bst.is_empty() is False
51+
assert len(bst) == 3
52+
53+
54+
def test_find_value1():
55+
bst = BinaryTree()
56+
57+
result = bst.find("Key")
58+
assert result is None
59+
60+
61+
def test_find_value2():
62+
bst = BinaryTree()
63+
64+
bst.add(10, "Denis")
65+
66+
result = bst.find(10)
67+
assert result == "Denis"
68+
69+
70+
def test_find_value3():
71+
bst = BinaryTree()
72+
73+
bst.add(10, "Denis")
74+
bst.add(11, "Dima")
75+
bst.add(9, "Egor")
76+
77+
result = bst.find(11)
78+
assert result == "Dima"
79+
80+
81+
def test_find_value4():
82+
bst = BinaryTree()
83+
84+
bst.add(10, "Denis")
85+
bst.add(11, "Dima")
86+
bst.add(9, "Egor")
87+
88+
result = bst.find(9)
89+
assert result == "Egor"
90+
91+
92+
def test_find_value5():
93+
bst = BinaryTree()
94+
95+
bst.add(10, "Denis")
96+
bst.add(11, "Dima")
97+
bst.add(9, "Egor")
98+
99+
result = bst.find(11)
100+
assert result == "Dima"
101+
102+
103+
def test_find_value6():
104+
bst = BinaryTree()
105+
106+
bst.add(10, "Denis")
107+
bst.add(11, "Dima")
108+
bst.add(9, "Egor")
109+
110+
result = bst.find(13)
111+
assert result is None

0 commit comments

Comments
 (0)