Geopolitics

Efficiently Determining Cousin Nodes- A Guide to Identifying Kinship in Graph Structures

How to Check if Two Nodes are Cousins

In a tree data structure, nodes are connected in a hierarchical manner, where each node can have zero or more child nodes. One interesting relationship between nodes in a tree is that of cousins. Two nodes are considered cousins if they are at the same level but are not directly connected to each other. This article will guide you through the process of checking if two nodes are cousins in a binary tree.

Understanding the Problem

Before diving into the solution, it’s essential to understand the problem statement. In a binary tree, a node can have at most two children: left and right. To determine if two nodes are cousins, we need to ensure that:

1. Both nodes are present in the tree.
2. Both nodes are at the same level in the tree.
3. The paths from the root to each node are different.

Approach to Solve the Problem

To solve this problem, we can use a recursive approach. The algorithm can be broken down into the following steps:

1. Traverse the tree to find the paths from the root to the two nodes.
2. Check if both nodes are at the same level.
3. Compare the paths to ensure they are different.

Here’s a step-by-step breakdown of the algorithm:

1. Define a recursive function, `findLevel(node, level)`, that returns the level of the given node.
2. Define another recursive function, `findPath(node, path)`, that returns the path from the root to the given node.
3. Traverse the tree using a depth-first search (DFS) algorithm to find the paths for the two nodes.
4. Compare the levels of the two nodes.
5. If the levels are the same, compare the paths. If the paths are different, the nodes are cousins.

Implementation

Below is the Python implementation of the algorithm:

“`python
class TreeNode:
def __init__(self, value):
self.val = value
self.left = None
self.right = None

def findLevel(node, level):
if node is None:
return -1
if node.left is None and node.right is None:
return level
return max(findLevel(node.left, level + 1), findLevel(node.right, level + 1))

def findPath(node, path):
if node is None:
return False
path.append(node.val)
if node.left is None and node.right is None:
return True
if findPath(node.left, path) or findPath(node.right, path):
return True
path.pop()
return False

def areCousins(root, node1, node2):
level1 = findLevel(root, 0)
level2 = findLevel(root, 0)
path1 = []
path2 = []

if findPath(root, path1) and findPath(root, path2):
if level1 == level2 and path1 != path2:
return True
return False

Example usage
root = TreeNode(1)
root.left = TreeNode(2)
root.right = TreeNode(3)
root.left.left = TreeNode(4)
root.left.right = TreeNode(5)
root.right.left = TreeNode(6)
root.right.right = TreeNode(7)

node1 = root.left.left
node2 = root.left.right

print(areCousins(root, node1, node2)) Output: True
“`

Conclusion

In this article, we discussed how to check if two nodes are cousins in a binary tree. By using a recursive approach and comparing the levels and paths of the nodes, we can determine if they are indeed cousins. The provided Python code demonstrates the implementation of this algorithm.

Related Articles

Back to top button