ssignment: Contains at minimum 20 nodes. All paths must have a cost between 1-15. You must use each cost at least once with the remaining costs of your choice in the same range. All nodes must be able to communicate with all other nodes (does not have to be a direct path) Each node can only connect to, at maximum, 5 other nodes. The cost of an extra hop is 1 1/2 tim
I need to construct a optimal binary search tree (OBST). I am using Python to create this code. I already started creating the binary search tree. I need help trying to implement the optimal binary search tree. I did the class and then I did the insertion next to add the nodes. There needs to be at least 20 nodes. After that, I'm stuck. Not sure what code is needed to add for optimal cost.
Criteria from my assignment:
- Contains at minimum 20 nodes.
- All paths must have a cost between 1-15. You must use each cost at least once with the remaining costs of your choice in the same range.
- All nodes must be able to communicate with all other nodes (does not have to be a direct path)
- Each node can only connect to, at maximum, 5 other nodes.
- The cost of an extra hop is 1 1/2 times the cost of the second path and 2 times the cost of a third path. Any path greater than 3 is at the same cost as the third.
This is what is required for us in the assignment.
My Code in Python:
class BST:
def __init__(self, value):
self.value = value
self.leftChild = None
self.rightChild = None
def insert(self, value):
# insertion node code
if self == self.value:
return
if self < self.value:
if self.leftChild:
self.leftChild.insert(value)
else:
self.leftChild = BST(value)
else:
# right node recursive function.
if self.rightChild:
self.rightChild.insert(value)
return
else:
self.rightChild = BST(value)
# in order post traversal - comment.
Questions:
What is the implementation code in Python for the optimal binary search tree?
Do I need to create a traversal first before doing the optimal cost? I don't think but I'm not sure.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps