Here is the 4th verison of the program. It works fine, it adds and prints
the tree elements and free() also works fine. The only thing that has
remained is how to heapify it. Any ideas on that ?
(Thanks to Chad for telling me the idea of using post-order to free() the
tree)
/* A simple program using a struct to implement a max Binary Heap in C.
This binary heap will support 3 operations:
*
* - insert an element to the heap O(log(n))
* - delete the element with the highest priority (which of course is the
root node in a Binary Heap) O(log(n))
* - find the maximum element without removing it (again it will be root
node, but will take only O(1) time)
*
* The struct will contain a character string (as priority) and a char
as (his grade). We will use the string to decide
* whether some student has higher priority than the other.
*
* As per Wikiepdia:
http://en.wikipedia.org/wiki/
Binary_heap#Building_a_heap , directly inserting an element in a Binary
Heap
* is not as much efficient as compared to first adding all elements to a
Binary Tree and then heapify the Binary-tree to make a
* Binary-heap. So we will first add entries to a Binary-Tree and then
heapify it to get a Binary Heap.
*
*
* VERSION 0.4
*
*/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum { VAL_SUCC = 0,
VAL_ERR = -1
};
enum { PRIORITY_SIZE = 20 };
struct BH_node
{
char priority[PRIORITY_SIZE];
char grade;
struct BH_node* left;
struct BH_node* right;
};
struct BH_node* BH_node_create(char [], char);
void BH_free(struct BH_node* s);
struct BH_node* BH_insert(struct BH_node**, struct BH_node* );
void BH_print(struct BH_node*);
int main(void)
{
struct BH_node* root = NULL;
struct BH_node* a1 = BH_node_create("First Class", 'A');
struct BH_node* a2 = BH_node_create("Second Class", 'B');
if(a1) BH_insert(&root, a1);
if(a1) BH_insert(&root, a2);
BH_print(root);
BH_free(root);
root = NULL;
return 0;
}
struct BH_node* BH_insert(struct BH_node** s, struct BH_node* n)
{
if(NULL == n)
{
/* fprintf(stderr, "IN: %s : Can not add NULL node\n",
__func__); */
}
else if(NULL == s)
{
/* fprintf(stderr, "IN: %s, NULL address of root node ?? \n",
__func__); */
}
else if(NULL == *s)
{
*s = n;
}
else
{
struct BH_node* r = *s;
int cmp = strcmp(n->priority, r->priority);
if(cmp == 0)
{
/* fprintf(stderr, "IN: %s : Duplicate entry, will not add\n",
__func__); */
}
else if(cmp < 0)
{
BH_insert( &(r->left), n);
}
else
{
BH_insert( &(r->right), n);
}
}
return *s;
}
struct BH_node* BH_node_create(char priority[], char grade)
{
struct BH_node* p = malloc(1 * sizeof *p);
if(NULL == p)
{
/* fprintf(stderr,"IN: %s :, Out of Memory\n", __func__); */
}
else
{
strcpy(p->priority, priority);
p->grade = grade;
p->left = p->right = NULL;
}
return p; /* Make sure calling function checks for NULL */
}
/* print tree in-order */
void BH_print(struct BH_node* s)
{
if(s)
{
BH_print(s->left);
printf("Student's Priority: %s\n", s->priority);
printf("Student's Grade: %c\n", s->grade);
BH_print(s->right);
}
}
/* free() the tree using post-order */
void BH_free(struct BH_node* s)
{
if(s)
{
BH_free(s->left);
BH_free(s->right);
free(s);
}
}
============================== OUTPUT =================================
[arnuld@dune programs]$ gcc -ansi -pedantic -Wall -Wextra binary-heap.c
[arnuld@dune programs]$ ./a.out
Student's Priority: First Class
Student's Grade: A
Student's Priority: Second Class
Student's Grade: B
[arnuld@dune programs]$