Change the NODE
data structure to allow more than one page in the node.
typedef struct node
{
int choice; // if 1 then page tree, if 0 then reference tree
vector<Page> pg; // if choice=0
Reference ref; // if choice=1
struct node* left;
struct node* right;
}NODE;
You can modify the equals
and lessthan
functions accordingly. The locatenode
function remains the same.
I'll add something about the data structures used here. You actually don't need a tree to maintain the references. References are required only when:
If the page exists, update its reference time to now
But, in the case above, if the tree has reached maximum capacity delete a page with the oldest reference time
So this can be done using a heap as well. The advantage is that, then insert operation will cost only O(1)
time.
For the first point, if reference has to updated, the node goes downward in the heap. So maintain a link from the page tree to the reference heap. And continue swapping the nodes in the reference heap until the current node goes to the last level. O(logn)
time.
For the second point, delete the first node of the heap. O(logn)
time here.
And as for If the page does not exist - add the page to both trees with a creation time of now
, add the new node at the end of the heap. O(1)
time.