47#include <gtest/gtest.h>
54using namespace testing;
68 std::vector<BinNode<int> *> allocated;
73 allocated.push_back(p);
78 for (
const auto * p : allocated)
86 std::vector<typename Node::key_type> keys;
95 ASSERT_TRUE(std::is_sorted(keys.begin(), keys.end()));
101 if (
root == Node::NullPtr)
110 bool operator()(
BinNode<int> * p,
const char * str)
const
123 return std::to_string(
KEY(p));
129 std::vector<BinNode<int> *> v;
130 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
131 v.push_back(it.get_curr_ne());
137 std::vector<BinNode<int> *> v;
138 for (
auto it =
l.
get_it(); it.has_curr(); it.next_ne())
139 v.push_back(it.get_curr_ne());
145 std::vector<int>
out;
147 for (
auto * p :
nodes)
155 auto pos = s.find(
needle);
156 if (pos == std::string::npos)
159 pos = s.find(
'{', pos);
160 if (pos == std::string::npos)
163 auto end = s.find(
'}', pos);
164 if (end == std::string::npos)
167 std::vector<unsigned char> bytes;
168 std::string
inside = s.substr(pos + 1, end - pos - 1);
175 bytes.push_back(
static_cast<unsigned char>(val));
176 while (
ss && (
ss.peek() ==
',' || std::isspace(
ss.peek())))
185 auto pos = s.find(
needle);
186 if (pos == std::string::npos)
189 pos = s.find(
'{', pos);
190 if (pos == std::string::npos)
193 auto end = s.find(
"};", pos);
194 if (end == std::string::npos)
197 std::string
inside = s.substr(pos + 1, end - pos - 1);
199 std::vector<std::string>
storage;
204 (std::isspace(
static_cast<unsigned char>(
inside[i])) ||
inside[i] ==
','))
209 if (
inside.compare(i, 7,
"nullptr") == 0)
226 token.push_back(
'\n');
228 token.push_back(
'\t');
248 auto *
root = pool.make(2);
264 auto *
root = pool.make(2);
268 std::vector<int>
in, pre,
post;
274 EXPECT_EQ(pre, (std::vector<int>{2, 1, 3}));
281 auto *
root = pool.make(4);
301 auto *
root = pool.make(2);
311 pre[0] = 4; pre[1] = 2; pre[2] = 1; pre[3] = 3; pre[4] = 6; pre[5] = 5; pre[6] = 7;
313 in[0] = 1;
in[1] = 2;
in[2] = 3;
in[3] = 4;
in[4] = 5;
in[5] = 6;
in[6] = 7;
326 in[0] = 1;
in[1] = 2;
in[2] = 3;
in[3] = 4;
in[4] = 5;
in[5] = 6;
in[6] = 7;
337 auto *
root = pool.make(2);
341 std::stringstream
ss;
355 std::stringstream
ss;
366 auto *
root = pool.make(2);
370 std::ostringstream
out;
371 const std::string name =
"t";
374 const std::string
gen =
out.str();
379 const std::string
key_var = name +
"_k";
383 std::vector<const char *> keys;
385 keys.push_back(s.c_str());
386 keys.push_back(
nullptr);
416 std::ostringstream
out;
417 const std::string name =
"s";
420 const std::string
gen =
out.str();
427 std::vector<const char *> keys;
429 keys.push_back(s.c_str());
430 keys.push_back(
nullptr);
436 (std::vector<std::string>{
"a\"b",
"root",
"c\\d"}));
445 auto *
root = pool.make(4);
482 auto * dup = pool.make(2);
504 auto *
root = pool.make(10);
517 auto * p = pool.make(5);
520 auto * q = pool.make(5);
533 for (
int k : {3, 1, 4, 2})
554 for (
int k : {1, 2, 3})
556 for (
int k : {4, 5, 6})
611 for (
int k : {1, 2, 3, 4, 5})
630 for (
int k : {1, 2, 3, 4, 5})
650 for (
int k : {1, 2, 3, 5, 6})
671 for (
int k : {5, 3, 7, 2, 4, 6, 8})
675 std::vector<int>
got;
682 EXPECT_EQ(
got, (std::vector<int>{2, 3, 4, 5, 6, 7, 8}));
689 auto * p = pool.make(2);
690 LLINK(p) = pool.make(1);
691 RLINK(p) = pool.make(3);
707 for (
int k : {5, 3, 7, 2, 4, 6, 8})
718 for (
int k : {5, 3, 7, 2, 4, 6, 8})
740 for (
int k : {5, 3, 7, 2, 4, 6, 8})
759 for (
int k : {5, 3, 7, 2, 4, 6, 8})
775 for (
int k : {2, 1, 3})
778 auto * p = pool.make(4);
791 for (
int k : {2, 1, 3})
795 auto * dup = pool.make(2);
804 for (
int k : {2, 1, 3})
810 auto *
fresh = pool.make(4);
820 for (
int k : {2, 1, 3})
823 auto * dup = pool.make(2);
832 bool operator()(
int a,
int b)
const noexcept {
return a > b; }
837 for (
const int k : {1, 2, 3, 4, 5})
858 auto *
root = pool.make(2);
868 const char * keys[] = {
"2",
"1",
"3",
nullptr};
910 std::mt19937
rng(12345);
911 std::uniform_int_distribution<int> dist(0, 200);
915 for (
int i = 0; i < 200; ++i)
918 auto * p = pool.make(
k);
926 for (
int i = 0; i < 100; ++i)
WeightedDigraph::Node Node
Inorder iterator on the nodes of a binary tree.
Node * get_curr_ne() const noexcept
Return the current link guaranteeing no exception. Be careful.
bool has_curr() const noexcept
Return true the iterator has current node.
Node for binary search tree.
Contiguous array of bits.
void push(const unsigned int value)
Inserts the value at the end of the array.
constexpr size_t size() const noexcept
Returns the dimension of the bit array.
Dynamic doubly linked list with O(1) size and bidirectional access.
Dynamic singly linked list with functional programming support.
T & insert(const T &item)
Insert a new item by copy.
void empty() noexcept
empty the list
size_t size() const noexcept
Count the number of elements of the list.
auto get_it() const
Return a properly initialized iterator positioned at the first item on the container.
__gmp_expr< T, __gmp_binary_expr< __gmp_expr< T, U >, unsigned long int, __gmp_root_function > > root(const __gmp_expr< T, U > &expr, unsigned long int l)
void delete_tree(Tree_Node< T > *node)
DynArray< Graph::Node * > nodes
DynList< typename GT::Node * > in_nodes(typename GT::Node *p, SA sa=SA())
Return the nodes connected to the filtered incoming arcs to p.
Node * rotate_to_left(Node *p) noexcept
Rotate to the left the tree with root p
Node * search_rank_parent(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Rank search of a key in a binary search tree.
void load_tree_keys_in_prefix(Node *root, std::istream &input)
Load the keys stored in preorder from an input stream.
DynDlist< Node * > compute_nodes_in_level(Node *root, const int &level)
Count the number of nodes in a specific tree level.
Node * join_preorder(Node *t1, Node *t2, Node *&dup, const Compare &cmp=Compare()) noexcept
Union of two binary search trees.
Node * remove_from_bst(Node *&root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Remove a key from a binary search tree.
Node * find_predecessor(Node *p, Node *&pp) noexcept
Find the inorder predecessor of p
Node * search_parent(Node *root, const typename Node::key_type &key, Node *&parent, const Compare &cmp=Compare()) noexcept
Search a key and find its node and parent.
Node * find_min(Node *root) noexcept
Return the minimum key contained in a binary search tree.
Node * rotate_to_right(Node *p) noexcept
Rotate to the right the tree with root p
bool split_key_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept
Split recursively according to a key.
Node * find_successor(Node *p, Node *&pp) noexcept
Find the inorder successor of p
Node * search_or_insert_in_bst(Node *&r, Node *p, const Compare &cmp=Compare()) noexcept
Search or insert a node in a binary search tree.
void save_tree(Node *root, std::ostream &output)
Store a binary tree in a stream.
void split_key_dup_rec(Node *&root, const typename Node::key_type &key, Node *&ts, Node *&tg, const Compare &cmp=Compare()) noexcept
Split a tree according to a key value.
bool check_bst(Node *p, const Compare &cmp=Compare())
Return true if p is a binary search tree.
constexpr Node *& RLINK(Node *p) noexcept
Return the right tree of p.
Node * bits_to_tree(const BitArray &array, int idx=0)
Build a binary tree given its bits code.
Node * insert_in_bst(Node *&r, Node *p, const Compare &cmp=Compare()) noexcept
Insert a node p in a binary search tree.
void tree_to_bits(Node *root, BitArray &array)
Compute a bit code for the binary tree.
size_t internal_path_length(Node *p) noexcept
Compute the internal path length.
void preOrderThreaded(Node *node, void(*visitFct)(Node *))
Traverse preorder a binary tree without recursion and without stack.
void inOrderThreaded(Node *root, void(*visitFct)(Node *))
Traverse inorder a binary tree without recursion and without stack.
Node * searchInBinTree(Node *root, const typename Node::key_type &key, const Compare &cmp=Compare()) noexcept
Search a key in a binary search tree.
Node * insert_dup_in_bst(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert a node p in a binary search tree.
Node * insert_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert the node p as root of a binary search tree.
Node * insert_dup_root(Node *&root, Node *p, const Compare &cmp=Compare()) noexcept
Insert node p as root of a binary search tree.
constexpr Node *& LLINK(Node *p) noexcept
Return a pointer to left subtree.
Node * insert_root_rec(Node *root, Node *p, const Compare &cmp=Compare()) noexcept
Insert a node as root in a binary search tree.
Node * find_max(Node *root) noexcept
Return the maximum key contained in a binary search tree.
void split_key(Node *&root, const Key &key, Node *&l, Node *&r, const Compare &cmp=Compare()) noexcept
Split a binary search tree according to a key.
Freq_Node * pred
Predecessor node in level-order traversal.
Main namespace for Aleph-w library functions.
size_t size(Node *root) noexcept
static void infix(Node *root, DynList< Node * > &acc)
static void suffix(Node *root, DynList< Node * > &acc)
static void prefix(Node *root, DynList< Node * > &acc)
std::ostream & join(const C &c, const std::string &sep, std::ostream &out)
Join elements of an Aleph-style container into a stream.
DynList< T > maps(const C &c, Op op)
Classic map operation.
Utility functions for binary tree operations.
Basic binary tree node definitions.
Lazy and scalable dynamic array implementation.
void inorder(int v[], int n, int i)
Write inorder traversal of heap.