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 ksum. The value of the ksum" is equivalent to the summed gold value of the k highest gold values in the subtree of the node.
Example with k :see picture
Move Subtree
You must also support the movesubtreenodea nodeb function, where the subtree rooted at nodea is made into a child of nodeb If nodeb already has children, your function should make nodea the last child. You must ensure that the ksum property is correct for all nodes, after moving the subtree. You can assume that nodeb isnt in the subtree of nodea and you dont have to check for this.
Example:see picture
Melt Subtree
Your tree must support the meltsubtreenodea operation. When called, the function removes the subtree rooted at nodea and update the gold value of nodea with the sum of the gold in its removed subtree. You must ensure that the ksum property is correct for all nodes, after moving the subtree.
Example:see picture
TO IMPLEMENT:
You will need to implement these functions:
node.py
init
addchild
updategold
returnksum
tree.py
put
movesubtree
meltsubtree
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: listNode
parent: 'Node'
gold: int
ksum: int
k: int
def initself gold: int, k: int None:
The constructor for the Node class.
:param gold: The gold of the node.
:param k: Value used to calculate ksum.
# TODO Initialize the properties of the node
def addchildself 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 isexternalself bool:
Returns True if the node is a leaf node.
:return: True if the node is a leaf node.
return lenselfchildren
def updategoldself 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 getchildrenself listNode:
Returns the children of the current node.
:return: The children of the current node.
return self.children
def returnksumself int:
Returns the ksum of the current node.
:return: The ksum of the current node.
# TODO Return the ksum of the node
Tree.py
from Node import Node
class Tree:
# These are the defined properties as described above
root: Node
def initself root: Node None None:
The constructor for the Tree class.
:param root: The root node of the Tree.
self.root root
def putself nodea: Node, nodeb: Node None:
Adds nodeb as the last child of nodea
You are guranteed that the given node is not already part of the tree
:param nodea: The node to add the child to
:param nodeb: The child to add to the node.
# TODO Add the child to the node
def movesubtreeself nodea: Node, nodeb: Node None:
The subtree rooted at nodea is made into a child of nodeb
If nodeb already has children, your function should make nodea the last child.
You must ensure that the ksum property is correct for all nodes, after moving the subtree.
You can assume that nodeb isn't in the subtree of nodea and you don't have to check for this.
:param nodea: The root of the subtree to move.
:param nodeb: The node to add the subtree to
# TODO Move the subtree rooted at nodea to the last child of nodeb
def meltsubtreeself nodea None:
Removes the subtree rooted at nodea and updates the gold value of nodea with the sum of the gold in its removed subtree.
You must ensure that the ksum property is correct for all nodes, after removing the subtree and updating the gold value.
# TODO Remove the subtree, update the gold value of nodea and update ksum values
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
