6 Module TREE
gullradriel edited this page 2026-10-29 20:54:40 +01:00

TREE: various tree with generic support

Structures

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

Types

Name
typedef void(*)(COORD_VALUE val) print_func
function pointer types for debug print
typedef int(*)(COORD_VALUE a, COORD_VALUE b) compare_func
function pointer types for comparison
enum COORD_TYPE { COORD_INT, COORD_FLOAT, COORD_DOUBLE}
Enum for coordinate types.

Functions

Name
int tree_insert_child(TREE_NODE * parent, TREE_NODE * child)
insert a child node under the given parent node
void tree_destroy(TREE ** tree)
destroy a TREE and all its nodes, set pointer to NULL
int tree_delete_node(TREE * tree, TREE_NODE * node)
delete a TREE_NODE and all its children from the tree
TREE_NODE * tree_create_node(NODE_DATA value, void(*)(void *ptr) destroy_func)
create a TREE_NODE with the given value and optional destructor
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
TREE * new_tree()
create a new empty n-ary TREE
void insert_octree(OCTREE * OCTREE, POINT3D point, void * data_ptr)
insert a point with data into the octree
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 free_quadtree(QUADTREE_NODE * root)
recursively free all nodes of a quadtree
void free_octree_node(OCTREE_NODE * node)
recursively free an octree node and its children
void free_octree(OCTREE * OCTREE)
free the entire octree and its root
QUADTREE * create_quadtree(int coord_type)
create a new quadtree for the given coordinate type
OCTREE_NODE * create_octree_node(POINT3D point, void * data_ptr)
create a new octree node with position and data pointer
OCTREE * create_octree(int type)
create a new octree for the given coordinate type
QUADTREE_NODE * create_node(COORD_VALUE x, COORD_VALUE y, void * data_ptr)
create a new quadtree node with position and data pointer

Types Documentation

typedef print_func

typedef void(* print_func) (COORD_VALUE val);

function pointer types for debug print

typedef compare_func

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

function pointer types for comparison

enum COORD_TYPE

Enumerator Value Description
COORD_INT
COORD_FLOAT
COORD_DOUBLE

Enum for coordinate types.

Functions Documentation

function tree_insert_child

int tree_insert_child(
    TREE_NODE * parent,
    TREE_NODE * child
)

insert a child node under the given parent node

Parameters:

Return: TRUE on success or FALSE on failure

insert a child node into the parent node

insert a child node under the given parent node

function tree_destroy

void tree_destroy(
    TREE ** tree
)

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

Parameters:

  • tree a pointer to the pointer of the TREE to destroy

destroy a TREE

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

function tree_delete_node

int tree_delete_node(
    TREE * tree,
    TREE_NODE * node
)

delete a TREE_NODE and all its children from the tree

Parameters:

  • tree the TREE in which we want to delete a node
  • node the TREE node we want to delete

Return: TRUE on success or FALSE on failure

delete a TREE node

delete a TREE_NODE and all its children from the tree

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

Parameters:

  • value NODE_DATA containing the value to add
  • destroy_func a destroy function pointer for the eventual private data_ptr that will later on be attached to the node

Return: a pointer to the new node or NULL on failure

create a TREE node

create a TREE_NODE with the given value and optional destructor

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

Parameters:

  • qt the target QUADTREE in which to search
  • root the starting QUADTREE_NODE in the tree
  • x x coordinate
  • y y coordinate

Return: the QUADTREE_NODE found at coordinate or NULL

Function to search for a point in the quad tree.

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

function new_tree

TREE * new_tree()

create a new empty n-ary TREE

Return: a pointer to the new tree or NULL on failure

create a new TREE

create a new empty n-ary TREE

function insert_octree

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

insert a point with data into the octree

Parameters:

  • octree target OCTREE tree
  • point the POINT3D point to insert in the OCTREE
  • data_ptr eventual private data for the point, can be NULL

Insert a point into the OCTREE.

insert a point with data into the octree

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

Parameters:

  • qt the target quad tree
  • root the QUADTREE_NODE to insert
  • x x coordinate
  • y y coordinate
  • data_ptr node private data pointer, can be NULL

Function to insert a point into the quad tree.

insert a point with data into the quadtree

function free_quadtree

void free_quadtree(
    QUADTREE_NODE * root
)

recursively free all nodes of a quadtree

Parameters:

  • root target quad tree to free

Function to free the quad tree.

recursively free all nodes of a quadtree

function free_octree_node

void free_octree_node(
    OCTREE_NODE * node
)

recursively free an octree node and its children

Parameters:

  • node the starting node for the deletion

recursive function to free an OCTREE node and its children

recursively free an octree node and its children

function free_octree

void free_octree(
    OCTREE * OCTREE
)

free the entire octree and its root

Parameters:

free the OCTREE

free the entire octree and its root

function create_quadtree

QUADTREE * create_quadtree(
    int coord_type
)

create a new quadtree for the given coordinate type

Parameters:

  • coord_type the type of nodes for the new tree (COORD_INT,COORD_FLOAT,COORD_DOUBLE)

Return: a new empty QUADTREE

Function to create a new quad tree.

create a new quadtree for the given coordinate type

function create_octree_node

OCTREE_NODE * create_octree_node(
    POINT3D point,
    void * data_ptr
)

create a new octree node with position and data pointer

Parameters:

  • point the POINT3D representing the coordinate
  • data_ptr the data to insert. Can be NULL

Return: a new OCTREE_NODE or NULL

create and OCTREE node

create a new octree node with position and data pointer

function create_octree

OCTREE * create_octree(
    int type
)

create a new octree for the given coordinate type

Parameters:

  • type the type of coordinate used by the OCTREE (COORD_INT,COORD_FLOAT,COORD_DOUBLE)

Return: a new empty OCTREE or NULL

Create a new OCTREE with a specified coordinate type.

create a new octree for the given coordinate type

function 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

Parameters:

  • x x coordinate
  • y y coordinate
  • data_ptr node private data pointer, can be NULL

Return: a new QUADTREE_NODE or NULL

function to create a new quad tree node

create a new quadtree node with position and data pointer