compleate mess of trying to write a binary tree cobeling together bits of code
i have tried to use bits of code that probably don't work with in themselves using what i have learnt from dealing with linked lists ie a struct to hold the pointer to the root node.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
typedef struct Bin_Node
{
int data;
struct Bin_Node *left;
struct Bin_Node *right;
}Bin_Node;
typedef struct Bin_Tree
{
struct Bin_Node *root; //same as head node in a linked list
int node_count;
}Bin_Tree;
Bin_Tree *create_tree(void);
Bin_Tree *initalize_tree(Bin_Tree *tree);
Bin_Node *create_node(void);
Bin_Node *initalize_node(Bin_Node *node, int data);
void insert_node(Bin_Tree *tree, Bin_Node *node);
void print_tree(const Bin_Tree *tree);
void destroy_tree(Bin_Tree *tree);
int main()
{
int i, nums[] = {2, 6, 3, 9, 20, 13, 54, 10, 43};
Bin_Tree *tree;
Bin_Node *node;
tree = create_tree();
assert(tree != NULL);
tree = initalize_tree(tree);
for (i = 0; i < 9; i++)
{
node = create_node();
assert(node != NULL);
node = initalize_node(node, nums[i]);
insert_node(tree, node);
tree->node_count++;
}
print_tree(tree);
destroy_tree(tree);
return 0;
}
Bin_Tree *create_tree(void)
{
return malloc(sizeof(Bin_Tree));
}
Bin_Tree *initalize_tree(Bin_Tree *tree)
{
tree->node_count = 0;
tree->root = NULL;
return tree;
}
Bin_Node *create_node(void)
{
return malloc(sizeof(Bin_Node));
}
Bin_Node *initalize_node(Bin_Node *node, int data)
{
node->data = data;
node->left = NULL;
node->right = NULL;
return node;
}
void insert_node(Bin_Tree *tree, Bin_Node *node)
{
if (!tree) //parent node has no children??
{
tree = node;
}
else
{
if (tree->root->data < node->data) //the data of the parent we are looking at is less than the data of the node so go to its right child
{
insert_node(tree->root->right, node);
}
else
{
insert_node(tree->root->left, node);
}
}
}
void print_tree(const Bin_Tree *tree)
{
if (!tree)
{
return;
}
if (tree->root->left) //parent node has a child to the left
{
print_tree(tree->root->left);
}
printf("node data = %d\n", tree->root->data);
if (tree->root->right) //parent node has a child to the right
{
print_tree(tree->root->right);
}
}
void destroy_tree(Bin_Tree *tree)
{
if (!tree) //parent has children
{
destroy_tree(tree->root->left);
destroy_tree(tree->root->right);
free(tree);
}
}
unsurprisingly i have a load of warnings (7) all saying incompatible pointer type which i realise Bin_Tree is a different type to Bin_Node but the root member is Bin_Node type
here is the list of warnings
Code:
||=== Build: Debug in binary treev2 (compiler: GNU GCC Compiler) ===|
/home/ben/Documents/coding/binary treev2/main.c||In function ‘insert_node’:|
/home/ben/Documents/coding/binary treev2/main.c|81|warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]|
/home/ben/Documents/coding/binary treev2/main.c|87|warning: passing argument 1 of ‘insert_node’ from incompatible pointer type [-Wincompatible-pointer-types]|
/home/ben/Documents/coding/binary treev2/main.c|77|note: expected ‘Bin_Tree * {aka struct Bin_Tree *}’ but argument is of type ‘struct Bin_Node *’|
/home/ben/Documents/coding/binary treev2/main.c|91|warning: passing argument 1 of ‘insert_node’ from incompatible pointer type [-Wincompatible-pointer-types]|
/home/ben/Documents/coding/binary treev2/main.c|77|note: expected ‘Bin_Tree * {aka struct Bin_Tree *}’ but argument is of type ‘struct Bin_Node *’|
/home/ben/Documents/coding/binary treev2/main.c||In function ‘print_tree’:|
/home/ben/Documents/coding/binary treev2/main.c|105|warning: passing argument 1 of ‘print_tree’ from incompatible pointer type [-Wincompatible-pointer-types]|
/home/ben/Documents/coding/binary treev2/main.c|96|note: expected ‘const Bin_Tree * {aka const struct Bin_Tree *}’ but argument is of type ‘struct Bin_Node *’|
/home/ben/Documents/coding/binary treev2/main.c|112|warning: passing argument 1 of ‘print_tree’ from incompatible pointer type [-Wincompatible-pointer-types]|
/home/ben/Documents/coding/binary treev2/main.c|96|note: expected ‘const Bin_Tree * {aka const struct Bin_Tree *}’ but argument is of type ‘struct Bin_Node *’|
/home/ben/Documents/coding/binary treev2/main.c||In function ‘destroy_tree’:|
/home/ben/Documents/coding/binary treev2/main.c|120|warning: passing argument 1 of ‘destroy_tree’ from incompatible pointer type [-Wincompatible-pointer-types]|
/home/ben/Documents/coding/binary treev2/main.c|116|note: expected ‘Bin_Tree * {aka struct Bin_Tree *}’ but argument is of type ‘struct Bin_Node *’|
/home/ben/Documents/coding/binary treev2/main.c|121|warning: passing argument 1 of ‘destroy_tree’ from incompatible pointer type [-Wincompatible-pointer-types]|
/home/ben/Documents/coding/binary treev2/main.c|116|note: expected ‘Bin_Tree * {aka struct Bin_Tree *}’ but argument is of type ‘struct Bin_Node *’|
||=== Build finished: 0 error(s), 7 warning(s) (0 minute(s), 0 second(s)) ===|
do i assume from all these that all this stuff can't be done recursively using a struct for keeping track of the root node?