52 lines
1.5 KiB
C
52 lines
1.5 KiB
C
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include "bintree.h"
|
|
|
|
// Vergleichsfunktion für unsigned int
|
|
static int cmp_uint_ptr(const void *a, const void *b)
|
|
{
|
|
unsigned int va = *(const unsigned int *)a;
|
|
unsigned int vb = *(const unsigned int *)b;
|
|
if(va < vb) return -1;
|
|
if(va > vb) return 1;
|
|
return 0;
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
TreeNode *root = NULL;
|
|
unsigned int vals[] = {5, 2, 8, 1, 3, 7, 9};
|
|
const size_t n = sizeof(vals)/sizeof(vals[0]);
|
|
|
|
// Werte in den Baum einfügen
|
|
for(size_t i = 0; i < n; i++)
|
|
{
|
|
int isDup = 0;
|
|
root = addToTree(root, &vals[i], sizeof(unsigned int), cmp_uint_ptr, &isDup);
|
|
if(root == NULL && isDup == 0) { fprintf(stderr, "addToTree allocation failed\n"); return 1; }
|
|
}
|
|
|
|
// Baumgröße prüfen
|
|
unsigned int sz = treeSize(root);
|
|
if(sz != n) { fprintf(stderr, "treeSize expected %zu got %u\n", n, sz); clearTree(root); return 2; }
|
|
|
|
// Inorder-Traversierung (muss sortiert sein)
|
|
unsigned int last = 0;
|
|
int first = 1;
|
|
unsigned int *data = nextTreeData(root);
|
|
while(data != NULL)
|
|
{
|
|
unsigned int v = *data;
|
|
// Prüfen, ob Reihenfolge korrekt ist
|
|
if(!first && v < last) { fprintf(stderr, "inorder traversal not sorted: %u after %u\n", v, last); clearTree(root); return 3; }
|
|
last = v; first = 0;
|
|
data = nextTreeData(NULL); // nächstes Element holen
|
|
}
|
|
|
|
clearTree(root); // Speicher freigeben
|
|
printf("test_bintree: OK\n");
|
|
return 0;
|
|
}
|
|
|