aGrUM  0.20.3
a C++ library for (probabilistic) graphical models
gum::SplayBinaryNode< Element > Class Template Reference

the nodes of splay trees More...

#include <agrum/tools/core/splay.h>

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

Protected Attributes

Data Members
Element elt
 The content. More...
 
Size size
 The size of the sub-tree. More...
 
SplayBinaryNodefg
 The left child. More...
 
SplayBinaryNodefd
 The right child. More...
 
SplayBinaryNodepere
 The father, nullptr for the root. More...
 

Protected Member Functions

Constructors / Destructors
 SplayBinaryNode (const Element &e, HashTable< Element, SplayBinaryNode< Element > * > &addr, SplayBinaryNode *g=0, SplayBinaryNode *d=0, SplayBinaryNode *p=0)
 Basic constructor: creates a node with a reference to the element. More...
 
 SplayBinaryNode (const SplayBinaryNode< Element > &from, HashTable< Element, SplayBinaryNode< Element > * > &addr)
 Copy constructor. More...
 
void copy_ (const SplayBinaryNode< Element > &from, HashTable< Element, SplayBinaryNode< Element > * > &addr)
 A function used to perform copies. More...
 
 ~SplayBinaryNode ()
 Class destructor. More...
 

Friends

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

Accessors / Modifiers

int position () const
 Position of the node. More...
 
const Element & getElement () const
 Returns the element in the node. More...
 
const SplayBinaryNode< Element > * getFg () const
 Returns the left child. More...
 
const SplayBinaryNode< Element > * getFd () const
 Returns the right child. More...
 
SplayBinaryNode< Element > * zig ()
 A right rotation, the node must have a father. More...
 
SplayBinaryNode< Element > * zag ()
 A left rotation, the node must hava a father. More...
 
SplayBinaryNode< Element > * splay ()
 A splay rotation, the node will be the root of the tree. More...
 
SplayBinaryNode< Element > * join (const SplayBinaryNode< Element > *e, HashTable< Element, SplayBinaryNode< Element > * > &addr)
 Concatenation of two trees. More...
 

Detailed Description

template<class Element>
class gum::SplayBinaryNode< Element >

the nodes of splay trees

Author
Karim Tekkal

These file implements a data structure. A splay tree is a self-balancing binary search tree.

Definition at line 41 of file splay.h.

Constructor & Destructor Documentation

◆ SplayBinaryNode() [1/2]

template<class Element >
INLINE gum::SplayBinaryNode< Element >::SplayBinaryNode ( const Element &  e,
HashTable< Element, SplayBinaryNode< Element > * > &  addr,
SplayBinaryNode< Element > *  g = 0,
SplayBinaryNode< Element > *  d = 0,
SplayBinaryNode< Element > *  p = 0 
)
protected

Basic constructor: creates a node with a reference to the element.

Parameters
eThe element in the node.
addrTODO don't know what to do here.
gThe left child of the node, can be nullptr.
dThe right child of the node, can be nullptr.
pThe father of the node, can be nullptr if the node is the root of the tree.

Definition at line 72 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

77  :
78  elt(e),
79  size(1), fg(g), fd(d), pere(p) {
80  if (addr.exists(elt))
81  addr[elt] = this;
82  else
83  addr.insert(elt, this);
84 
85  // for debugging purposes
86  GUM_CONSTRUCTOR(SplayBinaryNode);
87  }
SplayBinaryNode * pere
The father, nullptr for the root.
Definition: splay.h:199
SplayBinaryNode(const Element &e, HashTable< Element, SplayBinaryNode< Element > * > &addr, SplayBinaryNode *g=0, SplayBinaryNode *d=0, SplayBinaryNode *p=0)
Basic constructor: creates a node with a reference to the element.
Definition: splay_tpl.h:72
Size size
The size of the sub-tree.
Definition: splay.h:190
SplayBinaryNode * fg
The left child.
Definition: splay.h:193
Element elt
The content.
Definition: splay.h:187
SplayBinaryNode * fd
The right child.
Definition: splay.h:196
+ Here is the call graph for this function:

◆ SplayBinaryNode() [2/2]

template<class Element >
INLINE gum::SplayBinaryNode< Element >::SplayBinaryNode ( const SplayBinaryNode< Element > &  from,
HashTable< Element, SplayBinaryNode< Element > * > &  addr 
)
protected

Copy constructor.

Parameters
fromthe src SplayBinaryNode
addrTODO don't know what to do here.

Definition at line 92 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

94  {
95  copy_(from, addr);
96  // for debugging purposes
97  GUM_CONSTRUCTOR(SplayBinaryNode);
98  }
SplayBinaryNode(const Element &e, HashTable< Element, SplayBinaryNode< Element > * > &addr, SplayBinaryNode *g=0, SplayBinaryNode *d=0, SplayBinaryNode *p=0)
Basic constructor: creates a node with a reference to the element.
Definition: splay_tpl.h:72
void copy_(const SplayBinaryNode< Element > &from, HashTable< Element, SplayBinaryNode< Element > * > &addr)
A function used to perform copies.
Definition: splay_tpl.h:41
+ Here is the call graph for this function:

◆ ~SplayBinaryNode()

template<class Element >
INLINE gum::SplayBinaryNode< Element >::~SplayBinaryNode ( )
protected

Class destructor.

Definition at line 103 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

103  {
104  // for debugging purposes
105  GUM_DESTRUCTOR(SplayBinaryNode);
106  // remove the subtrees
107 
108  if (fg) delete fg;
109 
110  if (fd) delete fd;
111  }
SplayBinaryNode(const Element &e, HashTable< Element, SplayBinaryNode< Element > * > &addr, SplayBinaryNode *g=0, SplayBinaryNode *d=0, SplayBinaryNode *p=0)
Basic constructor: creates a node with a reference to the element.
Definition: splay_tpl.h:72
SplayBinaryNode * fg
The left child.
Definition: splay.h:193
SplayBinaryNode * fd
The right child.
Definition: splay.h:196
+ Here is the call graph for this function:

Member Function Documentation

◆ copy_()

template<class Element >
INLINE void gum::SplayBinaryNode< Element >::copy_ ( const SplayBinaryNode< Element > &  from,
HashTable< Element, SplayBinaryNode< Element > * > &  addr 
)
protected

A function used to perform copies.

Parameters
fromthe src SplayBinaryNode
addrTODO don't know what to do here ..

Definition at line 41 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

42  {
43  if (addr.exists(from.elt))
44  addr[from.elt] = this;
45  else
46  addr.insert(from.elt, this);
47 
48  elt = from.elt;
49 
50  size = from.size;
51 
52  pere = 0;
53 
54  if (from.fg) {
55  fg = new SplayBinaryNode< Element >(*from.fg, addr);
56  fg->pere = this;
57  } else {
58  fg = 0;
59  }
60 
61  if (from.fd) {
62  fd = new SplayBinaryNode< Element >(*from.fd, addr);
63  fd->pere = this;
64  } else {
65  fd = 0;
66  }
67  }
SplayBinaryNode * pere
The father, nullptr for the root.
Definition: splay.h:199
Size size
The size of the sub-tree.
Definition: splay.h:190
SplayBinaryNode * fg
The left child.
Definition: splay.h:193
Element elt
The content.
Definition: splay.h:187
SplayBinaryNode * fd
The right child.
Definition: splay.h:196
+ Here is the call graph for this function:

◆ getElement()

template<class Element >
INLINE const Element & gum::SplayBinaryNode< Element >::getElement ( ) const

Returns the element in the node.

Returns
Returns the element in the node.

Definition at line 319 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

319  {
320  return elt;
321  }
Element elt
The content.
Definition: splay.h:187
+ Here is the call graph for this function:

◆ getFd()

template<class Element >
INLINE const SplayBinaryNode< Element > * gum::SplayBinaryNode< Element >::getFd ( ) const

Returns the right child.

Returns
Returns the right child.
Warning
The returned value can be null.

Definition at line 339 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

339  {
340  return fd;
341  }
SplayBinaryNode * fd
The right child.
Definition: splay.h:196
+ Here is the call graph for this function:

◆ getFg()

template<class Element >
INLINE const SplayBinaryNode< Element > * gum::SplayBinaryNode< Element >::getFg ( ) const

Returns the left child.

Returns
Returns the left child.
Warning
The returned value can be null.

Definition at line 329 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

329  {
330  return fg;
331  }
SplayBinaryNode * fg
The left child.
Definition: splay.h:193
+ Here is the call graph for this function:

◆ join()

template<class Element >
INLINE SplayBinaryNode< Element > * gum::SplayBinaryNode< Element >::join ( const SplayBinaryNode< Element > *  e,
HashTable< Element, SplayBinaryNode< Element > * > &  addr 
)
protected

Concatenation of two trees.

Parameters
eThe node to add.
addrTODO Don't know what to do here.
Returns
Returns the root of the created tree.

Definition at line 263 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

264  {
265  SplayBinaryNode< Element >* b = new SplayBinaryNode< Element >(*e, addr);
266  GUM_ASSERT(b != 0);
267  SplayBinaryNode< Element >* act = this;
268 
269  for (; act->fd; act = act->fd)
270  ;
271 
272  // act is the rightmost element
273  act->splay();
274 
275  // insertion
276  act->fd = b;
277 
278  b->pere = act;
279 
280  act->size = 1;
281 
282  if (act->fg) act->size += act->fg->size;
283 
284  act->size += act->fd->size;
285 
286  return act;
287  }
+ Here is the call graph for this function:

◆ position()

template<class Element >
INLINE int gum::SplayBinaryNode< Element >::position ( ) const

Position of the node.

Returns
The position of the node into the tree.

Definition at line 292 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

292  {
293  if (!pere) {
294  // I'm the root
295  if (fg)
296  return fg->size + 1;
297  else
298  return 0;
299  } else if (pere->fg == this) {
300  // I'm the left child of my father
301  int pos = pere->position() - 1;
302 
303  if (fd) pos -= fd->size;
304 
305  return pos;
306  } else {
307  // I'm the right child of my father
308  int pos = pere->position() + 1;
309 
310  if (fg) pos += fg->size;
311 
312  return pos;
313  }
314  }
SplayBinaryNode * pere
The father, nullptr for the root.
Definition: splay.h:199
Size size
The size of the sub-tree.
Definition: splay.h:190
SplayBinaryNode * fg
The left child.
Definition: splay.h:193
SplayBinaryNode * fd
The right child.
Definition: splay.h:196
int position() const
Position of the node.
Definition: splay_tpl.h:292
+ Here is the call graph for this function:

◆ splay()

template<class Element >
INLINE SplayBinaryNode< Element > * gum::SplayBinaryNode< Element >::splay ( )
protected

A splay rotation, the node will be the root of the tree.

Returns
Returns a pointer to the root of the sub-tree after rotation.

Definition at line 217 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

217  {
218  SplayBinaryNode< Element >* gdp;
219 
220  while (pere) {
221  // While this node isn't the root
222  gdp = pere->pere; // gdp can be nullptr
223 
224  if (!gdp) {
225  if (this == pere->fg) {
226  // I'm the left child of my father
227  return zig();
228  } else {
229  GUM_ASSERT(this == pere->fd);
230  // I'm the right child of my father
231  return zag();
232  }
233  } else {
234  if (this == pere->fg) {
235  // I'm the left child of my father
236  if (pere == gdp->fg) {
237  gdp->fg = zig();
238  } else {
239  GUM_ASSERT(pere == gdp->fd);
240  gdp->fd = zig();
241  }
242  } else {
243  GUM_ASSERT(this == pere->fd);
244  // I'm the right child of my father
245 
246  if (pere == gdp->fg) {
247  gdp->fg = zag();
248  } else {
249  GUM_ASSERT(pere == gdp->fd);
250  gdp->fd = zag();
251  }
252  }
253  }
254  }
255 
256  return this; // for compiler satisfaction
257  }
SplayBinaryNode * pere
The father, nullptr for the root.
Definition: splay.h:199
SplayBinaryNode< Element > * zag()
A left rotation, the node must hava a father.
Definition: splay_tpl.h:167
SplayBinaryNode * fg
The left child.
Definition: splay.h:193
SplayBinaryNode * fd
The right child.
Definition: splay.h:196
SplayBinaryNode< Element > * zig()
A right rotation, the node must have a father.
Definition: splay_tpl.h:116
+ Here is the call graph for this function:

◆ zag()

template<class Element >
INLINE SplayBinaryNode< Element > * gum::SplayBinaryNode< Element >::zag ( )
protected

A left rotation, the node must hava a father.

Returns
Returns a pointer to the root of the sub-tree after rotation.

Definition at line 167 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

167  {
168  Size size_;
169  // Must be call on a node with a father
170  GUM_ASSERT(pere != 0);
171  // We chain childs
172  pere->fd = fg;
173 
174  if (fg) fg->pere = pere;
175 
176  fg = pere;
177 
178  SplayBinaryNode< Element >* p = fg->pere;
179 
180  fg->pere = this;
181 
182  // We compute the size of rigth child
183  size_ = 1;
184 
185  if (fg->fg) size_ += fg->fg->size;
186 
187  if (fg->fd) size_ += fg->fd->size;
188 
189  fg->size = size_;
190 
191  if (fd) size_ += fd->size;
192 
193  ++size_;
194 
195  size = size_;
196 
197  // We chain father
198  pere = p;
199 
200  if (pere) {
201  if (pere->fg == fg) {
202  // I'm left child of my father
203  pere->fg = this;
204  } else {
205  // I'm right child of my father
206  GUM_ASSERT(pere->fd == fg);
207  pere->fd = this;
208  }
209  }
210 
211  return this;
212  }
SplayBinaryNode * pere
The father, nullptr for the root.
Definition: splay.h:199
Size size
The size of the sub-tree.
Definition: splay.h:190
SplayBinaryNode * fg
The left child.
Definition: splay.h:193
SplayBinaryNode * fd
The right child.
Definition: splay.h:196
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

◆ zig()

template<class Element >
INLINE SplayBinaryNode< Element > * gum::SplayBinaryNode< Element >::zig ( )
protected

A right rotation, the node must have a father.

Returns
Returns a pointer to the root of the sub-tree after rotation.

Definition at line 116 of file splay_tpl.h.

References gum::Set< Key, Alloc >::emplace().

116  {
117  Size size_;
118  // Must be called on a node with a father
119  GUM_ASSERT(pere != 0);
120  // We chain childs
121  pere->fg = fd;
122 
123  if (fd) fd->pere = pere;
124 
125  fd = pere;
126 
127  SplayBinaryNode< Element >* p = fd->pere;
128 
129  fd->pere = this;
130 
131  // We compute the size of rigth child
132  size_ = 1;
133 
134  if (fd->fg) size_ += fd->fg->size;
135 
136  if (fd->fd) size_ += fd->fd->size;
137 
138  fd->size = size_;
139 
140  // size_ == fd->size
141  if (fg) size_ += fg->size;
142 
143  ++size_;
144 
145  size = size_;
146 
147  // We chain father
148  pere = p;
149 
150  if (pere) {
151  if (pere->fg == fd) {
152  // I'm left child of my father
153  pere->fg = this;
154  } else {
155  // I'm right child of my father
156  GUM_ASSERT(pere->fd == fd);
157  pere->fd = this;
158  }
159  }
160 
161  return this;
162  }
SplayBinaryNode * pere
The father, nullptr for the root.
Definition: splay.h:199
Size size
The size of the sub-tree.
Definition: splay.h:190
SplayBinaryNode * fg
The left child.
Definition: splay.h:193
SplayBinaryNode * fd
The right child.
Definition: splay.h:196
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition: types.h:47
+ Here is the call graph for this function:

Friends And Related Function Documentation

◆ operator<<

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

Friendly to display.

Definition at line 787 of file splay_tpl.h.

787  {
788  if (e.fg) out << *e.fg << ",";
789 
790  out << e.elt;
791 
792  if (e.fd) out << "," << *e.fd;
793 
794  return out;
795  }

◆ SplayTree< Element >

template<class Element >
friend class SplayTree< Element >
friend

Friendly with SplayTree.

Definition at line 204 of file splay.h.

Member Data Documentation

◆ elt

template<class Element >
Element gum::SplayBinaryNode< Element >::elt
protected

The content.

Definition at line 187 of file splay.h.

◆ fd

template<class Element >
SplayBinaryNode* gum::SplayBinaryNode< Element >::fd
protected

The right child.

Definition at line 196 of file splay.h.

◆ fg

template<class Element >
SplayBinaryNode* gum::SplayBinaryNode< Element >::fg
protected

The left child.

Definition at line 193 of file splay.h.

◆ pere

template<class Element >
SplayBinaryNode* gum::SplayBinaryNode< Element >::pere
protected

The father, nullptr for the root.

Definition at line 199 of file splay.h.

◆ size

template<class Element >
Size gum::SplayBinaryNode< Element >::size
protected

The size of the sub-tree.

Definition at line 190 of file splay.h.


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