I know it’s silly but can anyone explain the part
"Pre-order traversal of the tree gives numbering to vertices such that all descendants of any internal node get consecutive numbers, and form a single range. This is because after visiting any vertex, dfs first visits all descendants of the vertex, and then all the remaining vertices.
Therefore, once we arrange vertices by their preorder traversal order, all descendants of any node become a subarray of this array. "
So if you look at a node say ‘b’ for example , ‘b’ and its descendants form the range 2 to 4 . Similarly , ‘c’ and its descendants form the range 5 to 7 . ‘f’ and its descendants form the range 6 to 6 and ‘a’ and its descendants form the range 1-7 .
Because when DFS visits a node , it next visits the entire subtree rooted at that node and only then proceeds to visit anything else . Hence the entire subtree gets “NAMING” or “ORDERING” or “RANKING” as a single continuous RANGE .
@vineetpaliwal
I understood that sir, but what i wanted to know was that how did we limit those number of descendents.
For example, in your case how do we decide that the descendents vary from 2 to 4 for ‘b’ and that this would be the limit(because we can lets say its 2 to 5, all are consecutive numbers here also) I didn’t understand how do we decide the sub-array’s size.
I used Post-order: http://en.wikipedia.org/wiki/Tree_traversal
I think it’s similar.
The example on wiki gives order:
A, C, E, D, B, H, I, G, F
1, 2, 3, 4, 5, 6, 7, 8, 9
note that F is the root (e.g. chef 0). So from 1 to 8 will get all the chefs below chef F.
chef G (8) has chefs I & H below. So we get the interval 6-7 for chef G.
func order(node int) {
chefs[node].a = order_count
for _, child := range chefs[node].children {
order(child)
}
chefs[node].b = order_count - 1
chefs[node].order = order_count
order_count++
}
I still cannot understand the analyse of the time complexities of the online methods, which is O(N lof N + Q log N) using segment tree.
As for the update operation in segment tree structure, we use the tree[root].health-of-least-healthy-chef to tell if we should continue to update the subtree to update the health value of those who died. I think this still needs to update too many nodes.
For all, I do not know how to analyse the time complexity when it comes to segment tree. Anyone helps?
I made some modifications in the pre-order traversal function. Basically it is just counting number of descendants of a root node. First find the number of descendants in the left subtree. then find those in the right subtree.if position of root is i, then subarray will be from i+1 to nodes in left subtree + nodes in right.
Does every chef will have only 2 immediate subordinate chefs? because in this pseudocode you took 2 children only :-
propagate the value of decrement to both children, set decrement of root to 0 remove-dead-chefs(root * 2); // some dead chef exists and needs to be removed remove-dead-chefs (root * 2+1); // so check both children
How could you remove dead chef and lazy propagate in O(logn) time? You are visiting each each child once by dfs. Isn’t its complexity O(n)
Read about the part where the tree is first converted into a list.
the parameter ‘health-of-least-healthy-chef’ helps me figure out in O(1) time if any child of a given node is dead, so I can remove dead chefs in O(log n) time per death.