File tree Expand file tree Collapse file tree 1 file changed +31
-4
lines changed Expand file tree Collapse file tree 1 file changed +31
-4
lines changed Original file line number Diff line number Diff line change 19
19
import sys
20
20
from random import randint
21
21
22
+ flags = {
23
+ 1 : False ,
24
+ 2 : False ,
25
+ 3 : False ,
26
+ 4 : False ,
27
+ 5 : False ,
28
+ 6 : False
29
+ }
22
30
23
31
class Node :
24
32
"""Binary Search Tree Node"""
@@ -118,13 +126,29 @@ def find_optimal_binary_search_tree(nodes):
118
126
# Apply Knuth's optimization
119
127
# Loop without optimization: for r in range(i, j + 1):
120
128
for r in range (root [i ][j - 1 ], root [i + 1 ][j ] + 1 ): # r is a temporal root
121
- left = dp [i ][r - 1 ] if r != i else 0 # optimal cost for left subtree
122
- right = dp [r + 1 ][j ] if r != j else 0 # optimal cost for right subtree
129
+ # optimal cost for left subtree
130
+ if r != i : # ID: 1
131
+ flags [1 ] = True
132
+ left = dp [i ][r - 1 ]
133
+ else : # ID: 2
134
+ flags [2 ] = True
135
+ left = 0
136
+ # optimal cost for right subtree
137
+ if r != j : # ID: 3
138
+ flags [3 ] = True
139
+ right = dp [r + 1 ][j ]
140
+ else : # ID: 4
141
+ flags [4 ] = True
142
+ right = 0
143
+
123
144
cost = left + total [i ][j ] + right
124
145
125
- if dp [i ][j ] > cost :
146
+ if dp [i ][j ] > cost : # ID: 5
147
+ flags [5 ] = True
126
148
dp [i ][j ] = cost
127
149
root [i ][j ] = r
150
+ else : # ID: 6
151
+ flags [6 ] = True
128
152
129
153
print ("Binary search tree nodes:" )
130
154
for node in nodes :
@@ -141,4 +165,7 @@ def main():
141
165
142
166
143
167
if __name__ == "__main__" :
144
- main ()
168
+ import doctest
169
+ doctest .testmod ()
170
+
171
+ print (flags )
You can’t perform that action at this time.
0 commit comments