3
0
mirror of https://github.com/CrazyRedMachine/popnhax.git synced 2024-11-30 16:54:29 +01:00
popnhax/util/bst.cc

34 lines
722 B
C++

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "bst.h"
bst_t* bst_search(bst_t *root, uint32_t val)
{
if( root == NULL || root->data == val )
return root;
else if( val > (root->data) )
return bst_search(root->right, val);
else
return bst_search(root->left,val);
}
bst_t* bst_insert(bst_t *root, uint32_t val)
{
if ( root == NULL )
{
bst_t *p;
p = (bst_t *)malloc(sizeof(bst_t));
p->data = val;
p->left = NULL;
p->right = NULL;
return p;
}
else if ( val > root->data )
root->right = bst_insert(root->right, val);
else
root->left = bst_insert(root->left, val);
return root;
}