give nthe problem statements and code "" Locking The Tree of Space You have a world map…
- User
give nthe problem statements and code ""
Locking The Tree of Space
You have a world map represented as an M-Ary tree (sample tree below)
In graph theory, an m-ary tree (for nonnegative integers m) (also known as n-ary, k-ary or k-way tree) is an arborescence (or an ordered tree) in which each node has no more than m children. A binary tree is the special case where m = 2, and a ternary tree is another case with m = 3 that limits its children to three.
In graph theory, an arborescence is a directed graph in which, for a vertex u (called the root) and any other vertex v, there is exactly one directed path from u to v. A graph is a collection of vertices (also known as nodes) and edges that connect these vertices. A tree is a special type of graph that is connected and acyclic, meaning that there are no cycles in the graph. In a tree, there is a unique path between any two vertices, and there is a single vertex called the root that is used as the starting point for traversing the tree.
[image: Sample Tree]
You need to define three operations on it:
cpp1. lock(X, uid) 2. unlock(X, uid) 3. upgradeLock(X, uid)where
Xthe name of a node in the tree (that would be unique) anduidis the user who is performing the operation.
Input format: x is country name (string) and uid is user number (number).Here are the definitions for the operations:
lock(x, uid)
Lock takes an exclusive access on the subtree rooted atX. It is formally defined like this: Oncelock(x, uid)succeeds, then:
lock(A, anyUserid)should fail (returns false), whereAis a descendent ofX,lock(B, anyUserld)should fail (returns false), whereXis a descendent ofB, Lock operation cannot be performed on a node which is already locked i.e.lock(x, anyUserld)should fail (returns false).
Exclusive access means that only one user can lock a node and its descendants at a time, and no other user can lock any node that is related to the locked node by ancestry or descent. This ensures that there is no conflict or interference between different users who want to perform operations on the tree.Unlock reverts what was done by the lock operation. It can only be called on same node on which user had called a Lock on before. Returns true if it is successful.
- Assistant