-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlvlorder.c
More file actions
72 lines (60 loc) · 1.57 KB
/
Copy pathlvlorder.c
File metadata and controls
72 lines (60 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
#include <stdio.h>
#include <stdlib.h>
#include "template.h"
// Queue Node structure for BuyerTree
typedef struct QueueNode {
BuyerTree* treeNode;
struct QueueNode* next;
} QueueNode;
// Queue structure
typedef struct {
QueueNode* front;
QueueNode* rear;
} Queue;
// Queue functions
Queue* createQueue() {
Queue* q = (Queue*)malloc(sizeof(Queue));
q->front = q->rear = NULL;
return q;
}
void enqueue(Queue* q, BuyerTree* node) {
QueueNode* temp = (QueueNode*)malloc(sizeof(QueueNode));
temp->treeNode = node;
temp->next = NULL;
if (!q->rear) q->front = q->rear = temp;
else {
q->rear->next = temp;
q->rear = temp;
}
}
BuyerTree* dequeue(Queue* q) {
if (!q->front) return NULL;
QueueNode* temp = q->front;
BuyerTree* node = temp->treeNode;
q->front = q->front->next;
if (!q->front) q->rear = NULL;
free(temp);
return node;
}
int isQueueEmpty(Queue* q) {
return q->front == NULL;
}
// 📘 Level Order Traversal
void levelOrderTraversalBuyers(BuyerTree* root) {
if (!root) return;
Queue* q = createQueue();
enqueue(q, root);
while (!isQueueEmpty(q)) {
BuyerTree* current = dequeue(q);
printf("\n--- Buyer Node ---\n");
for (int i = 0; i < current->numKeys; i++) {
printf("BuyerID: %u\n", current->buyerdata[i].buyerID);
}
if (!current->is_leaf) {
for (int i = 0; i <= current->numKeys; i++) {
if (current->children[i])
enqueue(q, current->children[i]);
}
}
}
}