
- Data Structures & Algorithms
- DSA - Home
- DSA - Overview
- DSA - Environment Setup
- Algorithm
- DSA - Algorithms Basics
- DSA - Asymptotic Analysis
- DSA - Greedy Algorithms
- DSA - Divide and Conquer
- DSA - Dynamic Programming
- Data Structures
- DSA - Data Structure Basics
- DSA - Array Data Structure
- Stack & Queue
- DSA - Stack
- DSA - Expression Parsing
- DSA - Queue
- Searching Techniques
- DSA - Linear Search
- DSA - Binary Search
- DSA - Interpolation Search
- DSA - Hash Table
- Sorting Techniques
- DSA - Sorting Algorithms
- DSA - Bubble Sort
- DSA - Insertion Sort
- DSA - Selection Sort
- DSA - Merge Sort
- DSA - Shell Sort
- DSA - Quick Sort
- Graph Data Structure
- DSA - Graph Data Structure
- DSA - Depth First Traversal
- DSA - Breadth First Traversal
- Tree Data Structure
- DSA - Tree Data Structure
- DSA - Tree Traversal
- DSA - Binary Search Tree
- DSA - AVL Tree
- DSA - Spanning Tree
- DSA - Heap
- DSA Useful Resources
- DSA - Questions and Answers
- DSA - Quick Guide
- DSA - Useful Resources
- DSA - Discussion
Segment Trees in Data Structure
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
- Related Articles
- Huffman Trees in Data Structure
- Splay trees in Data Structure
- Solid Trees in Data Structure
- Range Trees in Data Structure
- BSP 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
- Level Linked (2,4)-Trees in Data Structure
- Randomized Finger Search Trees in Data Structure
