C Fundamentals
Algorithms · data structures · cryptography · systems — pure C11, zero deps
Loading...
Searching...
No Matches
trie.c File Reference
#include "trie.h"
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

Go to the source code of this file.

Classes

struct  trie
 
struct  trie_node
 

Typedefs

typedef struct trie_node trie_node_t
 

Functions

static int idx_of (char c)
 
static trie_node_tnode_create (void)
 
static void node_destroy (trie_node_t *n)
 
bool trie_contains (const trie_t *t, const char *word)
 
trie_ttrie_create (void)
 
void trie_destroy (trie_t *t)
 
bool trie_insert (trie_t *t, const char *word)
 
bool trie_starts_with (const trie_t *t, const char *prefix)
 
static const trie_node_twalk (const trie_t *t, const char *s)
 

Typedef Documentation

◆ trie_node_t

typedef struct trie_node trie_node_t

Function Documentation

◆ idx_of()

static int idx_of ( char  c)
static

Definition at line 43 of file trie.c.

Referenced by trie_insert(), and walk().

◆ node_create()

static trie_node_t * node_create ( void  )
static

Definition at line 19 of file trie.c.

Referenced by trie_create(), and trie_insert().

◆ node_destroy()

static void node_destroy ( trie_node_t n)
static

Definition at line 23 of file trie.c.

References trie_node::children, and node_destroy().

Referenced by node_destroy(), and trie_destroy().

◆ trie_contains()

bool trie_contains ( const trie_t t,
const char *  word 
)

Definition at line 76 of file trie.c.

References trie_node::terminal, and walk().

◆ trie_create()

trie_t * trie_create ( void  )

Definition at line 29 of file trie.c.

References node_create(), and trie::root.

◆ trie_destroy()

void trie_destroy ( trie_t t)

Definition at line 37 of file trie.c.

References node_destroy(), and trie::root.

◆ trie_insert()

bool trie_insert ( trie_t t,
const char *  word 
)

Inserts a lowercase a-z word. Returns false on allocation failure or if the word contains a non-{a-z} character.

Definition at line 49 of file trie.c.

References trie_node::children, idx_of(), node_create(), trie::root, and trie_node::terminal.

◆ trie_starts_with()

bool trie_starts_with ( const trie_t t,
const char *  prefix 
)

True if any inserted word begins with prefix.

Definition at line 81 of file trie.c.

References walk().

◆ walk()

static const trie_node_t * walk ( const trie_t t,
const char *  s 
)
static

Definition at line 65 of file trie.c.

References trie_node::children, idx_of(), and trie::root.

Referenced by trie_contains(), and trie_starts_with().