Skip to content

Commit 7ee4342

Browse files
author
Jimmy Y
committed
Elaborated BST
1 parent fa025c3 commit 7ee4342

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

dynamic_programming/optimal_bst.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/python3
22

33
# This Python program provides O(n^2) dynamic programming solution
4-
# to an optimal BST problem.
4+
# to an optimal binary search tree (abbreviated BST) problem.
55
#
66
# The goal of the optimal BST problem is to build a low-cost BST for a
77
# given set of nodes, each with its own key and frequency. The frequency
@@ -25,7 +25,7 @@ def __init__(self, key, freq):
2525
self.freq = freq
2626

2727

28-
def print_BST(root, key, i, j, parent, is_left):
28+
def print_binary_search_tree(root, key, i, j, parent, is_left):
2929
"""Recursive function to print a BST from a root table."""
3030
if i > j or i < 0 or j > len(root) - 1:
3131
return
@@ -39,15 +39,15 @@ def print_BST(root, key, i, j, parent, is_left):
3939
else:
4040
print(f"{key[root[i][j]]} is the right child of key {parent}.")
4141

42-
print_BST(
42+
print_binary_search_tree(
4343
root, key, i, root[i][j] - 1, key[root[i][j]], True
4444
) # recur to left child
45-
print_BST(
45+
print_binary_search_tree(
4646
root, key, root[i][j] + 1, j, key[root[i][j]], False
4747
) # recur to right child
4848

4949

50-
def find_optimal_BST(nodes):
50+
def find_optimal_binary_search_tree(nodes):
5151
"""
5252
Precondition: Node keys are sorted in an increasing order.
5353
@@ -97,7 +97,7 @@ def find_optimal_BST(nodes):
9797
root[i][j] = r
9898

9999
print(f"The cost of optimal BST is {dp[0][n - 1]}.")
100-
print_BST(root, key, 0, n - 1, -1, False)
100+
print_binary_search_tree(root, key, 0, n - 1, -1, False)
101101

102102

103103
def main():
@@ -108,7 +108,7 @@ def main():
108108
# increasing order and rearrange its frequencies accordingly.
109109
nodes.sort(key=lambda node: node.key)
110110

111-
find_optimal_BST(nodes)
111+
find_optimal_binary_search_tree(nodes)
112112

113113

114114
if __name__ == "__main__":

0 commit comments

Comments
 (0)