aGrUM  0.14.2
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 41 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 363 of file splay_tpl.h.

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

◆ 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 374 of file splay_tpl.h.

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

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

◆ 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 383 of file splay_tpl.h.

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

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

◆ ~SplayTree()

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

Class destructor.

Definition at line 413 of file splay_tpl.h.

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

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

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 352 of file splay_tpl.h.

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

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

352  {
353  if (from.root) {
354  root = new SplayBinaryNode< Element >(*from.root, addr);
355  } else {
356  root = 0;
357  }
358  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:418
+ 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 516 of file splay_tpl.h.

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

516  {
517  SplayBinaryNode< Element >* act = root;
518 
519  if (!root) { GUM_ERROR(NotFound, "The splay tree is empty"); }
520 
521  if (act->fd)
522  for (; act->fd; act = act->fd)
523  ;
524 
525  root = act->splay();
526 
527  return root->elt;
528  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 785 of file splay_tpl.h.

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

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

◆ 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 499 of file splay_tpl.h.

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

499  {
500  SplayBinaryNode< Element >* act = root;
501 
502  if (!root) { GUM_ERROR(NotFound, "The splay tree is empty"); }
503 
504  if (act->fg)
505  for (; act->fg; act = act->fg)
506  ;
507 
508  root = act->splay();
509 
510  return root->elt;
511  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 617 of file splay_tpl.h.

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

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

◆ 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 643 of file splay_tpl.h.

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

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

◆ 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 393 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().

393  {
394  // avoid self assignment
395  if (this != &from) {
396  // for debugging purposes
397  GUM_OP_CPY(SplayTree);
398  // delete the old contents
399 
400  if (root) delete root;
401 
402  addr.clear();
403 
404  _copy(from);
405  }
406 
407  return *this;
408  }
void _copy(const SplayTree< Element > &)
a function used to perform copies
Definition: splay_tpl.h:352
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
SplayTree()
Basic constructor, make an empty splay tree.
Definition: splay_tpl.h:363
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:418
+ 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 423 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().

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

460  {
461  int val = i;
462 
463  if (!root) {
464  GUM_ERROR(NotFound, "The tree is empty !");
465  } else if (val >= root->size) {
466  GUM_ERROR(NotFound, "The index is too large !");
467  } else {
468  // The element exists
469  // Find it
470  SplayBinaryNode< Element >* act = root;
471  int pos_act = val - 1;
472  bool next = true;
473 
474  while (next) {
475  if (!act->fg)
476  pos_act = 0;
477  else
478  pos_act = act->fg->size;
479 
480  if (pos_act > val) {
481  act = act->fg;
482  } else if (pos_act < val) {
483  act = act->fd;
484  val -= (pos_act + 1);
485  } else {
486  next = false;
487  }
488  }
489 
490  root = act->splay();
491 
492  return root->elt;
493  }
494  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
#define GUM_ERROR(type, msg)
Definition: exceptions.h:52
+ 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 556 of file splay_tpl.h.

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

556  {
557  SplayBinaryNode< Element >* act = root;
558 
559  if (root) {
560  if (root->fd)
561  for (; act->fd; act = act->fd)
562  ;
563 
564  act = act->splay();
565 
566  root = act->fg;
567 
568  if (root) root->pere = 0;
569 
570  act->fg = 0;
571 
572  delete act;
573  }
574  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
+ 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 533 of file splay_tpl.h.

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

533  {
534  SplayBinaryNode< Element >* act = root;
535 
536  if (root) {
537  if (root->fg)
538  for (; act->fg; act = act->fg)
539  ;
540 
541  act = act->splay();
542 
543  root = act->fd;
544 
545  if (root) root->pere = 0;
546 
547  act->fd = 0;
548 
549  delete act;
550  }
551  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
+ 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 598 of file splay_tpl.h.

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

598  {
599  SplayBinaryNode< Element >* act = root;
600 
601  if (root) {
602  if (root->fd)
603  for (; act->fd; act = act->fd)
604  ;
605 
606  root = act->splay();
607 
608  root->fd = new SplayBinaryNode< Element >(e, addr, 0, 0, root);
609  } else {
610  root = new SplayBinaryNode< Element >(e, addr);
611  }
612  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:418
+ 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 579 of file splay_tpl.h.

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

579  {
580  SplayBinaryNode< Element >* act = root;
581 
582  if (root) {
583  if (root->fg)
584  for (; act->fg; act = act->fg)
585  ;
586 
587  root = act->splay();
588 
589  root->fg = new SplayBinaryNode< Element >(e, addr, 0, 0, root);
590  } else {
591  root = new SplayBinaryNode< Element >(e, addr);
592  }
593  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
HashTable< Element, SplayBinaryNode< Element > *> addr
The hash table to find quickly the position of a node.
Definition: splay.h:418
+ 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 775 of file splay_tpl.h.

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

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

775  {
776  if (root)
777  return root->size;
778  else
779  return Size(0);
780  }
SplayBinaryNode< Element > * root
Root of the tree.
Definition: splay.h:415
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:45
+ 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 670 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().

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

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

807  {
808  out << "|[";
809 
810  if (s.root) out << *s.root;
811 
812  out << "]|";
813 
814  return out;
815  }

Member Data Documentation

◆ addr

◆ root


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