Coder Social home page Coder Social logo

qwinci / red_black_tree Goto Github PK

View Code? Open in Web Editor NEW
0.0 1.0 0.0 6 KB

Hopefully correct implementations of Red-Black tree written in c and c++.

License: BSD 3-Clause "New" or "Revised" License

CMake 0.97% C 57.05% C++ 41.98%
c cpp red-black-tree

red_black_tree's Introduction

Red-Black tree implementation in C and C++

Usage in C

Macros

Macro RBTREE_DECL_STRUCTS(Name, Node, K, T) is used to declare a Red-Black tree struct and node struct.

  • Name: Name of the main tree struct.
  • Node: Name of the tree node struct.
  • K: Key type.
  • T: Value type.

Macro RBTREE_IMPL(Name, Node, K, T, Mods...) is used to define the Red-Black tree functions.

  • Name: Name of the main tree struct.
  • Node: Name of the tree node struct.
  • K: Key type.
  • T: Value type.
  • Mods: Optional modifiers to apply to before function definitions, like static can be used to keep the tree functions local to a file.

Macro RBTREE_DECL_FUNCTIONS(Name, Node, K, T) is used to forward-declare the functions.

Code Example

#include "rb_tree.h"
#include "rb_tree_def.h"
#include <stdio.h>
#include <stdlib.h>

typedef struct {
	int my_int;
} Data;

RBTREE_DECL_STRUCTS(RBTree, Node, int, int)
RBTREE_IMPL(RBTree, Node, int, int)

int main() {
	RBTree tree = RBTree_new();
	
	// New nodes can be allocated however you like
	Node* node = malloc(sizeof(Node));
	
	// Inserting a new value into the tree
	RBTree_insert(&tree, 0, (Data) {10}, node);
	
	// Getting the height of the tree
	intmax_t height = RBTree_height(&tree);
	
	// Searching from the tree
	Node* searchedNode = RBTree_search(&tree, 10);
	printf("%d\n", searchedNode->value);
	
	// Getting the successor of a node
	Node* successor = RBTree_successor(&tree, searchedNode);
	
	// Getting the predecessor of a node
	Node* predecessor = RBTree_predecessor(&tree, searchedNode);
	
	// Deleting from the tree
	Node* deletedNode = RBTree_remove(&tree, searchedNode);
	
	// deletedNode can now be freed
	free(deletedNode);
}

Usage in C++

#include "rb_tree.hpp"
#include <iostream>

struct Data {
	int my_int;
};

int main() {
	RBTree<int, Data> tree;
	
	// New nodes can be allocated anywhere you would like
	auto node = new RBTree<int, Data>::Node();
	
	// Inserting a new value into the tree
	tree.insert(0, Data(10), node);
	
	// Getting the height of the tree
	intmax_t height = tree.height();
	
	// Searching from the tree
	auto searchedNode = tree.search(10);
	std::cout << searchedNode->value << std::endl;
	
	// Getting the successor of a node
	auto successor = tree.successor(searchedNode);
	
	// Getting the predecessor of a node
	auto predecessor = tree.predecessor(searchedNode);
	
	// Deleting from the tree
	auto deletedNode = tree.remove(searchedNode);
	
	// deletedNode can now be freed
	delete deletedNode;
}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.