Skip to content

Commit a571352

Browse files
authored
Merge pull request #4128 from AKSHITHA-CHILUKA/patch-14
Create SegmentTree.md
2 parents 9f0f3d3 + addab49 commit a571352

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

dsa/Advance/SegmentTree.md

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
# Segment Tree
2+
3+
A Segment Tree is a data structure that allows efficient range queries and updates on an array. It is particularly useful for problems where you need to perform multiple range queries and updates on an array.
4+
5+
## Key Features
6+
7+
- **Space Complexity:** O(n)
8+
- **Time Complexity for Updates:** O(log n)
9+
- **Time Complexity for Queries:** O(log n)
10+
11+
## Algorithm Description
12+
13+
A Segment Tree is a binary tree where each node represents an interval or segment of the array. The tree allows efficient querying of aggregate information (such as sum, minimum, or maximum) over any segment of the array, as well as efficient updates to individual elements.
14+
15+
### Operations
16+
17+
1. **Build:** Construct the tree from the given array.
18+
2. **Update:** Update an element in the array and propagate the change through the tree.
19+
3. **Query:** Calculate the aggregate information over a specified range of the array.
20+
21+
## Python Implementation
22+
23+
Here is a Python implementation of a Segment Tree for range sum queries:
24+
25+
```python
26+
class SegmentTree:
27+
def __init__(self, data):
28+
self.n = len(data)
29+
self.tree = [0] * (2 * self.n)
30+
self.build(data)
31+
32+
def build(self, data):
33+
# Initialize leaves in the segment tree
34+
for i in range(self.n):
35+
self.tree[self.n + i] = data[i]
36+
# Build the tree by calculating parents
37+
for i in range(self.n - 1, 0, -1):
38+
self.tree[i] = self.tree[i * 2] + self.tree[i * 2 + 1]
39+
40+
def update(self, index, value):
41+
# Update the value at the leaf node
42+
index += self.n
43+
self.tree[index] = value
44+
# Propagate the update up the tree
45+
while index > 1:
46+
index //= 2
47+
self.tree[index] = self.tree[2 * index] + self.tree[2 * index + 1]
48+
49+
def query(self, left, right):
50+
# Query the sum in the range [left, right)
51+
result = 0
52+
left += self.n
53+
right += self.n
54+
while left < right:
55+
if left % 2:
56+
result += self.tree[left]
57+
left += 1
58+
if right % 2:
59+
right -= 1
60+
result += self.tree[right]
61+
left //= 2
62+
right //= 2
63+
return result
64+
```
65+
66+
# Example usage
67+
if __name__ == "__main__":
68+
data = [1, 3, 5, 7, 9, 11]
69+
seg_tree = SegmentTree(data)
70+
71+
print("Initial Segment Tree:", seg_tree.tree)
72+
73+
# Query the sum of range [1, 5)
74+
print("Sum of range [1, 5):", seg_tree.query(1, 5))
75+
76+
# Update index 2 to value 6
77+
seg_tree.update(2, 6)
78+
print("Segment Tree after update:", seg_tree.tree)
79+
80+
# Query again after the update
81+
print("Sum of range [1, 5) after update:", seg_tree.query(1, 5))
82+
# Explanation
83+
The SegmentTree class initializes the tree and provides methods for building the tree, updating elements, and querying ranges.
84+
The build method constructs the tree by initializing the leaves with the input data and then building the parent nodes.
85+
The update method updates an element at a given index and propagates the change up the tree.
86+
The query method calculates the sum of elements within a specified range by traversing the tree.
87+
88+
89+
## Conclusion
90+
Segment Trees are powerful data structures for handling range queries and updates efficiently. They are widely used in scenarios requiring frequent and dynamic range queries, such as in competitive programming, database indexing, and real-time data analysis.
91+

0 commit comments

Comments
 (0)