Weak AVL Tree Implementation 3

Fri, Aug 9 2024 13:43:20 KST

I’ll create a wrapper API for easy use.

typedef struct _Tree  Tree;
struct _Tree {
  Node* root;
};

Tree* tree_new ()
{
  Tree* tree = malloc (sizeof (Tree));
  tree->root = nullptr;
  return tree;
}

void tree_insert (Tree* tree, int key)
{
  tree->root = insert (tree->root, key);
}

void tree_free (Tree* tree)
{
  node_free (tree->root);
  free (tree);
}

void tree_traverse (Tree* tree)
{
  traverse (tree->root);
}

I use the wrapper API like this:

int main ()
{
  int numbers[] = {
    25,
    9, 33,
    5, 13, 29, 59,
    2, 11, 20, 31,
    18, 23
  };

  Tree* tree = tree_new ();

  for (int i = 0; i < sizeof (numbers) / sizeof (numbers[0]); i++)
    tree_insert (tree, numbers[i]);

  tree_traverse (tree);
  tree_free (tree);

  return 0;
}