-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSearchRecursive.py
62 lines (55 loc) · 1.6 KB
/
SearchRecursive.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
class _Node:
__slots__ = '_element', '_left', '_right'
def __init__(self, element, left=None, right=None):
self._element = element
self._left = left
self._right = right
class BinarySearchTree:
def __init__(self):
self._root = None
def insert(self,troot,e):
temp = None
while troot:
temp = troot
if e == troot._element:
return
elif e < troot._element:
troot = troot._left
elif e > troot._element:
troot = troot._right
n = _Node(e)
if self._root:
if e < temp._element:
temp._left = n
else:
temp._right = n
else:
self._root = n
def rsearch(self,troot,key):
if troot:
if key == troot._element:
return True
elif key < troot._element:
return self.rsearch(troot._left,key)
elif key > troot._element:
return self.rsearch(troot._right,key)
else:
return False
def inorder(self,troot):
if troot:
self.inorder(troot._left)
print(troot._element,end=' ')
self.inorder(troot._right)
B = BinarySearchTree()
B.insert(B._root,50)
B.insert(B._root,30)
B.insert(B._root,80)
B.insert(B._root,10)
B.insert(B._root,40)
B.insert(B._root,60)
B.insert(B._root,90)
B.inorder(B._root)
print()
print(B.rsearch(B._root,60))
print(B.rsearch(B._root,30))
print(B.rsearch(B._root,70))