- Related Questions & Answers
- Splay trees in Data Structure
- Solid Trees in Data Structure
- Range Trees in Data Structure
- BSP Trees in Data Structure
- Huffman Trees in Data Structure
- R-trees in Data Structure
- Interval Trees in Data Structure
- Tournament Trees, Winner Trees and Loser Trees in Data Structure
- Optimal Lopsided Trees in Data Structure
- Threaded Binary Trees in Data Structure
- Red-Black Trees in Data Structure
- Comparison of Search Trees in Data Structure
- Dynamic Finger Search Trees in Data Structure
- Height Limited Huffman Trees in Data Structure
- Level Linked (2,4)-Trees in Data Structure

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

In this section we will see what is the segment tree. Before discussing that, let us see one problem.

Suppose we have an array arr[0,…,n-1], We can do following operations −

Find the sum of elements from index l to r, where 0 ≤ l ≤ r ≤ n-1

Change the value of a specified element of the array to a new value x. We need to do arr[i] = x. The i in range 0 to n – 1.

We can solve this problem by using the Segment tree. The segment tree can help us to get the sum and query in O(log n) time. So let us see how to represent this −

Leaf nodes are the elements of the given array

Each internal nodes are representing some merging of leaf nodes. The merging may be different in different cases. Here merging is the sum of leaves under a node.

Suppose we have an array like [1, 3, 5, 7, 9, 11]. So the segment tree will be

Advertisements