I'm looking for some help on a specific augmented Red Black Binary Tree. My goal is to make every single operation run in O(log(n))
in the worst case. The nodes of the tree will have an integer as there key. This integer can not be negative, and the tree should be sorted by a simple compare function off of this integer. Additionally, each node will also store another value: its power. (Note that this has nothing to do with mathematical exponents). Power is a floating point value. Both power and key are always non-negative. The tree must be able to provide these operations in O(log(n))
runtime.:
insert(key, power): Insert into the tree. The node in the tree should also store the power, and any other variables needed to augment the tree in such a way that all other operations are also
O(log(n))
. You can assume that there is no node in the tree which already has the same key.get(key): Return the power of the node identified by the key.
delete(key): Delete the node with key (assume that the key does exist in the tree prior to the delete.
update(key,power): Update the power at the node given by key.
Here is where it gets interesting:
highestPower(key1, key2): Return the maximum power of all nodes with key
k
in the range key1 <=k
<= key2. That is, all keys from key1 to key2, inclusive on both ends.powerSum(key1, key2): Return the sum of the powers of all nodes with key
k
in the ragne key1 <=k
<= key2. That is, all keys from key1 to key2, inclusive on both ends.
The main thing I would like to know is what extra variables should I store at each node. Then I need to work out how to use each one of these in each of the above functions so that the tree stays balanced and all operations can run in O(log(n))
My original thought was to store the following:
- highestPowerLeft: The highest power of all child nodes to the right of this node.
- highestPowerRight: The highest power of all child nodes to the right of this node.
- powerSumLeft: The sum of the powers of all child nodes to the left of this node.
- powerSumRight: The sum of the powers of all child nodes to the right of this node.
Would just this extra information work? If so, I'm not sure how to deal with it in the functions that are required. Frankly my knowledge of Red Black Tree's isn't great because I feel like every explanation of them gets convoluted really fast, and all the rotations and things confuse the hell out of me. Thanks to anyone willing to attempt helping here, I know what I'm asking is far from simple.