Prerequisite: Segment tree and depth first search.
In this article, an approach to convert an N-ary rooted tree( a tree with more than 2 children) into a segment tree is discussed which is used to perform a range update queries.
Why do we need a segment tree when we already have an n-ary rooted tree?
Many times, a situation occurs where the same operation has to be performed on multiple nodes and their subtrees along with query operations multiple times.
Let’s say that we have to perform N updates on different subtrees. Every operation can take up to O(N) time as it is an N-ary tree so overall complexity will be O(N^2) which is too slow to process more than 10^3 updates queries. So we have to go the other way around and we will build a segment tree for the same.
Approach: A depth first search is performed to walk through all the nodes and keep the track of the indexes of the subtree of every node in a converted array using two arrays tin and tout(which will be the range to do updates and queries). The DFS will perform a Euler walk. The idea is to create an array and add nodes to it in the order they get visited to the converted array.
Let’s see how the tin and tout arrays help in determining the range in the converted array.
Let N-ary rooted tree be:
real values on nodes: 1 2 2 1 4 3 6 converted arr(indexes): 1 2 3 5 6 7 4 Node 3 has three children 5, 6, 7. Therefore, the range of node 3 is index 3-6. NODE: RANGE(tin-tout) NODE 1: 1 - 7 NODE 2: 2 - 2 NODE 3: 3 - 6 NODE 5: 4 - 4 NODE 6: 5 - 5 NODE 7: 6 - 6 NODE 4: 7 - 7
Here, Node 1 has a range from 1-7 (all nodes) so the update and query will be performed on all the nodes. Leaf nodes like 2 which have no children will only update range 2-2(only itself) this proves that our range arrays tin and tout are correct. Similarly, tin and tout for all the nodes determine the range for query and update in the segment tree.
The following is the implementation of the approach:
sum at node 1: 19 sum at node 2: 2 sum at node 3: 15 sum at node 4: 1 sum at node 5: 4 sum at node 6: 3 sum at node 7: 6 After Update sum at node 1: 20 sum at node 2: 2 sum at node 3: 16 sum at node 4: 1 sum at node 5: 4 sum at node 6: 4 sum at node 7: 4
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Overview of Data Structures | Set 3 (Graph, Trie, Segment Tree and Suffix Tree)
- Cartesian tree from inorder traversal | Segment Tree
- Suffix Tree Application 4 - Build Linear Time Suffix Array
- Segment Tree | Set 1 (Sum of given range)
- Segment Tree | Set 2 (Range Minimum Query)
- Lazy Propagation in Segment Tree
- Longest Common Extension / LCE | Set 3 (Segment Tree Method)
- Reconstructing Segment Tree
- Segment Tree | Set 3 (XOR of given range)
- Segment tree | Efficient implementation
- LIS using Segment Tree
- Segment Tree | (XOR of a given range )
- Levelwise Alternating OR and XOR operations in Segment Tree
- Euler Tour | Subtree Sum using Segment Tree
- Two Dimensional Segment Tree | Sub-Matrix Sum
- Segment Tree | Set 2 (Range Maximum Query with Node Update)
- Levelwise Alternating GCD and LCM of nodes in Segment Tree
- Iterative Segment Tree (Range Maximum Query with Node Update)
- Iterative Segment Tree (Range Minimum Query)
- Counting inversions in an array using segment tree
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.