项目作者: mateuszstompor

项目描述 :
a generic data structure written in pure C, uses macros heavily 😂
高级语言: C
项目地址: git://github.com/mateuszstompor/BinarySearchTree.git
创建时间: 2018-06-19T13:15:54Z
项目社区:https://github.com/mateuszstompor/BinarySearchTree

开源协议:MIT License

下载


BinarySearchTree · License: MIT Build Status

The aim of this repository is to demonstrate how a generic data structure in pure C can be implemented.
I investigated here an approach that utilizes macros.
It seems a convenient way to make a data structure reusable keeping strong typing at the same time.
To find out more information about the topic I suggest using official documentation

About the data structure

binary search tree (BST), also called an ordered or sorted binary tree, is a rooted binary tree data structure with the key of each internal node being greater than all the keys in the respective node’s left subtree and less than the ones in its right subtree

Wikipedia

Declaration

To start using the tree of a certain type you must perform two steps.
<type> in the examples below refers to the actual C type, such as int, float or any other complex type that you define.

  1. Declare a tree of a certain type
    1. binary_search_tree(<type>);
  2. Declare comparison macro
    1. #define binary_search_tree_<type>_is_greater(a, b) <bool>

Example - Insertion

  1. #include <binary_search_tree.h>
  2. typedef struct Point2D {
  3. float x;
  4. float y;
  5. } Point2D;
  6. binary_search_tree(Point2D);
  7. // Define how elements should be compared against each other
  8. #define binary_search_tree_Point2D_is_greater(a, b) a.x > b.x
  9. int main() {
  10. Point2D p = {2.3f, 3.0f};
  11. struct binary_search_tree_Point2D * point2DTree;
  12. binary_search_tree_alloc(Point2D, point2DTree);
  13. binary_search_tree_insert(Point2D, point2DTree, p);
  14. binary_search_tree_release(Point2D, point2DTree);
  15. }

Example - Traversal

The tree provides a way to traverse its values.
It uses the concept of an iterator but is implemented purely in a procedural manner.
It means that objects representing end or begin of tree cannot be generated on the fly.

  1. #include <stdio.h>
  2. #include <binary_search_tree.h>
  3. binary_search_tree(int);
  4. #define binary_search_tree_int_is_greater(a, b) a > b
  5. int main() {
  6. struct binary_search_tree_int * intTree;
  7. binary_search_tree_alloc(int, intTree);
  8. binary_search_tree_insert(int, intTree, 2);
  9. binary_search_tree_insert(int, intTree, 1);
  10. struct binary_search_tree_iterator_int * it, * end;
  11. binary_search_tree_iterator_alloc(int, it);
  12. binary_search_tree_iterator_alloc(int, end);
  13. binary_search_tree_begin(int, intTree, it);
  14. binary_search_tree_end(int, intTree, end);
  15. while (it->current != end->current) {
  16. printf("%i\n", it->current->data);
  17. binary_search_tree_iterator_next(int, it);
  18. }
  19. binary_search_tree_iterator_release(int, it);
  20. binary_search_tree_iterator_release(int, end);
  21. binary_search_tree_release(int, intTree);
  22. }

Requirements

  • cmake 3.7.0
  • docker (optional, tests execution)

Running tests

  1. Navigate to the repository’s root folder
  2. Build the image
    1. docker build -f ./tests/Dockerfile -t bst-tests .
  3. Run the container
    1. docker run --rm bst-tests