set up the ds needed to get the bfs implemented
This commit is contained in:
parent
0c711115df
commit
fe6ba5fa8b
|
@ -167,6 +167,22 @@ void print_stack(Stack* stack) {
|
|||
}
|
||||
}
|
||||
|
||||
void print_stack_v2(Stack* stack) {
|
||||
if (stack->len == 0) {
|
||||
printf("ERROR: empty stack\n");
|
||||
} else {
|
||||
printf("[ ");
|
||||
int counter = 0;
|
||||
Node* curr = stack->head;
|
||||
while (counter < stack->len) {
|
||||
printf("%d ", curr->value);
|
||||
curr = curr->prev;
|
||||
counter++;
|
||||
}
|
||||
printf(" ]\n");
|
||||
}
|
||||
}
|
||||
|
||||
int main() {
|
||||
/* lets create the following tree
|
||||
12
|
||||
|
@ -192,10 +208,12 @@ int main() {
|
|||
walk_tree_post_order(root_node, post_stack);
|
||||
walk_tree_in_order(root_node, in_order_stack);
|
||||
|
||||
printf("the len of the stack is %d\n", stack->len);
|
||||
print_stack(stack);
|
||||
print_stack(post_stack);
|
||||
print_stack(in_order_stack);
|
||||
printf("pre order: ", stack->len);
|
||||
print_stack_v2(stack);
|
||||
printf("in order: ", stack->len);
|
||||
print_stack_v2(post_stack);
|
||||
printf("post order: ", stack->len);
|
||||
print_stack_v2(in_order_stack);
|
||||
free(stack);
|
||||
free(post_stack);
|
||||
free(in_order_stack);
|
||||
|
|
|
@ -0,0 +1,87 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
/* Set up data structures to be able to support the breadth first search of a tree */
|
||||
typedef struct QNode {
|
||||
int value;
|
||||
struct QNode* next;
|
||||
struct QNode* prev;
|
||||
} QNode;
|
||||
|
||||
typedef struct Q {
|
||||
QNode* head;
|
||||
QNode* tail;
|
||||
} Q;
|
||||
|
||||
typedef struct TreeNode {
|
||||
int value;
|
||||
struct TreeNode* left;
|
||||
struct TreeNode* right;
|
||||
} TreeNode;
|
||||
|
||||
typedef struct Tree {
|
||||
TreeNode* root;
|
||||
} Tree;
|
||||
|
||||
TreeNode* new_tree_node(int value) {
|
||||
TreeNode* node = malloc(sizeof(TreeNode));
|
||||
node->left = NULL;
|
||||
node->right = NULL;
|
||||
node->value = value;
|
||||
|
||||
return (node);
|
||||
}
|
||||
|
||||
QNode* new_qnode(int value) {
|
||||
QNode* node = malloc(sizeof(QNode));
|
||||
node->next = NULL;
|
||||
node->prev = NULL;
|
||||
node->value = value;
|
||||
|
||||
return (node);
|
||||
}
|
||||
|
||||
Tree* new_tree(TreeNode* root) {
|
||||
Tree* tree = malloc(sizeof(Tree));
|
||||
tree->root = root;
|
||||
return (tree);
|
||||
}
|
||||
|
||||
void add_child_left(TreeNode* parent, TreeNode* node) {
|
||||
if (parent->left != NULL) {
|
||||
printf("ERROR: left child is non-empty\n");
|
||||
exit(1);
|
||||
} else {
|
||||
parent->left = node;
|
||||
}
|
||||
}
|
||||
|
||||
void add_child_right(TreeNode* parent, TreeNode* node) {
|
||||
if (parent->right != NULL) {
|
||||
printf("ERROR: right child is non-empty\n");
|
||||
exit(1);
|
||||
} else {
|
||||
parent->right = node;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
10
|
||||
5 7
|
||||
12 8 88 14
|
||||
|
||||
*/
|
||||
|
||||
int main() {
|
||||
TreeNode* root = new_tree_node(10);
|
||||
Tree* tree = new_tree(root);
|
||||
|
||||
add_child_left(root, new_tree_node(5));
|
||||
add_child_right(root, new_tree_node(7));
|
||||
|
||||
add_child_left(root->left, new_tree_node(12));
|
||||
add_child_right(root->left, new_tree_node(8));
|
||||
|
||||
add_child_left(root->right, new_tree_node(88));
|
||||
add_child_right(root->right, new_tree_node(14));
|
||||
}
|
Loading…
Reference in New Issue