# Template for row-wise traversal in Python (store in a list and print)
def row_wise_traversal(matrix):
result = []
for i in range(len(matrix)): # Traverse each row
result.extend(matrix[i]) # Add each element to the result list
print(result)
#---------------------------------------------------------------------------
# Template for column-wise traversal in Python (store in a list and print)
def column_wise_traversal(matrix):
result = []
for j in range(len(matrix[0])): # Traverse each column
for i in range(len(matrix)):
result.append(matrix[i][j]) # Add each element to the result list
print(result)
#-------------------------------------------------------------------------------
# Template for zigzag diagonal traversal in Python (store in a list and print)
def zigzag_diagonal_traversal(matrix):
rows = len(matrix) # Get the number of rows in the matrix
cols = len(matrix[0]) # Get the number of columns in the matrix
result = [] # Initialize an empty list to store the traversal result
# Iterate through each diagonal, from 0 to rows + cols - 2
for d in range(rows + cols - 1):
if d % 2 == 0: # For even index diagonals, traverse upwards
# Determine the starting row (r) for the upward traversal
r = min(d, rows - 1)
c = d - r # Calculate the starting column (c)
# Traverse upwards along the diagonal
while r >= 0 and c < cols:
result.append(matrix[r][c]) # Add the current element to the result
r -= 1 # Move up in the row
c += 1 # Move right in the column
else: # For odd index diagonals, traverse downwards
c = min(d, cols - 1) # Determine the starting column (c) for the downward traversal
r = d - c # Calculate the starting row (r)
# Traverse downwards along the diagonal
while c >= 0 and r < rows:
result.append(matrix[r][c]) # Add the current element to the result
r += 1 # Move down in the row
c -= 1 # Move left in the column
print(result) # Print the final zigzag diagonal traversal result
#-------------------------------------------------------------------------------
# Template for spiral traversal in Python (store in a list and print)
def spiral_traversal(matrix):
# Check if the matrix is empty
if not matrix or not matrix[0]:
return []
result = [] # Initialize an empty list to store the traversal result
top, bottom = 0, len(matrix) - 1 # Set the top and bottom boundaries
left, right = 0, len(matrix[0]) - 1 # Set the left and right boundaries
# Continue the loop until all boundaries converge
while top <= bottom and left <= right:
# Traverse from left to right along the top row
for col in range(left, right + 1):
result.append(matrix[top][col])
top += 1 # Move the top boundary down
# Traverse from top to bottom along the right column
for row in range(top, bottom + 1):
result.append(matrix[row][right])
right -= 1 # Move the right boundary left
# Check if there are still rows to traverse
if top <= bottom:
# Traverse from right to left along the bottom row
for col in range(right, left - 1, -1):
result.append(matrix[bottom][col])
bottom -= 1 # Move the bottom boundary up
# Check if there are still columns to traverse
if left <= right:
# Traverse from bottom to top along the left column
for row in range(bottom, top - 1, -1):
result.append(matrix[row][left])
left += 1 # Move the left boundary right
print(result) # Print the final spiral traversal result
#---------------------------------------------------------------------------
# Example usage
matrix = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
print("Matrix:")
for row in matrix:
print(row)
print("\nRow-wise traversal: ")
row_wise_traversal(matrix)
print("\nColumn-wise traversal: ")
column_wise_traversal(matrix)
print("\nZig-zag traversal: ")
zigzag_diagonal_traversal(matrix)
print("\nSpiral traversal: ")
spiral_traversal(matrix)