Created
December 30, 2024 17:05
-
-
Save ravilock/76128ab5c79c15904ab386fb5a93a0a1 to your computer and use it in GitHub Desktop.
Tree print per level
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
#include <string.h> | |
typedef struct queueNode { | |
void *value; | |
struct queueNode *next; | |
} QueueNode; | |
QueueNode *newQueueNode(void *value) { | |
QueueNode *qnode = malloc(sizeof(QueueNode)); | |
if (qnode == NULL) { | |
return NULL; | |
} | |
qnode->value = value; | |
return qnode; | |
} | |
typedef struct queue { | |
QueueNode *first; | |
int length; | |
} Queue; | |
Queue *newQueue(void) { | |
Queue *q = malloc(sizeof(Queue)); | |
if (q == NULL) { | |
return NULL; | |
} | |
q->length = 0; | |
return q; | |
} | |
void enqueue(Queue *q, void *value) { | |
if (q == NULL) { | |
return; | |
} | |
if (q->first == NULL) { | |
q->first = newQueueNode(value); | |
q->length = 1; | |
return; | |
} | |
QueueNode *current = q->first; | |
while (current->next != NULL) { | |
current = current->next; | |
} | |
current->next = newQueueNode(value); | |
q->length++; | |
} | |
void *dequeue(Queue *q) { | |
if (q == NULL || q->length == 0) { | |
return NULL; | |
} | |
void *value = q->first->value; | |
q->first = q->first->next; | |
q->length--; | |
return value; | |
} | |
int is_empty(Queue *q) { | |
if (q == NULL || q->length == 0) { | |
return 1; | |
} | |
return 0; | |
} | |
typedef struct tree { | |
int value; | |
struct tree *left; | |
struct tree *right; | |
} Tree; | |
Tree *newTree(int value) { | |
Tree *a = malloc(sizeof(Tree)); | |
if (a == NULL) { | |
return NULL; | |
} | |
a->value = value; | |
return a; | |
} | |
void insert(Tree *tree, int value) { | |
if (tree == NULL) { | |
return; | |
} | |
Tree *current = tree; | |
Tree *new = newTree(value); | |
while (1) { | |
if (value < current->value) { | |
if (current->left == NULL) { | |
current->left = new; | |
return; | |
} | |
current = current->left; | |
} else { | |
if (current->right == NULL) { | |
current->right = new; | |
return; | |
} | |
current = current->right; | |
} | |
} | |
} | |
// 0, 2, 6, 14, 30 | |
int is_last_index_in_level(int level_last_element_index, int current_index) { | |
return ((level_last_element_index * 2) + 2) == current_index; | |
} | |
void print_per_level(Tree *tree) { | |
if (tree == NULL) { | |
return; | |
} | |
Queue *q = newQueue(); | |
if (q == NULL) { | |
return; | |
} | |
enqueue(q, tree); | |
Tree *current; | |
int current_index = 0; | |
int level_last_element_index = 0; | |
while (!is_empty(q)) { | |
if (is_last_index_in_level(level_last_element_index, current_index)) { | |
level_last_element_index = current_index; | |
printf("\n"); | |
} | |
current = dequeue(q); | |
if (current != NULL) { | |
printf("%d ", current->value); | |
if (current->left != NULL) | |
enqueue(q, current->left); | |
if (current->right != NULL) | |
enqueue(q, current->right); | |
} | |
current_index++; | |
current_index++; | |
} | |
printf("\n"); | |
} | |
int main(void) { | |
Tree *root = newTree(30); | |
insert(root, 32); | |
insert(root, 27); | |
insert(root, 29); | |
insert(root, 31); | |
insert(root, 33); | |
print_per_level(root); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment