Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
TREE: various tree with generic support

Data Structures

union  COORD_VALUE
 Union to store the coordinate values. More...
 
struct  NODE_DATA
 structure of a TREE node data More...
 
union  NODE_DATA_TYPES
 union of the possibles data values of a TREE node More...
 
struct  OCTREE
 structure of an OCTREE More...
 
struct  OCTREE_NODE
 structure of an OCTREE node More...
 
struct  POINT2D
 Structure for a POINT2D in the 2D space. More...
 
struct  POINT3D
 Structure for a POINT3D in the 3D space. More...
 
struct  QUADTREE
 structure of a quad tree More...
 
struct  QUADTREE_NODE
 structure of a quad tree node More...
 
struct  TREE
 structure of a TREE More...
 
struct  TREE_NODE
 structure of a n-ary TREE node More...
 

Typedefs

typedef int(* compare_func) (COORD_VALUE a, COORD_VALUE b)
 function pointer types for comparison
 
typedef void(* print_func) (COORD_VALUE val)
 function pointer types for debug print
 

Enumerations

enum  COORD_TYPE { COORD_INT , COORD_FLOAT , COORD_DOUBLE }
 Enum for coordinate types. More...
 

Functions

QUADTREE_NODEcreate_node (COORD_VALUE x, COORD_VALUE y, void *data_ptr)
 create a new quadtree node with position and data pointer
 
OCTREEcreate_octree (int type)
 create a new octree for the given coordinate type
 
OCTREE_NODEcreate_octree_node (POINT3D point, void *data_ptr)
 create a new octree node with position and data pointer
 
QUADTREEcreate_quadtree (int coord_type)
 create a new quadtree for the given coordinate type
 
void free_octree (OCTREE *OCTREE)
 free the entire octree and its root
 
void free_octree_node (OCTREE_NODE *node)
 recursively free an octree node and its children
 
void free_quadtree (QUADTREE_NODE *root)
 recursively free all nodes of a quadtree
 
void insert (QUADTREE *qt, QUADTREE_NODE **root, COORD_VALUE x, COORD_VALUE y, void *data_ptr)
 insert a point with data into the quadtree
 
void insert_octree (OCTREE *OCTREE, POINT3D point, void *data_ptr)
 insert a point with data into the octree
 
TREEnew_tree ()
 create a new empty n-ary TREE
 
QUADTREE_NODEsearch (QUADTREE *qt, QUADTREE_NODE *root, COORD_VALUE x, COORD_VALUE y)
 search for a point in the quadtree, return matching node or NULL
 
TREE_NODEtree_create_node (NODE_DATA value, void(*destroy_func)(void *ptr))
 create a TREE_NODE with the given value and optional destructor
 
int tree_delete_node (TREE *tree, TREE_NODE *node)
 delete a TREE_NODE and all its children from the tree
 
void tree_destroy (TREE **tree)
 destroy a TREE and all its nodes, set pointer to NULL
 
int tree_insert_child (TREE_NODE *parent, TREE_NODE *child)
 insert a child node under the given parent node
 

Detailed Description


Data Structure Documentation

◆ COORD_VALUE

union COORD_VALUE

Union to store the coordinate values.

Examples
ex_trees.c.

Definition at line 112 of file n_trees.h.

+ Collaboration diagram for COORD_VALUE:
Data Fields
double d
float f
int i

◆ NODE_DATA

struct NODE_DATA

structure of a TREE node data

Examples
ex_trees.c.

Definition at line 60 of file n_trees.h.

+ Collaboration diagram for NODE_DATA:
Data Fields
int32_t type node type
union NODE_DATA_TYPES value node value

◆ NODE_DATA_TYPES

union NODE_DATA_TYPES

union of the possibles data values of a TREE node

Definition at line 46 of file n_trees.h.

+ Collaboration diagram for NODE_DATA_TYPES:
Data Fields
double fval double type
int ival integral type
N_STR * nstr N_STR *type.
void * ptr pointer type
char * string char *type

◆ OCTREE

struct OCTREE

structure of an OCTREE

Examples
ex_trees.c.

Definition at line 197 of file n_trees.h.

+ Collaboration diagram for OCTREE:
Data Fields
int coord_type Coordinate type for the entire tree.
OCTREE_NODE * root tree list first node

◆ OCTREE_NODE

struct OCTREE_NODE

structure of an OCTREE node

Examples
ex_trees.c.

Definition at line 187 of file n_trees.h.

+ Collaboration diagram for OCTREE_NODE:
Data Fields
struct OCTREE_NODE * children[8] Child nodes.
void * data_ptr Pointer to additional data, can be NULL.
POINT3D point Point represented by this node.

◆ POINT2D

struct POINT2D

Structure for a POINT2D in the 2D space.

Definition at line 119 of file n_trees.h.

+ Collaboration diagram for POINT2D:
Data Fields
COORD_VALUE x x coordinate
COORD_VALUE y y coordinate

◆ POINT3D

struct POINT3D

Structure for a POINT3D in the 3D space.

Examples
ex_trees.c.

Definition at line 128 of file n_trees.h.

+ Collaboration diagram for POINT3D:
Data Fields
COORD_VALUE x x coordinate
COORD_VALUE y y coordinate
COORD_VALUE z z coordinate

◆ QUADTREE

struct QUADTREE

structure of a quad tree

Examples
ex_trees.c.

Definition at line 164 of file n_trees.h.

+ Collaboration diagram for QUADTREE:
Data Fields
compare_func compare pointer to comparison function
int coord_type type of coordinate used in the quad tree
print_func print pointer to print function
QUADTREE_NODE * root tree list first node

◆ QUADTREE_NODE

struct QUADTREE_NODE

structure of a quad tree node

Examples
ex_trees.c.

Definition at line 144 of file n_trees.h.

+ Collaboration diagram for QUADTREE_NODE:
Data Fields
void * data_ptr Pointer to data, can be NULL.
struct QUADTREE_NODE * ne North-East child.
struct QUADTREE_NODE * nw North-West child.
POINT2D point X,Y point.
struct QUADTREE_NODE * se South-East child.
struct QUADTREE_NODE * sw South-West child.
COORD_VALUE x X coordinate.
COORD_VALUE y Y coordinate.

◆ TREE

struct TREE

structure of a TREE

Examples
ex_trees.c.

Definition at line 82 of file n_trees.h.

+ Collaboration diagram for TREE:
Data Fields
size_t height height of the tree
size_t nb_nodes number of nodes in the tree
TREE_NODE * root pointer to first node
pthread_rwlock_t rwlock mutex for thread safety (optional)

◆ TREE_NODE

struct TREE_NODE

structure of a n-ary TREE node

Examples
ex_trees.c.

Definition at line 68 of file n_trees.h.

+ Collaboration diagram for TREE_NODE:

Data Fields

LISTchildren
 ordered list of children
 
NODE_DATA data
 structure holding values for node
 
void(* destroy_func )(void *ptr)
 value destructor if of type ptr and specified, else a simple free will be used
 
struct TREE_NODEparent
 pointer to parent
 
LIST_NODEparent_list_node
 pointer to parent container of the TREE_NODE, LIST_NODE
 

Field Documentation

◆ children

LIST* TREE_NODE::children

ordered list of children

Definition at line 78 of file n_trees.h.

Referenced by tree_create_node(), tree_delete_node(), and tree_insert_child().

◆ data

NODE_DATA TREE_NODE::data

structure holding values for node

Examples
ex_trees.c.

Definition at line 70 of file n_trees.h.

Referenced by main(), tree_create_node(), and tree_delete_node().

◆ destroy_func

void(* TREE_NODE::destroy_func) (void *ptr)

value destructor if of type ptr and specified, else a simple free will be used

Definition at line 72 of file n_trees.h.

Referenced by tree_create_node(), and tree_delete_node().

◆ parent

struct TREE_NODE* TREE_NODE::parent

pointer to parent

Definition at line 74 of file n_trees.h.

Referenced by tree_create_node(), tree_delete_node(), and tree_insert_child().

◆ parent_list_node

LIST_NODE* TREE_NODE::parent_list_node

pointer to parent container of the TREE_NODE, LIST_NODE

Definition at line 76 of file n_trees.h.

Referenced by tree_create_node(), tree_delete_node(), and tree_insert_child().

Typedef Documentation

◆ compare_func

typedef int(* compare_func) (COORD_VALUE a, COORD_VALUE b)

function pointer types for comparison

Definition at line 139 of file n_trees.h.

◆ print_func

typedef void(* print_func) (COORD_VALUE val)

function pointer types for debug print

Definition at line 141 of file n_trees.h.

Enumeration Type Documentation

◆ COORD_TYPE

enum COORD_TYPE

Enum for coordinate types.

Enumerator
COORD_INT 
COORD_FLOAT 
COORD_DOUBLE 

Definition at line 105 of file n_trees.h.

Function Documentation

◆ create_node()

QUADTREE_NODE * create_node ( COORD_VALUE  x,
COORD_VALUE  y,
void *  data_ptr 
)

create a new quadtree node with position and data pointer

create a new quadtree node with position and data pointer

Parameters
xx coordinate
yy coordinate
data_ptrnode private data pointer, can be NULL
Returns
a new QUADTREE_NODE or NULL

Definition at line 270 of file n_trees.c.

References QUADTREE_NODE::data_ptr, LOG_ERR, n_log, QUADTREE_NODE::ne, QUADTREE_NODE::nw, QUADTREE_NODE::se, QUADTREE_NODE::sw, QUADTREE_NODE::x, and QUADTREE_NODE::y.

Referenced by insert(), and main().

+ Here is the caller graph for this function:

◆ create_octree()

OCTREE * create_octree ( int  type)

create a new octree for the given coordinate type

create a new octree for the given coordinate type

Parameters
typethe type of coordinate used by the OCTREE (COORD_INT,COORD_FLOAT,COORD_DOUBLE)
Returns
a new empty OCTREE or NULL

Definition at line 382 of file n_trees.c.

References OCTREE::coord_type, LOG_ERR, n_log, and OCTREE::root.

Referenced by main().

+ Here is the caller graph for this function:

◆ create_octree_node()

OCTREE_NODE * create_octree_node ( POINT3D  point,
void *  data_ptr 
)

create a new octree node with position and data pointer

create a new octree node with position and data pointer

Parameters
pointthe POINT3D representing the coordinate
data_ptrthe data to insert. Can be NULL
Returns
a new OCTREE_NODE or NULL

Definition at line 361 of file n_trees.c.

References OCTREE_NODE::children, OCTREE_NODE::data_ptr, LOG_ERR, n_log, and OCTREE_NODE::point.

Referenced by insert_octree(), insert_octree_node(), and main().

+ Here is the caller graph for this function:

◆ create_quadtree()

QUADTREE * create_quadtree ( int  coord_type)

create a new quadtree for the given coordinate type

create a new quadtree for the given coordinate type

Parameters
coord_typethe type of nodes for the new tree (COORD_INT,COORD_FLOAT,COORD_DOUBLE)
Returns
a new empty QUADTREE

Definition at line 229 of file n_trees.c.

References QUADTREE::compare, compare_double(), compare_float(), compare_int(), COORD_DOUBLE, COORD_FLOAT, COORD_INT, QUADTREE::coord_type, Free, LOG_ERR, n_log, QUADTREE::print, print_double(), print_float(), print_int(), and QUADTREE::root.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ free_octree()

void free_octree ( OCTREE octree)

free the entire octree and its root

free the entire octree and its root

Parameters
octreethe OCTREE to free

Definition at line 467 of file n_trees.c.

References free_octree_node(), and OCTREE::root.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ free_octree_node()

void free_octree_node ( OCTREE_NODE node)

recursively free an octree node and its children

recursively free an octree node and its children

Parameters
nodethe starting node for the deletion

Definition at line 454 of file n_trees.c.

References OCTREE_NODE::children, and free_octree_node().

Referenced by free_octree(), free_octree_node(), and main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ free_quadtree()

void free_quadtree ( QUADTREE_NODE root)

recursively free all nodes of a quadtree

recursively free all nodes of a quadtree

Parameters
roottarget quad tree to free

Definition at line 344 of file n_trees.c.

References __n_assert, free_quadtree(), QUADTREE_NODE::ne, QUADTREE_NODE::nw, QUADTREE_NODE::se, and QUADTREE_NODE::sw.

Referenced by free_quadtree(), and main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insert()

void insert ( QUADTREE qt,
QUADTREE_NODE **  root,
COORD_VALUE  x,
COORD_VALUE  y,
void *  data_ptr 
)

insert a point with data into the quadtree

insert a point with data into the quadtree

Parameters
qtthe target quad tree
rootthe QUADTREE_NODE to insert
xx coordinate
yy coordinate
data_ptrnode private data pointer, can be NULL

Definition at line 294 of file n_trees.c.

References __n_assert, QUADTREE::compare, create_node(), insert(), LOG_ERR, and n_log.

Referenced by insert(), and main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ insert_octree()

void insert_octree ( OCTREE octree,
POINT3D  point,
void *  data_ptr 
)

insert a point with data into the octree

insert a point with data into the octree

Parameters
octreetarget OCTREE tree
pointthe POINT3D point to insert in the OCTREE
data_ptreventual private data for the point, can be NULL

Definition at line 442 of file n_trees.c.

References OCTREE::coord_type, create_octree_node(), insert_octree_node(), and OCTREE::root.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ new_tree()

TREE * new_tree ( )

create a new empty n-ary TREE

create a new empty n-ary TREE

Returns
a pointer to the new tree or NULL on failure

Definition at line 40 of file n_trees.c.

References __n_assert, height, LOG_ERR, Malloc, n_log, nb_nodes, root, and rwlock.

Referenced by main().

+ Here is the caller graph for this function:

◆ search()

QUADTREE_NODE * search ( QUADTREE qt,
QUADTREE_NODE root,
COORD_VALUE  x,
COORD_VALUE  y 
)

search for a point in the quadtree, return matching node or NULL

search for a point in the quadtree, return matching node or NULL

Parameters
qtthe target QUADTREE in which to search
rootthe starting QUADTREE_NODE in the tree
xx coordinate
yy coordinate
Returns
the QUADTREE_NODE found at coordinate or NULL

Definition at line 322 of file n_trees.c.

References QUADTREE::compare, QUADTREE_NODE::ne, QUADTREE_NODE::nw, QUADTREE_NODE::se, search(), QUADTREE_NODE::sw, QUADTREE_NODE::x, and QUADTREE_NODE::y.

Referenced by main(), and search().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tree_create_node()

TREE_NODE * tree_create_node ( NODE_DATA  value,
void(*)(void *ptr)  destroy_func 
)

create a TREE_NODE with the given value and optional destructor

create a TREE_NODE with the given value and optional destructor

Parameters
valueNODE_DATA containing the value to add
destroy_funca destroy function pointer for the eventual private data_ptr that will later on be attached to the node
Returns
a pointer to the new node or NULL on failure

Definition at line 59 of file n_trees.c.

References TREE_NODE::children, TREE_NODE::data, TREE_NODE::destroy_func, Free, LOG_ERR, MAX_LIST_ITEMS, n_log, new_generic_list(), TREE_NODE::parent, and TREE_NODE::parent_list_node.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tree_delete_node()

int tree_delete_node ( TREE tree,
TREE_NODE node 
)

delete a TREE_NODE and all its children from the tree

delete a TREE_NODE and all its children from the tree

Parameters
treethe TREE in which we want to delete a node
nodethe TREE node we want to delete
Returns
TRUE on success or FALSE on failure

Definition at line 118 of file n_trees.c.

References TREE_NODE::children, TREE_NODE::data, TREE_NODE::destroy_func, list_destroy(), nb_nodes, LIST_NODE::next, TREE_NODE::parent, TREE_NODE::parent_list_node, LIST_NODE::ptr, NODE_DATA_TYPES::ptr, remove_list_node_f(), LIST::start, tree_delete_node(), and NODE_DATA::value.

Referenced by main(), tree_delete_node(), and tree_destroy().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tree_destroy()

void tree_destroy ( TREE **  tree)

destroy a TREE and all its nodes, set pointer to NULL

destroy a TREE and all its nodes, set pointer to NULL

Parameters
treea pointer to the pointer of the TREE to destroy

Definition at line 154 of file n_trees.c.

References tree_delete_node().

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ tree_insert_child()

int tree_insert_child ( TREE_NODE parent,
TREE_NODE child 
)

insert a child node under the given parent node

insert a child node under the given parent node

Parameters
parentthe parent TREE_NODE
childthe new TREE_NODE to add as a child
Returns
TRUE on success or FALSE on failure

Definition at line 85 of file n_trees.c.

References __n_assert, TREE_NODE::children, LIST_NODE::destroy_func, Free, list_node_push(), LOG_ERR, MAX_LIST_ITEMS, n_log, new_generic_list(), new_list_node(), TREE_NODE::parent, TREE_NODE::parent_list_node, and LIST_NODE::ptr.

Referenced by main().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: