Construct Quad Tree
To solve this coding challenge, we will construct a Quad-Tree from a given \( n \times n \) matrix of 0's and 1's. The process involves recursive partitioning of the grid until we reach uniform sub-grids (either all 0's or all 1's) and building the tree by treating each non-uniform sub-grid as an internal node with four children. Letβs delve deeper into the methodology step-by-step.
# Explanation
Quad-Tree Basics
A Quad-Tree is a specialized tree structure used to partition a two-dimensional space by recursively subdividing it into four quadrants or regions. Each node in the tree represents a region of the grid:- Leaf node (terminal node) : A region where all elements are the same (either all 0's or all 1's).
- Internal node : A region composed of four sub-regions (explained as nodes themselves).
Node Properties in a Quad-Tree:
- val : This is the value the node holds. If the node is a leaf, it represents the region's uniform value (0 or 1). If it's an internal node, this value can be any placeholder as it doesnβt signify the uniform value of a region.
- isLeaf : A boolean indicating if the node is a leaf.
- topLeft, topRight, bottomLeft, bottomRight : The four children nodes representing the four quadrants of the current region.
- Check for Uniform Region : See if the current sub-grid has the same value.
- Recursive Subdivision : If not uniform, divide the grid into four equal sub-grids and recursively process each sub-grid.
- Build Nodes : Construct nodes based on the results from the recursive subdivisions. Letβs outline the process in a detailed manner with pseudocode:
-
Define the
isUniform
-
Define the
buildTree
-
In
buildTree
- If the sub-grid is not uniform, divide it into four smaller grids and recursively process each, then create an internal node with these four children.
-
Define the
construct
buildTree
Steps to Construct a Quad-Tree:
# Step-by-Step Explanation
Step-by-Step Pseudocode and Explanation
Step 1: Uniform Grid Check
A helper function to determine if a sub-grid is uniform (all elements are the same).
# Check if the sub-grid is uniform
# grid: the entire grid
# start_row, start_col: starting row and column indices of the sub-grid
# size: size of the sub-grid
function isUniform(grid, start_row, start_col, size):
first_val = grid[start_row][start_col]
for row from start_row to start_row + size:
for col from start_col to start_col + size:
if grid[row][col] != first_val:
return false # not uniform
return true # all values in sub-grid are the same
Step 2: Recursive Build Function
A recursive function to construct the Quad-Tree from the grid.
# Build the Quad-Tree
# grid: the entire grid
# start_row, start_col: starting row and column indices of the sub-grid
# size: size of the sub-grid
function buildTree(grid, start_row, start_col, size):
if size == 1 or isUniform(grid, start_row, start_col, size):
# Create a leaf node
leaf_value = (grid[start_row][start_col] == 1)
return Node(leaf_value, true, null, null, null, null)
else:
# Size of the sub-grid
half_size = size / 2
# Recursively build each quadrant
topLeft = buildTree(grid, start_row, start_col, half_size)
topRight = buildTree(grid, start_row, start_col + half_size, half_size)
bottomLeft = buildTree(grid, start_row + half_size, start_col, half_size)
bottomRight = buildTree(grid, start_row + half_size, start_col + half_size, half_size)
# Create an internal node
return Node('*', false, topLeft, topRight, bottomLeft, bottomRight)
Step 3: Main Function
The main function that initializes the recursion.
# Main function to initiate the Quad-Tree construction
function construct(grid):
n = length(grid) # assuming grid is a square matrix
if n == 0:
return null # handle edge case if grid is empty
return buildTree(grid, 0, 0, n)