-
Notifications
You must be signed in to change notification settings - Fork 16
/
scratch.py
41 lines (35 loc) · 1.04 KB
/
scratch.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
class BST:
def __init__(self, value, left=None, right=None):
self.value = value
self.left = left
self.right = right
def insert(self, value):
if value < self.value:
if self.left == None:
self.left = BST(value)
else:
self.left.insert(value)
elif value >= self.value:
if self.right == None:
self.right = BST(value)
else:
self.right.insert(value)
def deleteNodesAndReturnOrder(node, array):
if node.left is not None:
deleteNodesAndReturnOrder(node.left, array)
if node.right is not None:
deleteNodesAndReturnOrder(node.right, array)
array.append(node.value)
node = None
root = BST(10)
root.left = BST(5)
root.left.left = BST(2)
root.left.left.left = BST(1)
root.left.right = BST(5)
root.right = BST(15)
root.right.left = BST(13)
root.right.left.right = BST(14)
root.right.right = BST(22)
deletedNodes = []
deleteNodesAndReturnOrder(root, deletedNodes)
print(deletedNodes)