Permission Management for a General Tree
1) Implement a TreeNode class that represents a general tree, i.e. one where each node can have an arbitrary number of children.
2) Implement the following methods:
void grantAccess(TreeNode root);
void revokeAccess(TreeNode root);
boolean hasAccess(TreeNode root);Once one grants access to a certain node, access is also granted to all of the nodes below that node too. The same logic follows for revokeAccess. Finally, hasAccess should be able to decide whether a hypothetical use has access to a certain node at a given time.
Examples
10
1 2 3
5 13 9 0 4 8
// ex 1
hasAccess(4); // false
revokeAccess(10);
hasAccess(4); // false
grantAccess(2);
hasAccess(4); // true
// ex 2
hasAccess(4); // false
grantAccess(2);
hasAccess(4); // true
revokeAccess(10);
hasAccess(4); // falseThe Idea: We need to define some kind of priority for when we want to conclude whether or not a node has access priveleges. One way to do this is to define use time element where the closest relevent time decides permission priveleges.
Complexity: O(1) time and space for both revokeAccess and grantAccess. O(N) worse case time where N is the number of nodes for hasAccess (consider the linkedlist n-ary tree). If the tree is fairly balanced: revokeAccess and grantAccess is the same, but now hasAccess is O(log_k(N)) time (which is proportional to the number of levels in the tree).
Last updated
Was this helpful?