47 lines
1.6 KiB
C
47 lines
1.6 KiB
C
#ifndef VIETER_TREE_BALANCING
|
|
#define VIETER_TREE_BALANCING
|
|
|
|
#include "vieter_tree_node.h"
|
|
|
|
/*
|
|
* Check whether the given tree is a valid red-black tree.
|
|
*
|
|
* @param node root of the (sub)tree
|
|
* @param passed_black_nodes how many black nodes the recursion has already seen
|
|
* at this point. This should be initialized as 0 for the topmost call.
|
|
* @param expected_black_nodes the correct amount of black nodes to expect when
|
|
* a NULL child is encountered.
|
|
* @return true if the tree is valid, false otherwise
|
|
*/
|
|
bool vieter_tree_node_validate(vieter_tree_node *node,
|
|
uint64_t passed_black_nodes,
|
|
uint64_t expected_black_nodes);
|
|
|
|
/*
|
|
* Balance a path of 3 nodes into a complete binary tree, with a red root and
|
|
* black children.
|
|
*/
|
|
vieter_tree_node *vieter_tree_node_balance(vieter_tree_node *node);
|
|
|
|
/*
|
|
* Ensure the tree remains a valid red-black tree after having inserting the
|
|
* node.
|
|
*/
|
|
void vieter_tree_node_balance_after_insert(vieter_tree_node *node);
|
|
|
|
/*
|
|
* Remove the given node, ensuring the tree remains a valid red-black tree.
|
|
*
|
|
* @param node node to remove. This should have at most a single child. If node
|
|
* isn't the root, this should be the right child, otherwise it can be either.
|
|
* @return root of the subtree that this function operated on. This can
|
|
* sometimes be the new root of the entire tree.
|
|
*/
|
|
vieter_tree_node *vieter_tree_node_remove_balanced(vieter_tree_node *node);
|
|
|
|
/*
|
|
* Perform a tree rotation of the subtree with the given root.
|
|
*/
|
|
vieter_tree_node *vieter_tree_node_rotate(vieter_tree_node *old_root, bool dir);
|
|
#endif
|