/data_structures

A variety of data structures implemented in C

Primary LanguageCMIT LicenseMIT

Data Structures

A variety of data structures implemented in C, for learning purposes.

Usage

make run

to compile and run examples for all data structures under the main.c file.

Binary Tree

Simple binary tree with integer keys.

Usage

BTNode *root = NULL;
root = bt_insert(root, 20);
root = bt_insert(root, 15);
root = bt_insert(root, 35);

// Node with the lowest key
BTNode *min = bt_find_min(root);

// Node with the greatest key
BTNode *max = bt_find_max(root);

// Node with key == 20
BTNode *ten_node = bt_find(root, 20);

// Delete keys
root = bt_delete(root, 15);

// Print keys in increasing order
// In this case prints `15 20 35`
bt_in_order_traversal(root);

// Clean up afterwards
bt_destroy(root);

Dynamic Array

A list of integers that grows dynamically as more elements are appended.

Usage

List list;
list_init(&list);

// Append value 10
list_push(&list, 10);

// Retrieve value at index 0 (10 in this case).
int result = list_get(&list, 0);

// Clean up afterwards
list_destroy(&list);

Hash Table

A very simple hash table with integers as both keys and values, using linear probing to resolve collisions and

key --> key * (key + 3) % capacity

as the hash function.

Usage

HashTable ht = ht_create();
ht_set(&ht, 10, 20);

// Get key. If there's a hit, `found` will be true and the result
// will be set in the pointer passed as the last argument.
// In this case, `found` will be true and `result` == 20.
int result = 0;
bool found = ht_get(&ht, 10, &result);

// Here, `found` is false and `result` is not modified.
bool found = ht_get(&ht, 100, &result);

// Clean up afterwards
ht_destroy(&ht)