#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Tree node structure
typedef struct Node {
int val;
struct Node* left;
struct Node* right;
} Node;
// Queue node for BFS traversal
typedef struct QNode {
Node* treeNode;
int col;
struct QNode* next;
} QNode;
// Queue structure
typedef struct Queue {
QNode* front;
QNode* rear;
} Queue;
// Map node for storing column to list of values
typedef struct MapNode {
int col;
int* values;
int size;
int capacity;
struct MapNode* next;
} MapNode;
// Map structure (linked list for simplicity)
typedef struct Map {
MapNode* head;
} Map;
// Create new tree node
Node* newNode(int val) {
Node* node = (Node*)malloc(sizeof(Node));
node->val = val;
node->left = NULL;
node->right = NULL;
return node;
}
// Initialize queue
void initQueue(Queue* q) {
q->front = NULL;
q->rear = NULL;
}
// Enqueue tree node with column
void enqueue(Queue* q, Node* node, int col) {
QNode* qnode = (QNode*)malloc(sizeof(QNode));
qnode->treeNode = node;
qnode->col = col;
qnode->next = NULL;
if (q->rear == NULL) {
q->front = qnode;
q->rear = qnode;
} else {
q->rear->next = qnode;
q->rear = qnode;
}
}
// Dequeue
QNode* dequeue(Queue* q) {
if (q->front == NULL) return NULL;
QNode* temp = q->front;
q->front = q->front->next;
if (q->front == NULL) q->rear = NULL;
return temp;
}
// Initialize map
void initMap(Map* map) {
map->head = NULL;
}
// Find or create map node for column
MapNode* getMapNode(Map* map, int col) {
MapNode* curr = map->head;
while (curr != NULL) {
if (curr->col == col) return curr;
curr = curr->next;
}
// Not found, create new
MapNode* new_map_node = (MapNode*)malloc(sizeof(MapNode));
new_map_node->col = col;
new_map_node->size = 0;
new_map_node->capacity = 4;
new_map_node->values = (int*)malloc(sizeof(int) * new_map_node->capacity);
new_map_node->next = map->head;
map->head = new_map_node;
return new_map_node;
}
// Add value to map node
void addValue(MapNode* mapNode, int val) {
if (mapNode->size == mapNode->capacity) {
mapNode->capacity *= 2;
mapNode->values = (int*)realloc(mapNode->values, sizeof(int) * mapNode->capacity);
}
mapNode->values[mapNode->size++] = val;
}
// Compare function for qsort to sort map nodes by column
int cmpMapNode(const void* a, const void* b) {
MapNode* m1 = *(MapNode**)a;
MapNode* m2 = *(MapNode**)b;
return m1->col - m2->col;
}
// Vertical order traversal function
void verticalOrderTraversal(Node* root) {
if (root == NULL) return;
Queue q;
initQueue(&q);
Map map;
initMap(&map);
enqueue(&q, root, 0);
while (q.front != NULL) {
QNode* qnode = dequeue(&q);
Node* curr = qnode->treeNode;
int col = qnode->col;
free(qnode);
MapNode* mapNode = getMapNode(&map, col);
addValue(mapNode, curr->val);
if (curr->left != NULL) enqueue(&q, curr->left, col - 1);
if (curr->right != NULL) enqueue(&q, curr->right, col + 1);
}
// Count map nodes
int count = 0;
MapNode* curr = map.head;
while (curr != NULL) {
count++;
curr = curr->next;
}
// Collect map nodes into array for sorting
MapNode** arr = (MapNode**)malloc(sizeof(MapNode*) * count);
curr = map.head;
for (int i = 0; i < count; i++) {
arr[i] = curr;
curr = curr->next;
}
qsort(arr, count, sizeof(MapNode*), cmpMapNode);
// Print values column by column
for (int i = 0; i < count; i++) {
for (int j = 0; j < arr[i]->size; j++) {
printf("%d", arr[i]->values[j]);
if (j < arr[i]->size - 1) printf(" ");
}
printf("\n");
}
free(arr);
}
int main() {
// Build tree from dry run example
Node* root = newNode(1);
root->left = newNode(2);
root->right = newNode(3);
root->left->left = newNode(4);
root->left->right = newNode(5);
root->right->right = newNode(6);
verticalOrderTraversal(root);
return 0;
}
Start BFS with root at column 0
QNode* qnode = dequeue(&q);
Remove front node from queue to process
MapNode* mapNode = getMapNode(&map, col);
Find or create map entry for current column
addValue(mapNode, curr->val);
Add current node's value to its column group
if (curr->left != NULL) enqueue(&q, curr->left, col - 1);
Add left child to queue with column one less
if (curr->right != NULL) enqueue(&q, curr->right, col + 1);
Add right child to queue with column one more
qsort(arr, count, sizeof(MapNode*), cmpMapNode);
Sort columns from left to right
printf("%d", arr[i]->values[j]);
Print all nodes in current column