4101 Assignment 5-2
4101 Assignment 5-2
Homework Assignment #7
November 1, 2024
Answers
Answer 1: What is Stored in the RBT
The Red-Black Tree (RBT) stores:
1
Answer 4: Computing the Additional Fields Using Node
Information
Each node’s augmented fields are computed using its own value and the values
from its left and right children. Here’s how each field is computed:
1. Subtree Size
The subtree size of a node is 1 (itself) plus the sizes of its left and right
subtrees:
2. Sum of y-values
The sum of y-values in the subtree is the y-value of the node plus the sum
of y-values in its left and right subtrees:
2
Mean(a)
function Mean(a):
total_y = 0
total_count = 0
node = root
if total_count == 0:
return 0
return total_y / total_count
Explanation: Traverse the tree, summing y-values and counting nodes with
x < a. For nodes where x < a, add the left subtree’s sum y and size (if it exists)
to total y and total count, then add the current node’s y-value. Return the
mean y-value as total y/total count, or 0 if no nodes meet the condition.
Time Complexity: O(log n)
SD(a)
function SD(a):
total_y = 0
total_y_squared = 0
total_count = 0
node = root
3
node = node.left
if total_count == 0:
return 0
mean_y = total_y / total_count
variance = (total_y_squared / total_count) - (mean_y * mean_y)
return sqrt(variance)
• Search: O(log n)
• Mean: O(log n)
• SD: O(log n)