-
Notifications
You must be signed in to change notification settings - Fork 0
/
bin_search_tree.py
51 lines (40 loc) · 1.54 KB
/
bin_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
from __future__ import annotations
class Node():
def __init__(self, value, left: Node=None, right: Node=None):
self.value = value
self.left = left
self.right = right
self.parent: Node = None
def has_left_child(self) -> bool:
return bool(self.left)
def has_right_child(self) -> bool:
return bool(self.right)
def append(self, value) -> Node:
def append_to(child: str, value) -> Node:
if child not in ['left', 'right']:
raise f'Invalid child: {child}'
new_node = Node(value)
new_node.parent = self
setattr(self, child, new_node)
return new_node
if value <= self.value:
if not self.has_left_child():
node = append_to('left', value)
else:
node = self.left.append(value)
else:
if not self.has_right_child():
node = append_to('right', value)
else:
node = self.right.append(value)
return node
def print_tree(self):
queue = [self]
while queue != []:
node = queue.pop(0)
print(f'Node: {node.value} => left: { node.left.value if node.left != None else node.left }')
print(f'Node: {node.value} => right: { node.right.value if node.right != None else node.right }')
if type(node.left) == Node:
queue.append(node.left)
if type(node.right) == Node:
queue.append(node.right)