Question: please use python to finish code node.py and tree.py Maintain a tree where each node in the tree holds a gold value as its key,

please use python to finish code node.py and tree.py
Maintain a tree where each node in the tree holds a gold value as its key, as well as the property k_sum. The value of the "k_sum" is equivalent to the summed gold value of the k highest gold values in the subtree of the node.
Example with k =1:(see picture1)
Move Subtree
You must also support the move_subtree(node_a, node_b) function, where the subtree rooted at node_a is made into a child of node_b. If node_b already has children, your function should make node_a the last child. You must ensure that the k_sum property is correct for all nodes, after moving the subtree. You can assume that node_b isnt in the subtree of node_a and you dont have to check for this.
Example:(see picture1)
Melt Subtree
Your tree must support the melt_subtree(node_a) operation. When called, the function removes the subtree rooted at node_a and update the gold value of node_a with the sum of the gold in its (removed) subtree. You must ensure that the k_sum property is correct for all nodes, after moving the subtree.
Example:(see picture2)
TO IMPLEMENT:
You will need to implement these functions:
node.py
__init__
add_child
update_gold
return_k_sum
tree.py
put
move_subtree
melt_subtree
(Note, you can add additional functions and variables to the classes as you see fit, so feel free to modify and extend those as long as you leave the existing function signatures and variables intact.)
Code:
Node.py:
class Node():
# These are the defined properties as described above
children: list['Node']
parent: 'Node'
gold: int
k_sum: int
k: int
def __init__(self, gold: int, k: int)-> None:
"""
The constructor for the Node class.
:param gold: The gold of the node.
:param k: Value used to calculate k_sum.
"""
# TODO Initialize the properties of the node
def add_child(self, node: 'Node')-> None:
"""
Adds the given node as a child of the current node.
The given node is guaranteed to be new and not a child of any other node.
:param node: The node to add as the child
"""
# TODO Add the given node as the right child of the current node
def is_external(self)-> bool:
"""
Returns True if the node is a leaf node.
:return: True if the node is a leaf node.
"""
return len(self.children)==0
def update_gold(self, gold: int)-> None:
"""
Updates the gold of the current node.
:param gold: The new gold of the node.
"""
# TODO Update the gold of the node
def get_children(self)-> list['Node']:
"""
Returns the children of the current node.
:return: The children of the current node.
"""
return self.children
def return_k_sum(self)-> int:
"""
Returns the k_sum of the current node.
:return: The k_sum of the current node.
"""
# TODO Return the k_sum of the node
-----------------------------------
Tree.py
from Node import Node
class Tree():
# These are the defined properties as described above
root: Node
def __init__(self, root: Node = None)-> None:
"""
The constructor for the Tree class.
:param root: The root node of the Tree.
"""
self.root = root
def put(self, node_a: Node, node_b: Node)-> None:
"""
Adds node_b as the last child of node_a.
You are guranteed that the given node is not already part of the tree
:param node_a: The node to add the child to.
:param node_b: The child to add to the node.
"""
# TODO Add the child to the node
def move_subtree(self, node_a: Node, node_b: Node)-> None:
"""
The subtree rooted at node_a is made into a child of node_b.
If node_b already has children, your function should make node_a the last child.
You must ensure that the k_sum property is correct for all nodes, after moving the subtree.
You can assume that node_b isn't in the subtree of node_a and you don't have to check for this.
:param node_a: The root of the subtree to move.
:param node_b: The node to add the subtree to.
"""
# TODO Move the subtree rooted at node_a to the last child of node_b
def melt_subtree(self, node_a)-> None:
"""
Removes the subtree rooted at node_a and updates the gold value of node_a with the sum of the gold in its (removed) subtree.
You must ensure that the k_sum property is correct for all nodes, after removing the subtree and updating the gold value.
"""
# TODO Remove the subtree, update the gold value of node_a and update k_sum values
please use python to finish code node.py and

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!