Javatpoint Logo
Javatpoint Logo

Tree of Space - Locking and Unlocking N-Ary Tree

Introduction

N-Ary trees are a type of hierarchical data structure that can be used to represent hierarchical relationships in a variety of domains because their nodes can have multiple children. A strong locking and unlocking mechanism must be implemented in situations where several threads or processes must access and modify nodes concurrently in order to avoid race conditions and guarantee data consistency. Unlike binary trees, where nodes can have no more than two children, N-Ary trees allow nodes to have multiple children.

Representing hierarchical relationships in structures like file systems, organization charts, or network routing tables is a good use case for this structure. Data inconsistency is introduced when multiple processes or threads attempt to access or modify nodes in an N-Ary tree simultaneously. The data may become tainted, for example, if one thread is reading or changing a node while another thread is working on it. A locking mechanism is used to guarantee exclusive access to nodes during crucial operations in order to address this.

Code

Output:

Tree of Space - Locking and Unlocking N-Ary Tree

Code Explanation

Node Definition

  • The struct Node represents an N-ary tree node.
  • A boolean indicating whether the node is locked (locked), an integer count of its locked descendants (lockedDescendants), a pointer to the parent (parent), an array of pointers to the children (children), and an integer value (val) are all present in each node.

Node Initialization

  • The createNode function returns a pointer to the newly created node, initializes its fields, and allows memory for a new node.

Using the lockNode function to lock a node

  • The goal of this function is to lock a specific node.
  • It determines whether the node itself, or any of its offspring, is already locked. If so, false is returned.
  • It determines if any of the node's ancestors are locked. If so, false is returned.
  • The node updates the lockedDescendants count in its ancestors and sets the locked flag to true if it is capable of being locked.
  • If the node is successfully locked, it returns true; if not, it returns false.

Using the unlockNode function to unlock a node

  • This function makes an effort to unlock a specified node.
  • It determines whether the node is unlocked. If so, false is returned.
  • It updates the lockedDescendants count in its ancestors and unlocks the node if it is locked.
  • If the node is successfully unlocked, it returns true; if not, it returns false.

Example Usage (main function)

  • Produces a basic N-ary tree with a hierarchy and nodes.
  • Uses the lockNode and unlockNode functions to lock and unlock nodes in the tree.
  • Prints the lock or unlock status of each node.

Time Complexity

The lockNode and unlockNode functions are the main determinants of the time complexity of the given N-ary tree locking and unlocking code. In the worst scenario, both functions iterate through the target node's ancestors all the way to the root, resulting in an O(h) time complexity, where 'h' is the tree's height. In the worst scenario, when the tree is degenerate (basically a linked list), the time complexity approaches O(n), where 'n' is the number of nodes in the tree. The height of the tree is determined by its structure. Since each function's operations involve constant time steps, the tree's height is the most important factor.

Space Complexity

The code uses memory in proportion to the number of nodes in the N-ary tree in terms of space complexity. The tree nodes themselves and their associated fields-such as the integer value, pointers to the parent and children, the boolean indicating whether the node is locked, and the number of locked descendants-are the main components that take up space. O(n), where 'n' is the number of nodes in the tree, is the space complexity because a struct with a fixed set of fields represents each node. Furthermore, the call stack during function calls is a result of the recursive nature of the tree structure; however, this overhead is also proportional to the tree height, resulting in O(h) space complexity.


Next TopicTriply Linked list





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA