I'm trying to implement a binary search tree in C by using minheap. I've got the basic code down but I cannot get it to work properly. My main problem, according to the build messages, seem to be that I compare pointers and integers. As this code is based off of various explanations that I found in my textbooks and online, I'm not sure how to avoid that.
Here is the complete code (final and working - see edit history for original):
#include <stdio.h>
#include <stdlib.h>
typedef struct TreeNode {
int key;
struct TreeNode* lChild;
struct TreeNode* rChild;
} node;
node *createTree(int key) {
node *root = malloc(sizeof(node));
root->key = key;
root->lChild = NULL;
root->rChild = NULL;
return(root);
}
void insert(node *root, int key) {
if (root->key == -1)
root->key = key;
else if (key < root->key) {
if (root->lChild != NULL)
insert(root->lChild, key);
else {
root->lChild = malloc(sizeof(node));
root->lChild->key = key;
root->lChild->lChild = NULL;
root->lChild->rChild = NULL;
}
}
else if (key > root->key) {
if (root->rChild != NULL)
insert(root->rChild, key);
else {
root->rChild = malloc(sizeof(node));
root->rChild->key = key;
root->rChild->lChild = NULL;
root->rChild->rChild = NULL;
}
}
}
void deleteTree(node *root) {
if (root != NULL) {
if (root->lChild != NULL)
deleteTree(root->lChild);
if (root->rChild != NULL)
deleteTree(root->rChild);
free(root);
}
}
void printNode(node *root) {
if (root->lChild != NULL) {
printf("%d -- %d\n", root->key, root->lChild->key);
if (root->rChild != NULL)
printf("%d -- %d\n", root->key, root->rChild->key);
printNode(root->lChild);
}
if (root->rChild != NULL) {
if (root->lChild == NULL)
printf("%d -- %d\n", root->key, root->rChild->key);
printNode(root->rChild);
}
}
void printTree(node *root) {
printf("graph g {\n");
printNode(root);
printf("}\n");
}
void test() {
// 1.
node *root1 = createTree(-1);
insert(root1, 4);
insert(root1, 2);
insert(root1, 3);
insert(root1, 8);
insert(root1, 6);
insert(root1, 7);
insert(root1, 9);
insert(root1, 12);
insert(root1, 1);
printTree(root1);
// 2.
node *root2 = createTree(-1);
insert(root2, 3);
insert(root2, 8);
insert(root2, 10);
insert(root2, 1);
insert(root2, 7);
printTree(root2);
// 3.
deleteTree(root1);
// 4.
deleteTree(root2);
}
int main() {
test();
}
Here is the output (final and working - see edit history for original):
graph g {
4 -- 2
4 -- 8
2 -- 1
2 -- 3
8 -- 6
8 -- 9
6 -- 7
9 -- 12
}
graph g {
3 -- 1
3 -- 8
8 -- 7
8 -- 10
}
Process returned 1 (0x1) execution time : 0.712 s
Press any key to continue.
It looks like I need to exclude the connection from "nothing" to the root of the tree still.
printTree
I added for debugging purposes but there seem to be problems with it as well.