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

the nodes of splay trees More...

#include <agrum/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 42 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 73 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::elt.

78  :
79  elt(e),
80  size(1), fg(g), fd(d), pere(p) {
81  if (addr.exists(elt))
82  addr[elt] = this;
83  else
84  addr.insert(elt, this);
85 
86  // for debugging purposes
87  GUM_CONSTRUCTOR(SplayBinaryNode);
88  }
SplayBinaryNode * pere
The father, nullptr for the root.
Definition: splay.h:203
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:73
Size size
The size of the sub-tree.
Definition: splay.h:194
SplayBinaryNode * fg
The left child.
Definition: splay.h:197
Element elt
The content.
Definition: splay.h:191
SplayBinaryNode * fd
The right child.
Definition: splay.h:200

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

References gum::SplayBinaryNode< Element >::_copy().

95  {
96  _copy(from, addr);
97  // for debugging purposes
98  GUM_CONSTRUCTOR(SplayBinaryNode);
99  }
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:73
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 104 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::fd, and gum::SplayBinaryNode< Element >::fg.

104  {
105  // for debugging purposes
106  GUM_DESTRUCTOR(SplayBinaryNode);
107  // remove the subtrees
108 
109  if (fg) delete fg;
110 
111  if (fd) delete fd;
112  }
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:73
SplayBinaryNode * fg
The left child.
Definition: splay.h:197
SplayBinaryNode * fd
The right child.
Definition: splay.h:200

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::SplayBinaryNode< Element >::elt, gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, gum::SplayBinaryNode< Element >::pere, and gum::SplayBinaryNode< Element >::size.

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

43  {
44  if (addr.exists(from.elt))
45  addr[from.elt] = this;
46  else
47  addr.insert(from.elt, this);
48 
49  elt = from.elt;
50 
51  size = from.size;
52 
53  pere = 0;
54 
55  if (from.fg) {
56  fg = new SplayBinaryNode< Element >(*from.fg, addr);
57  fg->pere = this;
58  } else {
59  fg = 0;
60  }
61 
62  if (from.fd) {
63  fd = new SplayBinaryNode< Element >(*from.fd, addr);
64  fd->pere = this;
65  } else {
66  fd = 0;
67  }
68  }
SplayBinaryNode * pere
The father, nullptr for the root.
Definition: splay.h:203
Size size
The size of the sub-tree.
Definition: splay.h:194
SplayBinaryNode * fg
The left child.
Definition: splay.h:197
Element elt
The content.
Definition: splay.h:191
SplayBinaryNode * fd
The right child.
Definition: splay.h:200
+ Here is the caller 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 320 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::elt.

Referenced by gum::removeInfo().

320  {
321  return elt;
322  }
Element elt
The content.
Definition: splay.h:191
+ Here is the caller 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 342 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::fd.

Referenced by gum::removeInfo().

342  {
343  return fd;
344  }
SplayBinaryNode * fd
The right child.
Definition: splay.h:200
+ Here is the caller 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 331 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::fg.

Referenced by gum::removeInfo().

331  {
332  return fg;
333  }
SplayBinaryNode * fg
The left child.
Definition: splay.h:197
+ Here is the caller 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::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, gum::SplayBinaryNode< Element >::pere, gum::SplayBinaryNode< Element >::size, and gum::SplayBinaryNode< Element >::splay().

265  {
266  SplayBinaryNode< Element >* b = new SplayBinaryNode< Element >(*e, addr);
267  GUM_ASSERT(b != 0);
268  SplayBinaryNode< Element >* act = this;
269 
270  for (; act->fd; act = act->fd)
271  ;
272 
273  // act is the rightmost element
274  act->splay();
275 
276  // insertion
277  act->fd = b;
278 
279  b->pere = act;
280 
281  act->size = 1;
282 
283  if (act->fg) act->size += act->fg->size;
284 
285  act->size += act->fd->size;
286 
287  return act;
288  }
+ 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 293 of file splay_tpl.h.

References gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, gum::SplayBinaryNode< Element >::pere, gum::SplayBinaryNode< Element >::position(), and gum::SplayBinaryNode< Element >::size.

Referenced by gum::SplayBinaryNode< Element >::position(), and gum::SplayTree< Element >::split().

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

References gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, gum::SplayBinaryNode< Element >::pere, gum::SplayBinaryNode< Element >::zag(), and gum::SplayBinaryNode< Element >::zig().

Referenced by gum::SplayTree< Element >::back(), gum::SplayTree< Element >::front(), gum::SplayBinaryNode< Element >::join(), gum::SplayTree< Element >::operator[](), gum::SplayTree< Element >::popBack(), gum::SplayTree< Element >::popFront(), gum::SplayTree< Element >::pushBack(), gum::SplayTree< Element >::pushFront(), gum::SplayTree< Element >::split(), and gum::SplayTree< Element >::split_by_val().

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

References gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, gum::SplayBinaryNode< Element >::pere, and gum::SplayBinaryNode< Element >::size.

Referenced by gum::SplayBinaryNode< Element >::splay().

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

References gum::SplayBinaryNode< Element >::fd, gum::SplayBinaryNode< Element >::fg, gum::SplayBinaryNode< Element >::pere, and gum::SplayBinaryNode< Element >::size.

Referenced by gum::SplayBinaryNode< Element >::splay().

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

796  {
797  if (e.fg) out << *e.fg << ",";
798 
799  out << e.elt;
800 
801  if (e.fd) out << "," << *e.fd;
802 
803  return out;
804  }

◆ SplayTree< Element >

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

Friendly with SplayTree.

Definition at line 208 of file splay.h.

Member Data Documentation

◆ elt

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

◆ fd

◆ fg

◆ pere

◆ size


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