data_structures
bst_get.c
Go to the documentation of this file.
1 /* ************************************************************************** */
3 /* */
4 /* ::: :::::::: */
5 /* bst_get.c :+: :+: :+: */
6 /* +:+ +:+ +:+ */
7 /* By: unite <marvin@42.fr> +#+ +:+ +#+ */
8 /* +#+#+#+#+#+ +#+ */
9 /* Created: 2020/07/17 22:11:54 by unite #+# #+# */
10 /* Updated: 2020/07/18 16:55:22 by unite ### ########.fr */
11 /* */
12 /* ************************************************************************** */
13 
14 #include "bst.h"
15 
16 void *bst_get(const t_bst *bst, const void *key)
17 {
18  t_bst_node *node;
19  int cmp;
20 
21  node = bst->root;
22  while (node != NULL)
23  {
24  cmp = bst->key_type->cmp(key, node->key);
25  if (cmp < 0)
26  node = node->left;
27  else if (cmp > 0)
28  node = node->right;
29  else
30  return (node->val);
31  }
32  return (NULL);
33 }
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_type::cmp
int(* cmp)(const void *, const void *)
(optional) A function ponter used to compare members of this data type
Definition: types.h:52
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.h
s_bst_node::key
void * key
The key.
Definition: bst.h:38
bst_get
void * bst_get(const t_bst *bst, const void *key)
Returns the value associated with a specified key.
Definition: bst_get.c:16
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