ImmutableTrees
TCO '03 Semifinals 1 · 2003-10-07 · by vorthys
Problem Statement
Note to plugin users: There is an image in this problem statement. Please use the applet to view it.
An insertion into an immutable binary search tree does not alter the existing tree. Rather, it creates a new tree that contains the new value. For example, if a tree contains the values 1, 2, and 3, and you insert the value 4, then the new tree contains the values 1, 2, 3, and 4, but the old tree still exists and still contains only the values 1, 2, and 3. You could next insert, say, 5 into the new tree (obtaining a tree containing 1, 2, 3, 4, and 5) or into the old tree (obtaining a tree containing 1, 2, 3, and 5).
The simplest technique for implementing insertion into an immutable tree is called path-copying, because the entire path from the root to the site of the insertion is copied. Nodes not on this path are shared by the two trees. For example, if we insert 6 into the tree on the left in the picture below, we get the tree on the right.
If we start with an empty tree and perform N insertions, then we end up with a total of N non-empty trees. However, some of those trees may now be garbage, meaning that they are no longer needed and can be deallocated. When we deallocate a tree, we deallocate all of its nodes, except for those nodes that also belong to trees that are not garbage. You must determine how many nodes remain after deallocating all nodes that can be deallocated.
A series of insertions is represented by a
For example, the inputs
values = { 8, 6, 4, 7, 9, 1 }
trees = { -1, 0, 1, 0, -1, 2 }
correspond to the following series of insertions:
tree0 = insert(8,empty)
tree1 = insert(6,tree0)
tree2 = insert(4,tree1)
tree3 = insert(7,tree0)
tree4 = insert(9,empty)
tree5 = insert(1,tree2)
The
Constraints
- values contains between 1 and 50 elements, inclusive.
- trees contains the same number of elements as values.
- Each element of values is between 1 and 1000, inclusive.
- values contains no duplicate elements.
- Element I of trees is between -1 and I-1, inclusive.
- garbage contains between 0 and N elements, inclusive, where N is the number of elements in values.
- Each element of garbage is between 0 and N-1, inclusive.
- garbage contains no duplicate elements.
{ 1,2,3,4,5,6,7,8,9 }
{ -1,0,1,2,3,4,5,6,7 }
{}
Returns: 45
{ 3, 2, 5, 4, 1, 6 }
{-1, 0, 1, 2, 3, 4 }
{ 0, 1, 2, 3 }
Returns: 8
The first example above.
{ 8, 6, 4, 7, 9, 1 }
{-1, 0, 1, 0,-1, 2 }
{ 3, 1, 5}
Returns: 5
The second example above.
{ 1, 2, 3, 4 }
{-1, 0, 1, 0 }
{ 0, 1, 2, 3 }
Returns: 0
Everything is garbage.
{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50}
{-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,
30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48}
{}
Returns: 1275
Submissions are judged against all 70 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class ImmutableTrees with a public method int numNodes(vector<int> values, vector<int> trees, vector<int> garbage) · 70 test cases · 2 s / 256 MB per case