-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrees.py
122 lines (102 loc) · 3.23 KB
/
trees.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
class BST:
"""
Create an empty BST
"""
class Node:
"""
Create a node class to hold the data and references to other data
"""
def __init__(self, data):
# set the data to be the root. Links are None
# because they are unkown.
self.data = data
self.left = None
self.right = None
def __init__(self):
# Set the root to be None because it is empty
self.root = None
def insert(self, data):
# Insert data into Tree
if self.root is None:
self.root = BST.Node(data)
else:
self._insert(data, self.root)
def _insert(self, data, node):
"""
Determine where to insert the data into the tree
This uses recursion to go to the next line of the tree if
there is not an open branch.
"""
if data < node.data:
if node.left is None:
node.left = BST.Node(data)
else:
self._insert(data, node.left)
elif data > node.data:
if node.right is None:
node.right = BST.Node(data)
else:
self._insert(data, node.right)
# iter function to run through tree
def __iter__(self):
"""
Go forward through the tree
"""
yield from self._traverse_forward(self.root)
# traverse forward through the tree
def _traverse_forward(self, node):
"""
This runs through the tree on either side, and gets data until
there is not anymore to find. It starts on the left side to get
the smaller values, then the root value, and then the bigger
values.
"""
if node is not None:
yield from self._traverse_forward(node.left)
yield node.data
yield from self._traverse_forward(node.right)
# Runs through the tree backwards
def __reversed__(self):
"""
Go backward through the tree
"""
yield from self._traverse_backward(self.root)
# traverse backward through the tree
def _traverse_backward(self, node):
"""
This runs through the tree on either side, and gets data until
there is not anymore to find. It starts on the right side to get
the highest values, then the root value, and then the smaller
values.
"""
if node is not None:
yield from self._traverse_backward(node.right)
yield node.data
yield from self._traverse_backward(node.left)
print("\n=========== PROBLEM 1 TESTS ===========")
tree = BST()
# extra tests
tree.insert(5)
tree.insert(5)
tree.insert(5)
# given tests
tree.insert(5)
tree.insert(3)
tree.insert(7)
# After implementing 'no duplicates' rule,
# this next insert will have no effect on the tree.
tree.insert(7)
tree.insert(10)
tree.insert(1)
for x in tree:
print(x) # 1, 3, 5, 7, 10
print("\n=========== PROBLEM 2 TESTS ===========")
print(3 in tree) # True
print(4 in tree) # False
print(5 in tree) # True
print(10 in tree) # True
print(11 in tree) # False
print(0 in tree) # False
print("\n=========== PROBLEM 3 TESTS ===========")
for x in reversed(tree):
print(x) # 10, 7, 5, 3, 1