aGrUM  0.16.0
gum::SplayTree< Element > Class Template Reference

A splay tree. More...

#include <agrum/core/splay.h>

+ Collaboration diagram for gum::SplayTree< Element >:

Public Member Functions

Constructors / Destructors
 SplayTree ()
 Basic constructor, make an empty splay tree. More...
 
 SplayTree (const Element &e)
 Basic constructor, make a splay tree with one element. More...
 
 SplayTree (const SplayTree &from)
 Copy constructor. More...
 
 ~SplayTree ()
 Class destructor. More...
 
Operators
SplayTree< Element > & operator= (const SplayTree< Element > &from)
 Assignment operator. More...
 
Methods
Element & operator[] (const unsigned int i)
 Get the element at the position n. More...
 
const Element & operator[] (const unsigned int i) const
 Get the element at the position n. More...
 
Element & front ()
 Get the first element. More...
 
Element & back ()
 Get the last element. More...
 
void popFront ()
 Remove the first element. More...
 
void popBack ()
 Remove the last element. More...
 
void pushFront (const Element &e)
 Add an element in the first position. More...
 
void pushBack (const Element &e)
 Add an element in the last position. More...
 
void insert (const Element &e)
 Add an element to the tree. More...
 
void join (const SplayTree< Element > &s)
 Concatenation of two trees. More...
 
SplayTree< Element > split (const int i)
 Divide the tree at the position. More...
 
SplayTree< Element > split_by_val (const Element &e)
 Divide the tree at the position. More...
 
Size size () const
 The number of elements in the tree. More...
 
bool contains (const Element &e) const
 Test if the tree contains the element. More...
 

Protected Attributes

Data Menbers
SplayBinaryNode< Element > * root
 Root of the tree. More...
 
HashTable< Element, SplayBinaryNode< Element > *> addr
 The hash table to find quickly the position of a node. More...
 

Protected Member Functions

void _copy (const SplayTree< Element > &)
 a function used to perform copies More...
 

Friends

std::ostream & operator<< (std::ostream &out, const SplayTree< Element > &)
 Friendly to display. More...
 

Detailed Description

template<class Element>
class gum::SplayTree< Element >

A splay tree.

Warning
an Element must be in just one Splay Tree, the behavior is unspecified else.
Usage example:
// Creating empty trees
SplayTree<string> a;
// Make a copy
SplayTree<string> b(a);
// Add an element
a.insert("toto");
a.insert("titi");
// Get the first element
a.front();
// And the last
a.back();
// concatenate two trees
a.join(b);
// divide a tree at the third position, the first part is placed into a
// the second into b.
b = a.split(3);
// Display the splay tree
a.printTree();
// Get the size
a.size();
Template Parameters
ElementThe elements type.

Definition at line 44 of file splay.h.

Constructor & Destructor Documentation

◆ SplayTree() [1/3]

template<class Element >
INLINE gum::SplayTree< Element >::SplayTree ( )

Basic constructor, make an empty splay tree.

Definition at line 366 of file splay_tpl.h.

366  : root(0), addr() {
367  // for debugging purposes
368  GUM_CONSTRUCTOR(SplayTree);
369  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:366
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421

◆ SplayTree() [2/3]

template<class Element >
INLINE gum::SplayTree< Element >::SplayTree ( const Element &  e)

Basic constructor, make a splay tree with one element.

Parameters
eThe element of the tree.

Definition at line 377 of file splay_tpl.h.

References gum::SplayTree< Element >::addr, and gum::SplayTree< Element >::root.

377  : root(0), addr() {
378  root = new SplayBinaryNode< Element >(e, addr);
379  // for debugging purposes
380  GUM_CONSTRUCTOR(SplayTree);
381  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:366
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421

◆ SplayTree() [3/3]

template<class Element >
INLINE gum::SplayTree< Element >::SplayTree ( const SplayTree< Element > &  from)

Copy constructor.

Parameters
fromThe gum::SplayTree to copy.

Definition at line 386 of file splay_tpl.h.

References gum::SplayTree< Element >::_copy(), and gum::SplayTree< Element >::operator=().

386  : addr() {
387  _copy(from);
388  // for debugging purposes
389  GUM_CONS_CPY(SplayTree);
390  }
void _copy(const SplayTree< Element > &)
a function used to perform copies
Definition: splay_tpl.h:355
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:366
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421
+ Here is the call graph for this function:

◆ ~SplayTree()

template<class Element >
INLINE gum::SplayTree< Element >::~SplayTree ( )

Class destructor.

Definition at line 416 of file splay_tpl.h.

References gum::SplayTree< Element >::root.

416  {
417  // for debugging purposes
418  GUM_DESTRUCTOR(SplayTree);
419 
420  if (root) delete (root);
421  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:366

Member Function Documentation

◆ _copy()

template<class Element >
INLINE void gum::SplayTree< Element >::_copy ( const SplayTree< Element > &  from)
protected

a function used to perform copies

Definition at line 355 of file splay_tpl.h.

References gum::SplayTree< Element >::root.

Referenced by gum::SplayTree< Element >::operator=(), and gum::SplayTree< Element >::SplayTree().

355  {
356  if (from.root) {
357  root = new SplayBinaryNode< Element >(*from.root, addr);
358  } else {
359  root = 0;
360  }
361  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421
+ Here is the caller graph for this function:

◆ back()

template<class Element >
INLINE Element & gum::SplayTree< Element >::back ( )

Get the last element.

Exceptions
NotFoundRaised if the splay tree is empty.

Definition at line 519 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::fd, GUM_ERROR, gum::SplayTree< Element >::root, and gum::SplayBinaryNode< Element >::splay().

519  {
520  SplayBinaryNode< Element >* act = root;
521 
522  if (!root) { GUM_ERROR(NotFound, "The splay tree is empty"); }
523 
524  if (act->fd)
525  for (; act->fd; act = act->fd)
526  ;
527 
528  root = act->splay();
529 
530  return root->elt;
531  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ contains()

template<class Element >
INLINE bool gum::SplayTree< Element >::contains ( const Element &  e) const

Test if the tree contains the element.

Parameters
eThe element to test if it is in the splay tree.
Returns
Returns true if e is in this splay tree.

Definition at line 788 of file splay_tpl.h.

References gum::SplayTree< Element >::addr.

788  {
789  return addr.exists(e);
790  }
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421

◆ front()

template<class Element >
INLINE Element & gum::SplayTree< Element >::front ( )

Get the first element.

Exceptions
NotFoundRaised if the splay tree is empty.

Definition at line 502 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::fg, GUM_ERROR, gum::SplayTree< Element >::root, and gum::SplayBinaryNode< Element >::splay().

502  {
503  SplayBinaryNode< Element >* act = root;
504 
505  if (!root) { GUM_ERROR(NotFound, "The splay tree is empty"); }
506 
507  if (act->fg)
508  for (; act->fg; act = act->fg)
509  ;
510 
511  root = act->splay();
512 
513  return root->elt;
514  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ insert()

template<class Element >
INLINE void gum::SplayTree< Element >::insert ( const Element &  e)

Add an element to the tree.

Parameters
eThe element to add.

Definition at line 620 of file splay_tpl.h.

References gum::SplayTree< Element >::addr, gum::SplayBinaryNode< Element >::fd, gum::SplayTree< Element >::root, and gum::SplayBinaryNode< Element >::size.

620  {
621  SplayBinaryNode< Element >* act = root;
622 
623  if (!root) {
624  root = new SplayBinaryNode< Element >(e, addr);
625  } else {
626  while (act->fd) {
627  ++act->size;
628  act = act->fd;
629  }
630 
631  // act->fd == nullptr
632  act->fd = new SplayBinaryNode< Element >(e, addr, 0, 0, act);
633 
634  ++act->size;
635 
636  root = act->fd->splay();
637  }
638  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421

◆ join()

template<class Element >
INLINE void gum::SplayTree< Element >::join ( const SplayTree< Element > &  s)

Concatenation of two trees.

Parameters
sThe tree to add.

Definition at line 646 of file splay_tpl.h.

References gum::SplayTree< Element >::addr, and gum::SplayTree< Element >::root.

646  {
647  if (s.root) {
648  if (root) {
649  root = root->join(s.root, addr);
650  } else {
651  root = new SplayBinaryNode< Element >(*s.root, addr);
652  }
653  }
654  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421

◆ operator=()

template<class Element >
INLINE SplayTree< Element > & gum::SplayTree< Element >::operator= ( const SplayTree< Element > &  from)

Assignment operator.

Parameters
fromThe gum::SplayTree to copy.
Returns
This gum::SplayTree.

Definition at line 396 of file splay_tpl.h.

References gum::SplayTree< Element >::_copy(), gum::SplayTree< Element >::addr, and gum::SplayTree< Element >::root.

Referenced by gum::SplayTree< Element >::SplayTree().

396  {
397  // avoid self assignment
398  if (this != &from) {
399  // for debugging purposes
400  GUM_OP_CPY(SplayTree);
401  // delete the old contents
402 
403  if (root) delete root;
404 
405  addr.clear();
406 
407  _copy(from);
408  }
409 
410  return *this;
411  }
void _copy(const SplayTree< Element > &)
a function used to perform copies
Definition: splay_tpl.h:355
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:366
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ operator[]() [1/2]

template<class Element >
Element & gum::SplayTree< Element >::operator[] ( const unsigned int  i)

Get the element at the position n.

Parameters
iThe position of the element to return.
Exceptions
NotFoundRaised if no element was found.
Returns
Returns the element at the position n.

Definition at line 426 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, GUM_ERROR, gum::SplayTree< Element >::root, and gum::SplayBinaryNode< Element >::splay().

426  {
427  int val = i;
428 
429  if (!root) {
430  GUM_ERROR(NotFound, "The tree is empty !");
431  } else if (val >= root->size) {
432  GUM_ERROR(NotFound, "The index is too large !");
433  } else {
434  // The element exists
435  // Find it
436  SplayBinaryNode< Element >* act = root;
437  int pos_act = val - 1;
438  bool next = true;
439 
440  while (next) {
441  if (!act->fg)
442  pos_act = 0;
443  else
444  pos_act = act->fg->size;
445 
446  if (pos_act > val) {
447  act = act->fg;
448  } else if (pos_act < val) {
449  act = act->fd;
450  val -= (pos_act + 1);
451  } else {
452  next = false;
453  }
454  }
455 
456  root = act->splay();
457 
458  return root->elt;
459  }
460  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ operator[]() [2/2]

template<class Element >
const Element & gum::SplayTree< Element >::operator[] ( const unsigned int  i) const

Get the element at the position n.

Parameters
iThe position of the element to return.
Exceptions
NotFoundRaised if no element was found.
Returns
Returns the element at the position n.

Definition at line 463 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, GUM_ERROR, gum::SplayTree< Element >::root, and gum::SplayBinaryNode< Element >::splay().

463  {
464  int val = i;
465 
466  if (!root) {
467  GUM_ERROR(NotFound, "The tree is empty !");
468  } else if (val >= root->size) {
469  GUM_ERROR(NotFound, "The index is too large !");
470  } else {
471  // The element exists
472  // Find it
473  SplayBinaryNode< Element >* act = root;
474  int pos_act = val - 1;
475  bool next = true;
476 
477  while (next) {
478  if (!act->fg)
479  pos_act = 0;
480  else
481  pos_act = act->fg->size;
482 
483  if (pos_act > val) {
484  act = act->fg;
485  } else if (pos_act < val) {
486  act = act->fd;
487  val -= (pos_act + 1);
488  } else {
489  next = false;
490  }
491  }
492 
493  root = act->splay();
494 
495  return root->elt;
496  }
497  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
+ Here is the call graph for this function:

◆ popBack()

template<class Element >
INLINE void gum::SplayTree< Element >::popBack ( )

Remove the last element.

Definition at line 559 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, gum::SplayTree< Element >::root, and gum::SplayBinaryNode< Element >::splay().

559  {
560  SplayBinaryNode< Element >* act = root;
561 
562  if (root) {
563  if (root->fd)
564  for (; act->fd; act = act->fd)
565  ;
566 
567  act = act->splay();
568 
569  root = act->fg;
570 
571  if (root) root->pere = 0;
572 
573  act->fg = 0;
574 
575  delete act;
576  }
577  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
+ Here is the call graph for this function:

◆ popFront()

template<class Element >
INLINE void gum::SplayTree< Element >::popFront ( )

Remove the first element.

Definition at line 536 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, gum::SplayTree< Element >::root, and gum::SplayBinaryNode< Element >::splay().

536  {
537  SplayBinaryNode< Element >* act = root;
538 
539  if (root) {
540  if (root->fg)
541  for (; act->fg; act = act->fg)
542  ;
543 
544  act = act->splay();
545 
546  root = act->fd;
547 
548  if (root) root->pere = 0;
549 
550  act->fd = 0;
551 
552  delete act;
553  }
554  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
+ Here is the call graph for this function:

◆ pushBack()

template<class Element >
INLINE void gum::SplayTree< Element >::pushBack ( const Element &  e)

Add an element in the last position.

Parameters
eThe element to push.

Definition at line 601 of file splay_tpl.h.

References gum::SplayTree< Element >::addr, gum::SplayBinaryNode< Element >::fd, gum::SplayTree< Element >::root, and gum::SplayBinaryNode< Element >::splay().

601  {
602  SplayBinaryNode< Element >* act = root;
603 
604  if (root) {
605  if (root->fd)
606  for (; act->fd; act = act->fd)
607  ;
608 
609  root = act->splay();
610 
611  root->fd = new SplayBinaryNode< Element >(e, addr, 0, 0, root);
612  } else {
613  root = new SplayBinaryNode< Element >(e, addr);
614  }
615  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421
+ Here is the call graph for this function:

◆ pushFront()

template<class Element >
INLINE void gum::SplayTree< Element >::pushFront ( const Element &  e)

Add an element in the first position.

Parameters
eThe element to push.

Definition at line 582 of file splay_tpl.h.

References gum::SplayTree< Element >::addr, gum::SplayBinaryNode< Element >::fg, gum::SplayTree< Element >::root, and gum::SplayBinaryNode< Element >::splay().

582  {
583  SplayBinaryNode< Element >* act = root;
584 
585  if (root) {
586  if (root->fg)
587  for (; act->fg; act = act->fg)
588  ;
589 
590  root = act->splay();
591 
592  root->fg = new SplayBinaryNode< Element >(e, addr, 0, 0, root);
593  } else {
594  root = new SplayBinaryNode< Element >(e, addr);
595  }
596  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421
+ Here is the call graph for this function:

◆ size()

template<class Element >
INLINE Size gum::SplayTree< Element >::size ( ) const

The number of elements in the tree.

Returns
the size of the tree

Definition at line 778 of file splay_tpl.h.

References gum::SplayTree< Element >::root.

Referenced by gum::SplayTree< Element >::split().

778  {
779  if (root)
780  return root->size;
781  else
782  return Size(0);
783  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
+ Here is the caller graph for this function:

◆ split()

template<class Element >
INLINE SplayTree< Element > gum::SplayTree< Element >::split ( const int  i)

Divide the tree at the position.

Warning
all the elements less than e are stored into the tree and those who are greater than e in the returned tree.
Parameters
iThe position of the element (e) for split.
Returns
Returns a tree with all the elements greater than i.

Definition at line 673 of file splay_tpl.h.

References gum::SplayTree< Element >::addr, gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, gum::SplayBinaryNode< Element >::position(), gum::removeInfo(), gum::SplayTree< Element >::root, gum::SplayTree< Element >::size(), and gum::SplayBinaryNode< Element >::splay().

673  {
674  GUM_ASSERT(i >= 0 && i < root->size);
675  GUM_ASSERT(root != 0);
676 
677  if (i == 0) {
678  // the tree will be empty
679  // the returned tree is a copy of the present tree
680  SplayTree< Element > s = *this;
681  addr.clear();
682  delete root;
683  root = 0;
684  return s;
685  } else if (i == root->size - 1) {
686  SplayTree< Element > s;
687  return s;
688  } else {
689  // We will find the node at position i
690  SplayBinaryNode< Element >* act = root;
691  int pos = root->position();
692 
693  while (pos != i) {
694  GUM_ASSERT(act != 0);
695 
696  if (i < pos) {
697  act = act->fg;
698  } else {
699  act = act->fd;
700  }
701 
702  pos = act->position();
703  }
704 
705  // We reorganize the tree
706  act->splay();
707 
708  // We take the first part
709  root = act->fg;
710 
711  if (root) root->pere = 0;
712 
713  // We take the second part
714  SplayTree< Element > s;
715 
716  s.root = act;
717 
718  s.root->fg = 0;
719 
720  Size _size = 1;
721 
722  if (s.root->fd) _size += s.root->fd->size;
723 
724  s.root->size = _size;
725 
726  // We remove the old nodes in the hash table
727  // Complexity O(n) => very expensive
728  removeInfo(act, addr);
729 
730  return s;
731  }
732  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
Size size() const
The number of elements in the tree.
Definition: splay_tpl.h:778
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:48
static INLINE void removeInfo(const SplayBinaryNode< Element > *e, HashTable< Element, SplayBinaryNode< Element > * > &addr)
Definition: splay_tpl.h:660
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421
+ Here is the call graph for this function:

◆ split_by_val()

template<typename Element >
INLINE SplayTree< Element > gum::SplayTree< Element >::split_by_val ( const Element &  e)

Divide the tree at the position.

Parameters
ethe element for split
Warning
All the elements less than e are stored into the tree and those greater than e are in the returned tree
The element e is neither in the trees.
Returns
Returns the tree with all value greather than e.

Definition at line 738 of file splay_tpl.h.

References gum::SplayTree< Element >::addr, gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, GUM_ERROR, gum::removeInfo(), gum::SplayTree< Element >::root, and gum::SplayBinaryNode< Element >::splay().

738  {
739  GUM_ASSERT(root != 0);
740 
741  if (!addr.exists(e)) {
742  GUM_ERROR(NotFound, "not enough elements in the splay tree");
743  }
744 
745  // We will find the node at position i
746  SplayBinaryNode< Element >* act = addr[e];
747 
748  // We reorganize the tree
749  act->splay();
750 
751  // We take the two parts
752  SplayTree< Element > s;
753 
754  s.root = act->fd;
755 
756  if (s.root) { s.root->pere = 0; }
757 
758  root = act->fg;
759 
760  if (root) root->pere = 0;
761 
762  act->fg = 0;
763 
764  // We remove the old nodes in the hash table
765  // Complexity O(n) => very expensive
766  removeInfo(act, addr);
767 
768  act->fd = 0;
769 
770  delete act;
771 
772  return s;
773  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:418
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
static INLINE void removeInfo(const SplayBinaryNode< Element > *e, HashTable< Element, SplayBinaryNode< Element > * > &addr)
Definition: splay_tpl.h:660
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:421
+ Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator<<

template<class Element>
std::ostream& operator<< ( std::ostream &  out,
const SplayTree< Element > &  s 
)
friend

Friendly to display.

Definition at line 809 of file splay_tpl.h.

810  {
811  out << "|[";
812 
813  if (s.root) out << *s.root;
814 
815  out << "]|";
816 
817  return out;
818  }

Member Data Documentation

◆ addr

◆ root


The documentation for this class was generated from the following files: