code
code
def __repr__(self):
return f'TreeNode(id={self.id}, data={self.data})'
def build_tree(node_dict):
if not node_dict:
return None
node = TreeNode(node_dict['id'], node_dict['data'])
if 'left' in node_dict:
node.left = build_tree(node_dict['left'])
if 'right' in node_dict:
node.right = build_tree(node_dict['right'])
return node
def find_highest_data_path(node):
if not node:
return []
if not node.left and not node.right:
return [node]
left_path = find_highest_data_path(node.left)
right_path = find_highest_data_path(node.right)
if sum(n.data for n in left_path) > sum(n.data for n in right_path):
return [node] + left_path
else:
return [node] + right_path
input_data = {
"id": "SE1",
"data": 10,
"left": {
"id": "SE2",
"data": 12,
"left": {
"id": "SE4",
"data": 4,
"right": {
"id": "SE8",
"data": 9,
"left": {
"id": "SE15",
"data": 20
}
}
},
"right": {
"id": "SE5",
"data": 17,
"left": {
"id": "SE9",
"data": 10,
"left": {
"id": "SE16",
"data": 8,
"right": {
"id": "SE17",
"data": 18
}
}
},
"right": {
"id": "SE10",
"data": 11
}
}
},
"right": {
"id": "SE3",
"data": 7,
"left": {
"id": "SE6",
"data": 6,
"left": {
"id": "SE11",
"data": 18
},
"right": {
"id": "SE12",
"data": 2
}
},
"right": {
"id": "SE7",
"data": 3,
"left": {
"id": "SE13",
"data": 13
},
"right": {
"id": "SE14",
"data": 15
}
}
}
}
root = build_tree(input_data)
highest_path = find_highest_data_path(root)
subordinates_count = {
"SE1": 5,
"SE2": 4,
"SE5": 3,
"SE9": 2,
"SE16": 0,
"SE17": 0
}
total_bonus = 3000000
sales_bonus_share = 0.75 * total_bonus
subordinate_bonus_share = 0.25 * total_bonus
total_subordinates = sum(subordinates_count[node_id] for node_id, _ in result)
total_sales = sum(node_data for _, node_data in result)
bonus_distribution = []
for node_id, node_data in result:
subordinate_efficiency = subordinates_count.get(node_id, 0) /
total_subordinates if total_subordinates > 0 else 0
subordinate_bonus = subordinate_efficiency * subordinate_bonus_share
sales_efficiency = node_data / total_sales if total_sales > 0 else 0
sales_bonus = sales_efficiency * sales_bonus_share
print("Bonus distribution:")
print(bonus_distribution)