All Nodes Distance K in Tree šŸŽÆ

beginner
25 min

All Nodes Distance K in Tree šŸŽÆ

Welcome to this comprehensive guide on the All Nodes Distance K in Tree problem! In this lesson, we'll learn how to find the distance between all nodes and a given node in a tree structure. Let's dive in! šŸ“

Understanding the Problem šŸ’”

Given a tree and a node, find the distance between the given node and all other nodes in the tree. The distance between two nodes is the number of edges in the shortest path connecting them.

Breaking it Down šŸ“

Breadth-First Search (BFS)

Breadth-First Search is an algorithm used to traverse or search through a graph in a breadth-wise or level-wise manner. It starts at the root node and explores all of the nodes at the current depth before moving on to nodes at the next depth level.

Distance Calculation

To calculate the distance between two nodes, we'll use the BFS algorithm and maintain a distance array to store the shortest distance from the root node to each node. Initially, we set all distances to -1, except for the root node's distance, which is 0.

Implementation in Python šŸ’”

Here's a complete working example of the All Nodes Distance K in Tree problem using Python.

python
class Node: def __init__(self, data): self.data = data self.children = [] self.parent = None self.level = 0 def find_distance(root, node, parent_distance): for child in root.children: if child.data == node: return parent_distance + 1 distance = find_distance(child, node, parent_distance + 1) if distance != -1: return distance return -1 def bfs(root): queue = [] root.level = 0 queue.append(root) parent_node = {} while queue: current_node = queue.pop(0) for child in current_node.children: child.level = current_node.level + 1 queue.append(child) parent_node[child.data] = current_node.data return parent_node def print_all_distances(root, node): parent_node = bfs(root) parent_distance = find_distance(root, node, 0) if parent_distance == -1: print(f"Node {node} not found in the tree.") else: distances = [] def calculate_distances(node, current_distance): if node.data == node: distances.append(current_distance) return current_distance += 1 current_node_distance = parent_node[node.data] calculate_distances(node.children[0], current_distance) calculate_distances(node, current_distance - (current_node_distance - parent_distance)) calculate_distances(root, 0) print(f"Distances of node {node} from itself are: ", distances) print(f"All other distances for node {node}: ", {distance: count for distance, count in Counter(distances[1:])}) # Creating the tree structure root = Node(1) root.children.append(Node(2)) root.children.append(Node(3)) root.children[0].children.append(Node(4)) root.children[0].children.append(Node(5)) root.children[1].children.append(Node(6)) root.children[2].children.append(Node(7)) root.children[2].children.append(Node(8)) # Running the code print_all_distances(root, 5)

Let's Test It Out šŸ’”

In this example, we've created a tree with root node 1 and 8 nodes in total. The All Nodes Distance K in Tree function will calculate the distances for node 5. Run the code to see the results!

Quiz Time! šŸŽÆ

Quick Quiz
Question 1 of 1

What is the algorithm used to solve the All Nodes Distance K in Tree problem?