7 # include <ahFunction.H>
9 # include <tpl_dynListQueue.H>
23 Control_Fields control_fields;
29 control_fields.is_leaf =
true;
30 control_fields.is_left =
true;
35 Control_Fields & get_control_fields() {
return control_fields; }
39 control_fields.is_leaf =
true;
40 control_fields.is_left =
true;
46 # define PREV(p) (p->getL())
47 # define NEXT(p) (p->getR())
48 # define ULINK(p) reinterpret_cast<Node*&>((p)->getU())
49 # define IS_LEAF(p) ((p)->get_control_fields().is_leaf)
50 # define IS_LEFT(p) ((p)->get_control_fields().is_left)
51 # define CTRL_BITS(p) ((p)->get_control_fields())
75 template <
template <
class>
class NodeType,
typename Key,
83 typedef NodeType<Key> Node;
85 Compare & key_comp() {
return cmp; }
87 Compare & get_compare() {
return cmp; }
101 std::swap(root, h.root);
102 std::swap(last, h.last);
103 std::swap(num_nodes, h.num_nodes);
104 std::swap(cmp, h.cmp);
109 static bool is_in_list(Node * p)
117 static bool has_sibling(Node * p)
119 return ULINK(p) !=
RLINK(p);
122 void swap_with_parent(Node * p)
129 const bool p_has_sibling = has_sibling(p);
130 const bool p_is_in_list = is_in_list(p);
131 const bool pp_is_in_list = is_in_list(pp);
132 const bool p_has_child = not IS_LEAF(p);
134 std::swap(CTRL_BITS(pp), CTRL_BITS(p));
139 Node *ppp = ULINK(pp);
144 if (
LLINK(ppp) == pp)
163 Node *lcp =
LLINK(p);
164 Node *rcp =
RLINK(p);
184 if (not p_is_in_list)
186 ULINK(lcp) = ULINK(rcp) = pp;
207 if (not pp_is_in_list)
210 ULINK(
LLINK(p)) = pp;
241 virtual void sift_up(Node * p)
243 while (p != root and cmp (
KEY(p),
KEY(ULINK(p))))
247 virtual void sift_down(Node * p)
249 while (not IS_LEAF(p))
251 Node * cp =
LLINK(p);
256 if (cmp (
KEY(p),
KEY(cp)))
259 swap_with_parent(cp);
263 void swap_root_with_last()
266 I(ULINK(root) == head);
267 I(not IS_LEAF(root));
272 Node * lRoot =
LLINK(root);
273 Node * rRoot =
RLINK(root);
274 Node * f_last = ULINK(last);
275 Node * prev_last =
LLINK(last);
276 Node * next_last =
RLINK(last);
278 if (
LLINK(f_last) == last)
279 LLINK(f_last) = root;
281 RLINK(f_last) = root;
283 if (
RLINK(root) != last)
284 std::swap(ULINK(root), ULINK(last));
294 ULINK(lRoot) = ULINK(rRoot) = last;
299 PREV(root) = prev_last;
300 NEXT(root) = next_last;
302 NEXT(prev_last) = PREV(next_last) = root;
304 else if (num_nodes == 3)
306 I(
RLINK(root) == last);
309 ULINK(last) = ULINK(root);
312 Node *s_last =
LLINK(last);
313 ULINK(s_last) = last;
315 LLINK(last) = s_last;
323 I(
LLINK(root) == last);
325 ULINK(last) = ULINK(root);
331 std::swap(CTRL_BITS(root), CTRL_BITS(last));
332 std::swap(root, last);
337 I(last != root and num_nodes > 0);
340 Node * ret_val = last;
341 Node * pp = ULINK(last);
342 Node * new_last =
LLINK(last);
347 LLINK(pp) = new_last;
363 void replace_node(Node * node, Node * new_node)
369 Node * parent = ULINK(node);
370 Node * left_child =
LLINK(node);
371 Node * right_child =
RLINK(node);
374 ULINK(new_node) = parent;
375 LLINK(new_node) = left_child;
376 RLINK(new_node) = right_child;
381 I(
LLINK(parent) == node);
382 LLINK(parent) = new_node;
386 I(
RLINK(parent) == node);
387 RLINK(parent) = new_node;
393 RLINK(left_child) = new_node;
394 LLINK(right_child) = new_node;
398 ULINK(left_child) = new_node;
400 if (ULINK(right_child) == node)
401 ULINK(right_child) = new_node;
404 I(left_child == last);
405 RLINK(left_child) = new_node;
406 LLINK(right_child) = new_node;
410 CTRL_BITS(new_node) = CTRL_BITS(node);
413 static void __postorder_delete(Node * p, Node * incomplete_node)
421 __postorder_delete(
LLINK(p), incomplete_node);
423 if (p != incomplete_node)
424 __postorder_delete(
RLINK(p), incomplete_node);
431 Node * getRoot() {
return root; }
433 Node * getRoot()
const {
return const_cast<Node*
>(root); }
436 void for_each_in_preorder(Node * p, Op & op)
443 for_each_in_preorder(advance_left(p), op);
444 for_each_in_preorder(advance_right(p), op);
448 void for_each_in_preorder(Node * p, Op && op())
450 return for_each_in_preorder<Op>(p, op);
456 bool __level_traverse(Node * root, Op & operation)
464 while (not queue.is_empty())
466 Node * p = queue.
get();
468 if (not operation(p))
471 Node * c = advance_left(p);
477 c = advance_right(p);
488 bool level_traverse(Node * root, Op & operation)
490 return __level_traverse(root, operation);
494 bool level_traverse(Node * root, Op && operation = Op())
496 return __level_traverse(root, operation);
500 bool level_traverse(Node * root, Op & operation)
const
502 return const_cast<GenBinHeap&
>(*this).__level_traverse(root, operation);
506 bool level_traverse(Node * root, Op && operation = Op())
const
508 return const_cast<GenBinHeap&
>(*this).__level_traverse(root, operation);
512 : cmp(__cmp), head(&head_node), root(
RLINK(head)), last(&head_node),
552 Node * pp =
RLINK(last);
589 Node *
getMin() throw(std::exception, std::underflow_error)
592 throw std::underflow_error (
"Heap is empty");
594 Node *ret_val = root;
605 swap_root_with_last();
614 Node *
getMax() throw(std::exception, std::underflow_error)
644 Node *
remove(Node * node)
throw(std::exception, std::underflow_error)
647 throw std::underflow_error (
"Heap is empty");
653 return remove_last();
655 Node * p = remove_last();
664 replace_node(node, p);
686 __postorder_delete(root, ULINK(last));
688 __postorder_delete(root, NULL);
697 Node *
top()
const throw(std::exception, std::underflow_error)
700 throw std::underflow_error (
"Heap is empty");
706 const size_t &
size()
const {
return num_nodes; }
713 Node * advance_left(Node * p)
721 Node * advance_right(Node * p)
725 if (not has_sibling(
LLINK(p)))
731 virtual bool verify_heap(Node * p)
733 Node * left_link = advance_left(p);
735 if (left_link == NULL)
742 if (cmp (
KEY(left_link),
KEY(p)))
745 Node * right_link = advance_right(p);
747 if (right_link == NULL)
748 return verify_heap(left_link);
750 if (cmp (
KEY(right_link),
KEY(p)))
753 return verify_heap(right_link);
763 return verify_heap(root);
783 template <
class Key,
typename Compare = Aleph::less<Key> >
809 template <
class Key,
typename Compare = Aleph::less<Key> >
813 typedef BinHeapNodeVtl<Key>
Node;
825 # endif // TPL_BINHEAP_H
#define LLINK(p)
Definition: tpl_binNode.H:196
Node * insert(Node *p)
Definition: tpl_binHeap.H:533
#define RLINK(p)
Definition: tpl_binNode.H:201
Definition: ahFunction.H:145
Definition: tpl_binHeap.H:13
Definition: tpl_dynListQueue.H:22
Node * getMax()
Definition: tpl_binHeap.H:614
void update(Node *p)
Definition: tpl_binHeap.H:629
Definition: tpl_binHeap.H:77
Node * top() const
Definition: tpl_binHeap.H:697
Node * getMin()
Definition: tpl_binHeap.H:589
const size_t & size() const
Retorna la cardinalidad del heap.
Definition: tpl_binHeap.H:706
Definition: tpl_binHeap.H:784
Definition: tpl_binHeap.H:810
#define DECLARE_BINNODE(Name, height, Control_Data)
Definition: tpl_binNode.H:126
BinHeapNode< Key > Node
El tipo de nodo del heap.
Definition: tpl_binHeap.H:787
BinHeapNodeVtl< Key > Node
El tipo de nodo del heap.
Definition: tpl_binHeap.H:813
#define KEY(p)
Definition: tpl_binNode.H:206
T get()
Definition: tpl_dynListQueue.H:107
bool is_empty() const
Retorna true si el heap está vacío.
Definition: tpl_binHeap.H:709
void remove_all_and_delete()
Definition: tpl_binHeap.H:673
T & put(const T &data)
Definition: tpl_dynListQueue.H:86