-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdigital_search_tree.py
64 lines (57 loc) · 1.6 KB
/
digital_search_tree.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
class Node:
def __init__(self, val):
self.val = val
self.left = None
self.right = None
class BinarySearchTree:
def __init__(self):
self.root = None
def insert(self, val):
new_node = Node(val)
if self.root is None:
self.root = new_node
else:
curr = self.root
i = 0
while i < 4:
if val[i] == '0':
if curr.left is None:
curr.left = new_node
break
else:
curr = curr.left
elif val[i] == '1':
if curr.right is None:
curr.right = new_node
break
else:
curr = curr.right
i += 1
def search(self, val):
curr = self.root
i = 0
while curr is not None:
if val == curr.val:
return True
elif val[i] == '0':
curr = curr.left
else:
curr = curr.right
i += 1
return False
# Create a new binary search tree
bst = BinarySearchTree()
# Insert some 4-bit binary numbers
bst.insert("0000")
bst.insert("0001")
bst.insert("0010")
bst.insert("1110")
bst.insert("1111")
# Search for a binary number
print(bst.search("0101")) # False
print(bst.search("0000")) # True
print(bst.search("0001")) # True
print(bst.search("1111")) # True
print(bst.search("0100")) # False
print(bst.search("1010")) # Flase
print(bst.search("11111")) # False