Skip to main content

Segment Tree

SpaceTime
AccessLookupInsertionDeletion
O(n)O(log n)O(log n)O(log n)O(log n)

Definition​

Segment Tree is a data structure that allows efficient range queries on an array. It's built by recursively dividing the array into smaller segments and storing the results of operations on those segments.

Simplified

Segment Tree is like a librarian for data. It organizes data into segments for quick searches and updates, similar to how a librarian sorts books into sections for easy access. However, once the Segment Tree is built, it can't be modified. It's a tool for efficient data management and retrieval.

Practice​

AspectPseudo Code
Init
constructor(input_array, operation, operation_fallback):
input_array = input_array
operation = operation
operation_fallback = operation_fallback
segment_tree = init_segment_tree(input_array)

build_segment_tree()

init_segment_tree(input_array):
input_array_length = length(input_array)
if is_power_of_two(input_array_length):
segment_tree_array_length = (2 * input_array_length) - 1
else:
current_power = floor(log2(input_array_length))
next_power = current_power + 1
next_powerOfTwoNumber = 2^next_power
segment_tree_array_length = (2 * next_powerOfTwoNumber) - 1
return Array(segment_tree_array_length)

is_power_of_two(number):
if number < 1:
return False
divided_number = number
While divided_number ≠ 1:
if divided_number % 2 ≠ 0:
return False
divided_number /= 2
return True

build_segment_tree():
left_index = 0
right_index = input_array.length - 1
position = 0
build_tree_recursively(left_index, right_index, position)

build_tree_recursively(left_input_index, right_input_index, position):
if left_input_index = right_input_index:
segment_tree[position] = input_array[left_input_index]
return
middle_index = floor((left_input_index + right_input_index) / 2)
build_tree_recursively(left_input_index, middle_index, get_left_child_index(position))
build_tree_recursively(middle_index + 1, right_input_index, get_right_child_index(position))
segment_tree[position] = operation(
segment_tree[get_left_child_index(position)],
segment_tree[get_right_child_index(position)]
)

get_left_child_index(parentIndex):
return (2 * parentIndex) + 1

get_right_child_index(parentIndex):
return (2 * parentIndex) + 2
Range Query
range_query(query_left_index, query_right_index):
left_index = 0
right_index = input_array.length - 1
position = 0
return range_query_helper(query_left_index, query_right_index, left_index, right_index, position)

range_query_helper(query_left_index, query_right_index, left_index, right_index, position):
if query_left_index ≤ left_index and query_right_index ≥ right_index:
return segment_tree[position]
if query_left_index > right_index or query_right_index < left_index:
return operation_fallback
middle_index = floor((left_index + right_index) / 2)
left_operation_result = range_queryRecursive(
query_left_index, query_right_index, left_index, middle_index, get_left_child_index(position)
)
right_operation_result = range_queryRecursive(
query_left_index, query_right_index, middle_index + 1, right_index, get_right_child_index(position)
)
return operation(left_operation_result, right_operation_result)

get_left_child_index(parentIndex):
return (2 * parentIndex) + 1

get_right_child_index(parentIndex):
return (2 * parentIndex) + 2