aGrUM  0.14.2
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 39 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 70 of file splay_tpl.h.

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

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

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

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

92  {
93  _copy(from, addr);
94  // for debugging purposes
95  GUM_CONSTRUCTOR(SplayBinaryNode);
96  }
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:70
void _copy(const SplayBinaryNode< Element > &from, HashTable< Element, SplayBinaryNode< Element > * > &addr)
A function used to perform copies.
Definition: splay_tpl.h:38
+ Here is the call graph for this function:

◆ ~SplayBinaryNode()

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

Class destructor.

Definition at line 101 of file splay_tpl.h.

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

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

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 38 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().

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

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

Referenced by gum::removeInfo().

317  {
318  return elt;
319  }
Element elt
The content.
Definition: splay.h:188
+ 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 339 of file splay_tpl.h.

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

Referenced by gum::removeInfo().

339  {
340  return fd;
341  }
SplayBinaryNode * fd
The right child.
Definition: splay.h:197
+ 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 328 of file splay_tpl.h.

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

Referenced by gum::removeInfo().

328  {
329  return fg;
330  }
SplayBinaryNode * fg
The left child.
Definition: splay.h:194
+ 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 260 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().

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

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

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

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

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

793  {
794  if (e.fg) out << *e.fg << ",";
795 
796  out << e.elt;
797 
798  if (e.fd) out << "," << *e.fd;
799 
800  return out;
801  }

◆ SplayTree< Element >

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

Friendly with SplayTree.

Definition at line 205 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: