data_structures
bst_delete.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* bst_delete.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/18 00:24:47 by unite #+# #+# */
10 /* Updated: 2020/07/18 00:28:45 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "bst.h"
15 
16 void bst_delete_node(t_bst *bst, t_bst_node *node)
17 {
18  if (node == NULL)
19  return ;
20  bst->val_type->del(node->val);
21  bst->key_type->del(node->key);
22  free(node);
23 }
24 
25 void bst_delete_recur(t_bst *bst, t_bst_node *node)
26 {
27  if (node == NULL)
28  return ;
29  bst_delete_recur(bst, node->left);
30  bst_delete_recur(bst, node->right);
31  bst_delete_node(bst, node);
32 }
33 
34 void bst_delete(t_bst *bst)
35 {
36  if (bst == NULL)
37  return ;
38  bst_delete_recur(bst, bst->root);
39  free(bst);
40 }
s_bst_node
A node in a binary search tree.
Definition: bst.h:36
s_bst_node::val
void * val
The value.
Definition: bst.h:39
s_bst::val_type
const t_type * val_type
The type of values in the ree.
Definition: bst.h:62
s_bst::key_type
const t_type * key_type
The type of keys in the tree.
Definition: bst.h:61
s_bst_node::right
struct s_bst_node * right
The right child.
Definition: bst.h:41
s_bst_node::left
struct s_bst_node * left
The left child.
Definition: bst.h:40
bst_delete
void bst_delete(t_bst *bst)
Deletes this tree and free all its items and the associated data.
Definition: bst_delete.c:34
bst.h
s_type::del
void(* del)(void *)
A function pointer used to free the memory taken by the data type.
Definition: types.h:51
s_bst_node::key
void * key
The key.
Definition: bst.h:38
s_bst
A binary search tree.
Definition: bst.h:57
s_bst::root
t_bst_node * root
The root of the tree.
Definition: bst.h:59