1 # ifndef TPL_BINNODEUTILS_H
2 # define TPL_BINNODEUTILS_H
4 # include <ahFunction.H>
5 # include <tpl_arrayStack.H>
6 # include <tpl_arrayQueue.H>
7 # include <tpl_dynListQueue.H>
9 # include <tpl_dynDlist.H>
12 using namespace Aleph;
15 template <
class Node>
inline static
16 void __inorder_rec(Node * node,
const int& level,
int & position,
17 void (*visitFct)(Node *,
int,
int))
19 if (node == Node::NullPtr)
22 __inorder_rec((Node*)
LLINK(node), level + 1, position, visitFct);
24 (*visitFct)(node, level, position);
27 __inorder_rec((Node*)
RLINK(node), level + 1, position, visitFct);
50 template <
class Node>
inline
51 int inOrderRec(Node * root,
void (*visitFct)(Node*,
int,
int))
54 __inorder_rec(root, 0, position, visitFct);
57 template <
class Node>
inline static
58 void __preorder_rec (Node * p,
const int & level,
int & position,
59 void (*visitFct)(Node*,
int,
int))
61 if (p == Node::NullPtr)
64 (*visitFct)(p, level, position);
67 __preorder_rec((Node*)
LLINK(p), level + 1, position, visitFct);
68 __preorder_rec((Node*)
RLINK(p), level + 1, position, visitFct);
92 template <
class Node>
inline
93 int preOrderRec(Node * root,
void (*visitFct)(Node*,
int,
int))
96 __preorder_rec(root, 0, position, visitFct);
99 template <
class Node>
inline static
100 void __postorder_rec(Node * node,
const int & level,
int & position,
101 void (*visitFct)(Node*,
int,
int))
103 if (node == Node::NullPtr)
106 __postorder_rec((Node*)
LLINK(node), level + 1, position, visitFct);
107 __postorder_rec((Node*)
RLINK(node), level + 1, position, visitFct);
109 (*visitFct)(node, level, position);
133 template <
class Node>
inline
137 __postorder_rec(root, 0, position, visitFct);
171 template <
class Node,
class Op>
174 static void for_each_inorder(Node * root, Op & op)
176 if (root == Node::NullPtr)
179 for_each_inorder((Node*)
LLINK(root), op);
181 for_each_inorder((Node*)
RLINK(root), op);
189 for_each_inorder(root, op);
195 for_each_inorder(root, op);
204 template <
class Node,
class Operation>
inline
205 bool traverse(Node * root, Operation & operation)
207 if (root == Node::NullPtr)
210 return traverse<Node, Operation>((Node*)
LLINK(root), operation) and
212 traverse<Node, Operation>((Node*)
RLINK(root), operation);
215 template <class Node, class Operation> inline
216 bool traverse(Node * root, Operation && operation = Operation())
218 return traverse<Node, Operation>(root, operation);
259 template <
class Node,
class Op>
264 void inorder(Node * root, Op & op)
266 if (root == Node::NullPtr or to_leave)
269 for_each_inorder((Node*)
LLINK(root), op);
271 for_each_inorder((Node*)
RLINK(root), op);
278 void leave() { to_leave =
true; }
324 template <
class Node,
class Op>
327 static void preorder(Node * root, Op & op)
329 if (root == Node::NullPtr)
333 preorder((Node*)
LLINK(root), op);
334 preorder((Node*)
RLINK(root), op);
406 template <
class Node,
class Op>
409 static void postorder(Node * root, Op & op)
411 if (root == Node::NullPtr)
414 postorder((Node*)
LLINK(root), op);
415 postorder((Node*)
RLINK(root), op);
459 template <
class Node>
inline
462 if (node == Node::NullPtr)
474 (*visitFct) (p, stack.
size(), count++);
476 if (
RLINK(p) != Node::NullPtr)
479 if (
LLINK(p) != Node::NullPtr)
511 template <
class Node>
inline
514 if (node == Node::NullPtr)
523 (*visitFct)(p, stack.
size(), count++);
525 if (
LLINK(p) != Node::NullPtr)
533 if (
RLINK(p) != Node::NullPtr)
571 template <
class Node>
inline
574 if (node == Node::NullPtr)
583 if (
LLINK(p) != Node::NullPtr)
591 (*visitFct)(p, stack.
size(), count++);
593 if (
RLINK(p) != Node::NullPtr)
631 template <
class Node>
inline
634 if (root == Node::NullPtr)
637 typedef std::pair<Node*, char> Postorder_Pair;
640 Postorder_Pair __pair;
641 Node *& p = __pair.first = root;
642 char & side = __pair.second =
'i';
648 __pair = stack.
pop();
652 if (
LLINK(p) != Node::NullPtr)
653 stack.
push(Postorder_Pair(
LLINK(p),
'i'));
655 stack.
push(Postorder_Pair(p,
'l'));
659 if (
RLINK(p) != Node::NullPtr)
660 stack.
push(Postorder_Pair(
RLINK(p),
'i'));
662 stack.
push(Postorder_Pair(p,
'r'));
666 (*visitFct) (p, stack.
size(), count++);
674 template <
class Node>
677 if (root == Node::NullPtr)
685 template <
class Node>
688 if (root == Node::NullPtr)
696 template <
class Node>
699 if (root == Node::NullPtr)
702 sufffix((Node*)
LLINK(root), acc);
703 siffix((Node*)
RLINK(root), acc);
713 template <
class Node>
727 template <
class Node>
731 infix(root, ret_val);
741 template <
class Node>
759 template <
class Node>
inline
762 if (node == Node::NullPtr)
780 if (node == Node::NullPtr)
786 return 1 + std::max(left_height, right_height);
797 template <
class Node>
inline
800 if (root == Node::NullPtr)
807 root = (Node*) Node::NullPtr;
820 template <
class Node>
inline
822 throw(std::exception, std::bad_alloc)
824 if (src_root == Node::NullPtr)
825 return (Node*) Node::NullPtr;
827 Node * tgt_root =
new Node(*src_root);
831 LLINK(tgt_root) = copyRec<Node>((Node*)
LLINK(src_root));
832 RLINK(tgt_root) = copyRec<Node>((Node*)
RLINK(src_root));
837 I(
RLINK(tgt_root) == Node::NullPtr);
839 if (
LLINK(tgt_root) != Node::NullPtr)
860 template <
class Node>
inline
861 bool areSimilar(Node * t1, Node * t2)
863 if (t1 == Node::NullPtr and t2 == Node::NullPtr)
866 if (t1 == Node::NullPtr or t2 == Node::NullPtr)
891 template <
class Node,
class Equal>
inline
892 bool areEquivalents(Node * t1, Node * t2)
894 if (t1 == Node::NullPtr and t2 == Node::NullPtr)
897 if (t1 == Node::NullPtr or t2 == Node::NullPtr)
900 if (not Equal () (
KEY(t1),
KEY(t2)))
903 return (areEquivalents<Node, Equal>(
LLINK(t1),
LLINK(t2)) and
904 areEquivalents<Node, Equal>(
RLINK(t1),
RLINK(t2)));
906 template <
class Node>
inline
907 bool areEquivalents(Node * t1, Node * t2)
909 return areEquivalents<Node, Aleph::equal_to<typename Node::key_type> >(t1, t2);
942 template <
class Node>
inline
944 void (*visitFct)(Node*,
int,
bool),
size_t queue_size)
947 if (root == Node::NullPtr)
950 const size_t two_power =
951 (size_t) (std::log((
float)queue_size)/std::log(2.0) + 1);
953 queue.
put(std::pair<Node*, bool>(root,
bool()));
955 for (
int pos = 0; not queue.is_empty(); pos++)
957 std::pair<Node*, bool> pr = queue.
get();
958 Node *& p = pr.first;
960 (*visitFct) (p, pos, pr.second);
962 if (
LLINK(p) != Node::NullPtr)
963 queue.
put(std::pair <Node*, bool> (
LLINK(p),
true));
965 if (
RLINK(p) != Node::NullPtr)
966 queue.
put(std::pair <Node*, bool> (
RLINK(p),
false));
991 template <
class Node,
class Operation>
inline
994 if (root == Node::NullPtr)
1000 while (not queue.is_empty())
1002 Node * p = queue.
get();
1004 if (not operation(p))
1007 if (
LLINK(p) != Node::NullPtr)
1010 if (
RLINK(p) != Node::NullPtr)
1016 template <
class Node,
class Operation>
inline
1017 bool level_traverse(Node * root, Operation && operation = Operation())
1019 return level_traverse<Node, Operation>(root, operation);
1044 template <
template <
class>
class Node,
typename Key>
inline
1046 const int & l_p,
const int & r_p,
1048 const int & l_i,
const int & r_i)
1053 return Node <Key> ::NullPtr;
1056 I(r_p - l_p == r_i - l_i);
1058 Node<Key> * root =
new Node <Key> (preorder[l_p]);
1065 for (
int j = l_i; j <= r_i; ++j)
1066 if (inorder[j] == preorder[l_p])
1074 LLINK(root) = build_tree <Node, Key> (preorder, l_p + 1, l_p + i,
1075 inorder, l_i, l_i + (i - 1));
1076 RLINK(root) = build_tree <Node, Key> (preorder, l_p + i + 1, r_p,
1077 inorder, l_i + i + 1, r_i);
1081 template <
class Node>
inline static void
1082 __compute_nodes_in_level(Node * root,
const int & level,
1083 const int & current_level,
1086 if (root == Node::NullPtr)
1088 if (current_level == level)
1093 __compute_nodes_in_level(
LLINK(root), level, current_level + 1, level_list);
1094 __compute_nodes_in_level(
RLINK(root), level, current_level + 1, level_list);
1110 template <
class Node>
inline
1114 __compute_nodes_in_level(root, level, 0, list);
1142 template <
class Node>
inline
1145 if (root == Node::NullPtr)
1148 Node *p = root, *r = Node::NullPtr, *q;
1149 while (p != Node::NullPtr)
1152 if (q == Node::NullPtr)
1161 while (q != r and
RLINK(q) != Node::NullPtr)
1173 RLINK (q) = Node::NullPtr;
1204 template <
class Node>
inline
1207 if (node == Node::NullPtr)
1210 Node * p = node, * r = Node::NullPtr, *q;
1211 while (p != Node::NullPtr)
1215 if (q == Node::NullPtr)
1224 while (q != r and
RLINK(q) != Node::NullPtr)
1235 RLINK(q) = Node::NullPtr;
1241 template <
class Node>
inline static
1242 size_t __internal_path_length(Node * p,
const size_t & level)
1244 if (p == Node::NullPtr)
1247 return level + __internal_path_length(
LLINK(p), level + 1) +
1248 __internal_path_length(
RLINK(p), level + 1);
1258 template <
class Node>
inline
1261 return __internal_path_length(p, 0);
1283 template <
class Node>
inline
1286 if (root == Node::NullPtr)
1313 template <
class Node>
inline
1321 template <
class Node>
static inline
1322 Node * __bits_to_tree(
BitArray & array,
int & i)
1326 return Node::NullPtr;
1328 Node * p =
new Node;
1329 LLINK(p) = (Node*) __bits_to_tree<Node>(array, i);
1330 RLINK(p) = (Node*) __bits_to_tree<Node>(array, i);
1358 return __bits_to_tree <Node> (array, idx);
1376 template <
class Node,
class Get_Key>
inline
1379 if (root == Node::NullPtr)
1382 output << Get_Key () (root) <<
" ";
1384 save_tree_keys_in_prefix<Node,Get_Key>((Node*)
LLINK(root), output);
1385 save_tree_keys_in_prefix<Node,Get_Key>((Node*)
RLINK(root), output);
1408 template <
class Node,
class Load_Key>
inline
1411 if (root == Node::NullPtr)
1414 Load_Key () (root, input);
1416 load_tree_keys_in_prefix<Node,Load_Key>((Node*)
LLINK(root), input);
1417 load_tree_keys_in_prefix<Node,Load_Key>((Node*)
RLINK(root), input);
1445 template <
class Node,
class Get_Key>
inline
1450 prefix.
save(output);
1451 save_tree_keys_in_prefix <Node, Get_Key> (root, output);
1469 template <
class Node,
class Load_Key>
inline
1474 Node * root = bits_to_tree <Node> (
prefix);
1475 load_tree_keys_in_prefix <Node, Load_Key> (root, input);
1480 template <
class Node,
class Get_Key>
inline
1481 void put_tree_keys_in_array(Node * root, ofstream & out)
1483 if (root == Node::NullPtr)
1486 const string str = Get_Key () (root);
1490 else if (str ==
"\n")
1493 out <<
"\"" << str <<
"\"";
1496 put_tree_keys_in_array <Node, Get_Key> ((Node *)
LLINK(root), out);
1497 put_tree_keys_in_array <Node, Get_Key> ((Node *)
RLINK(root), out);
1501 template <
class Node,
class Load_Key>
inline
1502 void load_tree_keys_from_array(Node * root,
const char * keys[],
int & idx)
1504 if (root == Node::NullPtr)
1507 if (Load_Key()(root, keys[idx]))
1510 load_tree_keys_from_array <Node, Load_Key> ((Node *)
LLINK(root), keys, idx);
1511 load_tree_keys_from_array <Node, Load_Key> ((Node *)
RLINK(root), keys, idx);
1558 template <
class Node,
class Get_Key>
inline
1560 const string & array_name,
1566 output <<
"const char * " << array_name <<
"_k[] = { " << endl;
1567 put_tree_keys_in_array <Node, Get_Key> (root, output);
1568 output <<
"NULL };" << endl;
1619 template <
class Node,
class Load_Key>
inline
1621 const size_t & num_bits,
1622 const char * keys[])
1626 Node * root = bits_to_tree <Node> (
prefix);
1628 load_tree_keys_from_array <Node, Load_Key> (root, keys, i);
1645 template <
class Node,
1649 typedef typename Node::key_type Key_Type;
1651 if (p == Node::NullPtr)
1654 if (
LLINK(p) != Node::NullPtr)
1656 if (less_or_equal_than<Key_Type, Compare> (
KEY(
LLINK(p)),
KEY(p)))
1657 return check_binary_search_tree<Node, Compare>(
LLINK(p));
1662 if (
RLINK(p) != Node::NullPtr)
1664 if (less_or_equal_than<Key_Type, Compare> (
KEY(p),
KEY(
RLINK(p))))
1665 return check_binary_search_tree<Node, Compare>(
RLINK(p));
1686 template <
class Node>
inline Node *
1688 const int & l,
const int & r)
1691 return Node::NullPtr;
1693 Node * root =
new Node(preorder[l]);
1698 int first_greater = l + 1;
1699 while ((first_greater <= r) and (preorder[first_greater] < preorder[l]))
1703 preorder_to_binary_search_tree<Node>(preorder, l + 1, first_greater - 1);
1705 preorder_to_binary_search_tree<Node>(preorder, first_greater, r);
1725 template <
class Node,
1729 const typename Node::key_type & key,
1732 while (root != Node::NullPtr)
1733 if (cmp(key,
KEY(root)))
1734 root = (Node*)
LLINK(root);
1735 else if(cmp (
KEY(root), key))
1736 root = (Node*)
RLINK(root);
1743 template <
class Node,
1746 const typename Node::key_type & key,
1747 Compare && cmp = Compare())
1749 return searchInBinTree <Node, Compare> (root, key, cmp);
1763 template <
class Node>
inline
1766 while (
LLINK(root) != Node::NullPtr)
1767 root =
static_cast<Node*
>(
LLINK(root));
1783 template <
class Node>
inline
1786 while (
RLINK(root) != Node::NullPtr)
1787 root =
static_cast<Node*
>(
RLINK(root));
1803 template <
class Node>
inline
1806 I(p != Node::NullPtr);
1807 I(
RLINK(p) != Node::NullPtr);
1810 p = (Node*)
RLINK(p);
1811 while (
LLINK(p) != Node::NullPtr)
1814 p = (Node*)
LLINK(p);
1832 template <
class Node>
inline
1835 I(p != Node::NullPtr);
1836 I(
LLINK(pp) != Node::NullPtr);
1839 p =
static_cast<Node*
>(
LLINK(p));
1841 while (
RLINK(p) != Node::NullPtr)
1844 p =
static_cast<Node*
>(
RLINK(p));
1871 template <
class Node,
1874 Node *& parent, Compare && cmp = Compare())
1876 I((
LLINK(parent) == root) or (
RLINK(parent) == root));
1877 I(root != Node::NullPtr);
1880 if (cmp(key,
KEY(root)))
1882 if (
LLINK(root) == Node::NullPtr)
1886 root =
static_cast<Node*
>(
LLINK(root));
1888 else if (cmp(
KEY(root), key))
1890 if (
RLINK(root) == Node::NullPtr)
1894 root =
static_cast<Node*
>(
RLINK(root));
1918 template <
class Node,
1922 const typename Node::key_type & key,
1923 Compare && cmp = Compare())
1925 I(root != Node::NullPtr);
1928 if (cmp(key,
KEY(root)))
1930 if (
LLINK(root) == Node::NullPtr)
1933 root =
static_cast<Node*
>(
LLINK(root));
1935 else if (cmp(
KEY(root), key))
1937 if (
RLINK(root) == Node::NullPtr)
1940 root =
static_cast<Node*
>(
RLINK(root));
1961 template <
class Node,
1965 if (root == Node::NullPtr)
1968 if (Compare () (
KEY(p),
KEY(root)))
1969 return insert_in_binary_search_tree<Node,Compare>((Node*&)
LLINK(root), p);
1970 else if (Compare () (
KEY(root),
KEY(p)))
1971 return insert_in_binary_search_tree<Node,Compare>((Node*&)
RLINK(root), p);
1973 return Node::NullPtr;
1976 # define INSERT_DUP insert_dup_in_binary_search_tree<Node,Compare>
1995 template <
class Node,
1999 if (root == Node::NullPtr)
2002 if (Compare () (
KEY(p),
KEY(root)))
2003 return INSERT_DUP ((Node*&)
LLINK(root), p);
2005 return INSERT_DUP((Node*&)
RLINK(root), p);
2027 template <
class Node,
2031 if (r == Node::NullPtr)
2034 if (Compare () (
KEY(p),
KEY(r)))
2036 search_or_insert_in_binary_search_tree<Node,Compare>((Node*&)
LLINK(r), p);
2037 else if (Compare () (
KEY(r),
KEY(p)))
2039 search_or_insert_in_binary_search_tree<Node,Compare>((Node*&)
RLINK(r), p);
2044 # define SPLIT split_key_rec<Node, Compare>
2065 template <
class Node,
2068 Node *& ts, Node *& tg)
2070 if (root == Node::NullPtr)
2072 ts = tg = Node::NullPtr;
2076 if ( Compare() (key,
KEY(root)) )
2077 if (SPLIT((Node*&)
LLINK(root), key, ts, (Node*&)
LLINK(root)))
2085 if (Compare() (
KEY(root), key))
2086 if (SPLIT((Node*&)
RLINK(root), key, (Node*&)
RLINK(root), tg))
2099 # define SPLIT split_key_dup_rec<Node, Compare>
2119 template <
class Node,
2122 Node *& ts, Node *& tg)
2124 if (root == Node::NullPtr)
2126 ts = tg = Node::NullPtr;
2130 if (Compare() (key,
KEY(root)))
2131 SPLIT((Node*&)
LLINK(root), key, ts, (Node*&)
LLINK(root));
2132 else if (Compare () (
KEY(root), key))
2133 SPLIT((Node*&)
RLINK(root), key, (Node*&)
RLINK(root), tg);
2135 SPLIT((Node*&)
LLINK(root), key, ts, (Node*&)
LLINK(root));
2157 if (ts == Node::NullPtr)
2160 if (tg == Node::NullPtr)
2166 Node * ret_val = ts;
2167 ts = tg = Node::NullPtr;
2172 # define REMOVE remove_from_search_binary_tree<Node, Compare>
2191 template <
class Node,
2194 const typename Node::key_type & key)
2196 if (root == Node::NullPtr)
2197 return Node::NullPtr;
2199 if (Compare () (key,
KEY(root)))
2200 return REMOVE((Node*&)
LLINK(root), key);
2201 else if (Compare () (
KEY(root), key))
2202 return REMOVE((Node*&)
RLINK(root), key);
2204 Node * ret_val = root;
2234 template <
class Node,
2238 Node * l = Node::NullPtr, * r = Node::NullPtr;
2240 if (not split_key_rec<Node, Compare>(root,
KEY(p), l, r))
2241 return Node::NullPtr;
2272 template <
class Node,
2276 split_key_dup_rec<Node, Compare>(root,
KEY(p),
LLINK(p),
RLINK(p));
2298 template <
class Node,
2302 if (t2 == Node::NullPtr)
2305 Node * l = (Node*)
LLINK(t2);
2306 Node * r = (Node*)
RLINK(t2);
2308 if (insert_in_binary_search_tree <Node, Compare> (t1, t2) == Node::NullPtr)
2310 insert_in_binary_search_tree<Node, Compare>(dup, t2);
2335 template <
class Node,
2337 Node *
join(Node * t1, Node * t2, Node *& dup)
2339 if (t1 == Node::NullPtr)
2342 if (t2 == Node::NullPtr)
2345 Node * l = (Node*)
LLINK(t1);
2346 Node * r = (Node*)
RLINK(t1);
2351 while (t1 != Node::NullPtr and
2352 insert_root<Node, Compare>(t2, t1) == Node::NullPtr)
2356 l = (Node*)
LLINK(t1);
2357 r = (Node*)
RLINK(t1);
2359 I(p != Node::NullPtr);
2361 insert_in_binary_search_tree<Node, Compare>(dup, p);
2364 LLINK(t2) = join<Node, Compare>(l, (Node*)
LLINK(t2), dup);
2365 RLINK(t2) = join<Node, Compare>(r, (Node*)
RLINK(t2), dup);
2374 template <
class Node>
inline
2377 I(p != Node::NullPtr);
2379 Node * q =
static_cast<Node*
>(
LLINK(p));
2398 template <
class Node>
inline
2401 I(p != Node::NullPtr);
2402 I(pp != Node::NullPtr);
2403 I( ((Node*)
LLINK(pp)) == p or ((Node*)
RLINK(pp)) == p);
2405 Node *q =
static_cast<Node*
>(
LLINK(p));
2409 if (static_cast<Node*>(
LLINK(pp)) == p)
2421 template <
class Node>
inline
2424 I(p != Node::NullPtr);
2426 Node *q =
static_cast<Node*
>(
RLINK(p));
2447 template <
class Node>
inline
2450 I(p != Node::NullPtr);
2451 I(pp != Node::NullPtr);
2452 I(static_cast<Node*>(
LLINK(pp)) == p or static_cast<Node*>(
RLINK(pp)) == p);
2454 Node *q =
static_cast<Node*
>(
RLINK(p));
2492 template <
class Node,
class Key,
2494 void split_key(Node * root,
const Key & key, Node *& l, Node *& r)
2496 if (root == Node::NullPtr)
2498 l = r = (Node*) Node::NullPtr;
2503 Node ** current_parent = NULL;
2504 Node ** pending_child = NULL;
2505 char current_is_right;
2508 if (cmp (key,
KEY(root)))
2512 current_is_right =
true;
2518 current_is_right =
false;
2523 while (current != Node::NullPtr)
2525 if (cmp (key,
KEY(current)))
2527 if (not current_is_right)
2529 current_is_right = not current_is_right;
2530 *pending_child = *current_parent;
2531 pending_child = current_parent;
2534 current_parent =
static_cast<Node**
>(&
LLINK(current));
2538 if (current_is_right)
2540 current_is_right = not current_is_right;
2541 *pending_child = *current_parent;
2542 pending_child = current_parent;
2544 current_parent =
static_cast<Node**
>(&
RLINK(current));
2547 current = *current_parent;
2550 *pending_child =
static_cast<Node*
>(Node::NullPtr);
2570 template <
class Node>
inline
2576 I(p != Node::NullPtr and q != Node::NullPtr and
2577 pp != Node::NullPtr and pq != Node::NullPtr);
2580 I(
LLINK(q) == Node::NullPtr);
2589 LLINK(p) = Node::NullPtr;
2605 Node *qr =
RLINK(q);
2630 template <
class Node>
inline
2636 I((p != Node::NullPtr) and (q != Node::NullPtr) and
2637 (pp != Node::NullPtr) and (pq != Node::NullPtr));
2640 I(
RLINK(q) == Node::NullPtr);
2649 RLINK(p) = Node::NullPtr;
2665 Node *ql =
LLINK(q);
2690 template <
class Node,
class Key,
2694 if (root == Node::NullPtr)
2697 if (Compare () (
KEY(p),
KEY(root)))
2700 insert_root_rec<Node, Key, Compare>(
static_cast<Node*
>(
LLINK(root)), p);
2701 if (left_branch == Node::NullPtr)
2702 return (Node*) Node::NullPtr;
2704 LLINK(root) = left_branch;
2707 else if (Compare () (
KEY(root),
KEY(p) ))
2709 Node *right_branch =
2710 insert_root_rec<Node, Key, Compare>(
static_cast<Node*
>(
RLINK(root)), p);
2711 if (right_branch == Node::NullPtr)
2712 return (Node*) Node::NullPtr;
2714 RLINK(root) = right_branch;
2718 return (Node*) Node::NullPtr;
2741 template <
class Node,
class Key,
2745 if (root == Node::NullPtr)
2748 if (Compare () (
KEY(p),
KEY(root)))
2750 Node * left_branch =
2751 search_or_insert_root_rec<Node, Key, Compare>((Node*)
LLINK(root), p);
2752 if (left_branch == p)
2754 LLINK(root) = left_branch;
2761 else if (Compare () (
KEY(root),
KEY(p)))
2763 Node * right_branch =
2764 search_or_insert_root_rec<Node, Key, Compare>((Node*)
RLINK(root), p);
2765 if (right_branch == p)
2767 RLINK(root) = right_branch;
2772 return right_branch;
2781 # endif // TPL_BINNODEUTILS_H
Node * join_preorder(Node *t1, Node *t2, Node *&dup)
Definition: tpl_binNodeUtils.H:2300
Node * search_or_insert_root_rec(Node *root, Node *p)
Definition: tpl_binNodeUtils.H:2743
Itor::difference_type count(const Itor &beg, const Itor &end, const T &value)
Definition: ahAlgo.H:127
#define LLINK(p)
Definition: tpl_binNode.H:196
DynList< Node * > infix(Node *root)
Definition: tpl_binNodeUtils.H:728
void save(ofstream &output)
Definition: bitArray.H:378
#define RLINK(p)
Definition: tpl_binNode.H:201
Definition: tpl_binNodeUtils.H:407
size_t postOrderStack(Node *root, void(*visitFct)(Node *, int, int))
Definition: tpl_binNodeUtils.H:632
void preOrderThreaded(Node *node, void(*visitFct)(Node *))
Definition: tpl_binNodeUtils.H:1205
DynList< Node * > suffix(Node *root)
Definition: tpl_binNodeUtils.H:742
size_t preOrderStack(Node *node, void(*visitFct)(Node *, int, int))
Definition: tpl_binNodeUtils.H:512
Definition: ahFunction.H:145
Definition: bitArray.H:96
Definition: tpl_dynDlist.H:26
Definition: tpl_arrayStack.H:42
const size_t & size() const
Retorna el número de elementos que contiene la pila.
Definition: tpl_arrayStack.H:184
void operator()(Node *root, Op &op)
operación sobre cada nodo
Definition: tpl_binNodeUtils.H:281
Node * search_rank_parent(Node *root, const typename Node::key_type &key, Compare &&cmp=Compare())
Definition: tpl_binNodeUtils.H:1921
void split_key(Node *root, const Key &key, Node *&l, Node *&r)
Definition: tpl_binNodeUtils.H:2494
void load(ifstream &input)
Definition: bitArray.H:406
Definition: tpl_arrayQueue.H:38
Node * insert_root_rec(Node *root, Node *p)
Definition: tpl_binNodeUtils.H:2692
void swap_node_with_successor(Node *p, Node *&pp, Node *q, Node *&pq)
Definition: tpl_binNodeUtils.H:2571
void swap_node_with_predecessor(Node *p, Node *&pp, Node *q, Node *&pq)
Definition: tpl_binNodeUtils.H:2631
void load_tree_keys_in_prefix(Node *root, ifstream &input)
Definition: tpl_binNodeUtils.H:1409
Node * insert_root(Node *&root, Node *p)
Definition: tpl_binNodeUtils.H:2236
Node * insert_dup_in_binary_search_tree(Node *&root, Node *p)
Definition: tpl_binNodeUtils.H:1997
Node * find_max(Node *root)
Definition: tpl_binNodeUtils.H:1784
Definition: tpl_dynListQueue.H:22
T & put(const T &item)
Definition: tpl_arrayQueue.H:138
void operator()(Node *root, Op &op)
operación sobre cada nodo
Definition: tpl_binNodeUtils.H:421
void save_in_array_of_chars(const string &name, ofstream &output)
Definition: bitArray.H:442
Node * bits_to_tree(BitArray &array, int idx=0)
Definition: tpl_binNodeUtils.H:1356
Node * rotate_to_right(Node *p)
Definition: tpl_binNodeUtils.H:2375
void operator()(Node *root, Op &op)
operación sobre cada nodo
Definition: tpl_binNodeUtils.H:187
T & push(const T &data)
Definition: tpl_arrayStack.H:84
size_t internal_path_length(Node *p)
Definition: tpl_binNodeUtils.H:1259
Node * find_min(Node *root)
Definition: tpl_binNodeUtils.H:1764
Node * searchInBinTree(Node *root, const typename Node::key_type &key, Compare &cmp)
Definition: tpl_binNodeUtils.H:1728
Node * join_exclusive(Node *&ts, Node *&tg)
Definition: tpl_binNodeUtils.H:2155
void save_tree_in_array_of_chars(Node *root, const string &array_name, ofstream &output)
Definition: tpl_binNodeUtils.H:1559
int preOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Definition: tpl_binNodeUtils.H:93
Definition: tpl_binNodeUtils.H:325
Node * rotate_to_left(Node *p, Node *pp)
Definition: tpl_binNodeUtils.H:2448
Node * preorder_to_binary_search_tree(DynArray< typename Node::key_type > &preorder, const int &l, const int &r)
Definition: tpl_binNodeUtils.H:1687
Definition: tpl_binNodeUtils.H:260
Node * join(Node *t1, Node *t2, Node *&dup)
Definition: tpl_binNodeUtils.H:2337
void split_key_dup_rec(Node *root, const typename Node::key_type &key, Node *&ts, Node *&tg)
Definition: tpl_binNodeUtils.H:2121
size_t computeHeightRec(Node *node)
Definition: tpl_binNodeUtils.H:778
Node * load_tree(ifstream &input)
Definition: tpl_binNodeUtils.H:1470
Node * find_predecessor(Node *p, Node *&pp)
Definition: tpl_binNodeUtils.H:1833
void push(const unsigned int value)
Inserta al final del arreglo el valor value.
Definition: bitArray.H:281
Node * insert_in_binary_search_tree(Node *&root, Node *p)
Definition: tpl_binNodeUtils.H:1963
Node * insert_dup_root(Node *&root, Node *p)
Definition: tpl_binNodeUtils.H:2274
bool split_key_rec(Node *root, const typename Node::key_type &key, Node *&ts, Node *&tg)
Definition: tpl_binNodeUtils.H:2067
int read_bit(const size_t i) const
Definition: bitArray.H:225
size_t compute_cardinality_rec(Node *node)
Definition: tpl_binNodeUtils.H:760
void load_from_array_of_chars(const unsigned char str[], const size_t num_bits)
Definition: bitArray.H:479
void save_tree_keys_in_prefix(Node *root, ofstream &output)
Definition: tpl_binNodeUtils.H:1377
void destroyRec(Node *&root)
Definition: tpl_binNodeUtils.H:798
T get()
Definition: tpl_arrayQueue.H:199
int postOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Definition: tpl_binNodeUtils.H:134
bool level_traverse(Node *root, Operation &operation)
Definition: tpl_binNodeUtils.H:992
Node * search_parent(Node *root, const typename Node::key_type &key, Node *&parent, Compare &&cmp=Compare())
Definition: tpl_binNodeUtils.H:1873
size_t simple_preOrderStack(Node *node, void(*visitFct)(Node *, int, int))
Definition: tpl_binNodeUtils.H:460
void levelOrder(Node *root, void(*visitFct)(Node *, int, bool), size_t queue_size)
Definition: tpl_binNodeUtils.H:943
size_t inOrderStack(Node *node, void(*visitFct)(Node *, int, int))
Definition: tpl_binNodeUtils.H:572
Definition: tpl_binNodeUtils.H:172
BitArray tree_to_bits(Node *root)
Definition: tpl_binNodeUtils.H:1314
Node * copyRec(Node *src_root)
Definition: tpl_binNodeUtils.H:821
Node * remove_from_search_binary_tree(Node *&root, const typename Node::key_type &key)
Definition: tpl_binNodeUtils.H:2193
T pop()
Definition: tpl_arrayStack.H:119
Node * find_successor(Node *p, Node *&pp)
Definition: tpl_binNodeUtils.H:1804
Node * load_tree_from_array(const unsigned char bits[], const size_t &num_bits, const char *keys[])
Definition: tpl_binNodeUtils.H:1620
bool check_binary_search_tree(Node *p)
Definition: tpl_binNodeUtils.H:1647
void compute_nodes_in_level(Node *root, const int &level, DynDlist< Node * > &list)
Definition: tpl_binNodeUtils.H:1111
T & append(const T &item)
Inserta un item como último elemento.
Definition: htlist.H:685
#define KEY(p)
Definition: tpl_binNode.H:206
void operator()(Node *root, Op &op)
operación sobre cada nodo
Definition: tpl_binNodeUtils.H:340
T get()
Definition: tpl_dynListQueue.H:107
void save_tree(Node *root, ofstream &output)
Definition: tpl_binNodeUtils.H:1446
DynList< Node * > prefix(Node *root)
Definition: tpl_binNodeUtils.H:714
Node * search_or_insert_in_binary_search_tree(Node *&r, Node *p)
Definition: tpl_binNodeUtils.H:2029
bool is_empty() const
Retorna true si la pila está vacía.
Definition: tpl_arrayStack.H:181
Node< Key > * build_tree(DynArray< Key > &preorder, const int &l_p, const int &r_p, DynArray< Key > &inorder, const int &l_i, const int &r_i)
Definition: tpl_binNodeUtils.H:1045
T & put(const T &data)
Definition: tpl_dynListQueue.H:86
T & append(const T &item)
Definition: tpl_dynDlist.H:106
int inOrderRec(Node *root, void(*visitFct)(Node *, int, int))
Definition: tpl_binNodeUtils.H:51
void inOrderThreaded(Node *root, void(*visitFct)(Node *))
Definition: tpl_binNodeUtils.H:1143