Skip to content

exgit/Avl-Tree

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 

Repository files navigation

AVL Tree

This is a library which implements AVL Tree functionality.

Usage

Include "avltree.h" to your source file.

#include "avltree.h"

Create your datatype and embed library datatype in it.

struct mydata
{
  int myvalue1;
  struct eg_avltree_node avlnode;
};

Declare a macro to convert from library datatype to your datatype

#define MYDATAPTR(pnode) BASEPTR_TO_DERIVEDPTR(pnode, struct mydata, avlnode)

Define a comparison function

int mycmpfunc(struct eg_avltree_node *node1, struct eg_avltree_node *node2)
{
  struct mydata *p1 = MYDATAPTR(node1);
  struct mydata *p2 = MYDATAPTR(node2);

  return p1->myvalue - p2->myvalue;
}

Declare and initialize an AVL Tree

struct eg_avltree tree;

eg_avltree_init(&tree, mycmpfunc);

Use a tree

struct mydata data;
struct mydata tmp;
struct mydata *presult;
struct eg_avltree_node *pnode;

data.myvalue = 5;
eg_avltree_insert(&tree, &data.avlnode);

tmp.myvalue = 5;
pnode = eg_avltree_find(&tree, &tmp.avlnode);
if (pnode)
  presult = MYDATAPTR(pnode);  // 'presult' now points to 'data'

About

No description or website provided.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages