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

the nodes of splay trees More...

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

Collaboration diagram for gum::SplayBinaryNode< Element >:

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.
 SplayBinaryNode (const SplayBinaryNode< Element > &from, HashTable< Element, SplayBinaryNode< Element > * > &addr)
 Copy constructor.
void copy_ (const SplayBinaryNode< Element > &from, HashTable< Element, SplayBinaryNode< Element > * > &addr)
 A function used to perform copies.
 ~SplayBinaryNode ()
 Class destructor.

Protected Attributes

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

Friends

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

Accessors / Modifiers

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

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

96 : elt(e), size(1), fg(g), fd(d), pere(p) {
97 if (addr.exists(elt)) addr[elt] = this;
98 else addr.insert(elt, this);
99
100 // for debugging purposes
102 }
the nodes of splay trees
Definition splay.h:91
SplayBinaryNode * fg
The left child.
Definition splay.h:214
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:91
SplayBinaryNode * pere
The father, nullptr for the root.
Definition splay.h:220
SplayBinaryNode * fd
The right child.
Definition splay.h:217
Element elt
The content.
Definition splay.h:208
Size size
The size of the sub-tree.
Definition splay.h:211

References SplayBinaryNode(), elt, fd, fg, pere, and size.

Referenced by SplayBinaryNode(), SplayBinaryNode(), ~SplayBinaryNode(), copy_(), getFd(), getFg(), join(), operator<<, splay(), zag(), and zig().

Here is the call graph for this function:
Here is the caller 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 107 of file splay_tpl.h.

109 {
110 copy_(from, addr);
111 // for debugging purposes
113 }
void copy_(const SplayBinaryNode< Element > &from, HashTable< Element, SplayBinaryNode< Element > * > &addr)
A function used to perform copies.
Definition splay_tpl.h:62

References SplayBinaryNode(), and copy_().

Here is the call graph for this function:

◆ ~SplayBinaryNode()

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

Class destructor.

Definition at line 118 of file splay_tpl.h.

118 {
119 // for debugging purposes
121 // remove the subtrees
122
123 if (fg) delete fg;
124
125 if (fd) delete fd;
126 }

References SplayBinaryNode(), fd, and fg.

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

63 {
64 if (addr.exists(from.elt)) addr[from.elt] = this;
65 else addr.insert(from.elt, this);
66
67 elt = from.elt;
68
69 size = from.size;
70
71 pere = 0;
72
73 if (from.fg) {
75 fg->pere = this;
76 } else {
77 fg = 0;
78 }
79
80 if (from.fd) {
82 fd->pere = this;
83 } else {
84 fd = 0;
85 }
86 }

References SplayBinaryNode(), elt, fd, fg, pere, and size.

Referenced by SplayBinaryNode().

Here is the call graph for this function:
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 332 of file splay_tpl.h.

332 {
333 return elt;
334 }

References elt.

Referenced by gum::removeInfo().

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

352 {
353 return fd;
354 }

References SplayBinaryNode(), and fd.

Referenced by gum::removeInfo().

Here is the call graph for this function:
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 342 of file splay_tpl.h.

342 {
343 return fg;
344 }

References SplayBinaryNode(), and fg.

Referenced by gum::removeInfo().

Here is the call graph for this function:
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 278 of file splay_tpl.h.

279 {
281 GUM_ASSERT(b != 0);
283
284 for (; act->fd; act = act->fd)
285 ;
286
287 // act is the rightmost element
288 act->splay();
289
290 // insertion
291 act->fd = b;
292
293 b->pere = act;
294
295 act->size = 1;
296
297 if (act->fg) act->size += act->fg->size;
298
299 act->size += act->fd->size;
300
301 return act;
302 }
SplayBinaryNode< Element > * splay()
A splay rotation, the node will be the root of the tree.
Definition splay_tpl.h:232

References SplayBinaryNode(), fd, fg, pere, size, and splay().

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

307 {
308 if (!pere) {
309 // I'm the root
310 if (fg) return fg->size + 1;
311 else return 0;
312 } else if (pere->fg == this) {
313 // I'm the left child of my father
314 int pos = pere->position() - 1;
315
316 if (fd) pos -= fd->size;
317
318 return pos;
319 } else {
320 // I'm the right child of my father
321 int pos = pere->position() + 1;
322
323 if (fg) pos += fg->size;
324
325 return pos;
326 }
327 }

References fd, fg, and pere.

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

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

232 {
234
235 while (pere) {
236 // While this node isn't the root
237 gdp = pere->pere; // gdp can be nullptr
238
239 if (!gdp) {
240 if (this == pere->fg) {
241 // I'm the left child of my father
242 return zig();
243 } else {
244 GUM_ASSERT(this == pere->fd);
245 // I'm the right child of my father
246 return zag();
247 }
248 } else {
249 if (this == pere->fg) {
250 // I'm the left child of my father
251 if (pere == gdp->fg) {
252 gdp->fg = zig();
253 } else {
254 GUM_ASSERT(pere == gdp->fd);
255 gdp->fd = zig();
256 }
257 } else {
258 GUM_ASSERT(this == pere->fd);
259 // I'm the right child of my father
260
261 if (pere == gdp->fg) {
262 gdp->fg = zag();
263 } else {
264 GUM_ASSERT(pere == gdp->fd);
265 gdp->fd = zag();
266 }
267 }
268 }
269 }
270
271 return this; // for compiler satisfaction
272 }
SplayBinaryNode< Element > * zag()
A left rotation, the node must hava a father.
Definition splay_tpl.h:182
SplayBinaryNode< Element > * zig()
A right rotation, the node must have a father.
Definition splay_tpl.h:131

References SplayBinaryNode(), fd, fg, pere, zag(), and zig().

Referenced by gum::SplayTree< Element >::back(), gum::SplayTree< Element >::front(), gum::SplayTree< Element >::insert(), join(), gum::SplayTree< Element >::operator[](), 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().

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

182 {
183 Size size_;
184 // Must be call on a node with a father
185 GUM_ASSERT(pere != 0);
186 // We chain childs
187 pere->fd = fg;
188
189 if (fg) fg->pere = pere;
190
191 fg = pere;
192
194
195 fg->pere = this;
196
197 // We compute the size of rigth child
198 size_ = 1;
199
200 if (fg->fg) size_ += fg->fg->size;
201
202 if (fg->fd) size_ += fg->fd->size;
203
204 fg->size = size_;
205
206 if (fd) size_ += fd->size;
207
208 ++size_;
209
210 size = size_;
211
212 // We chain father
213 pere = p;
214
215 if (pere) {
216 if (pere->fg == fg) {
217 // I'm left child of my father
218 pere->fg = this;
219 } else {
220 // I'm right child of my father
221 GUM_ASSERT(pere->fd == fg);
222 pere->fd = this;
223 }
224 }
225
226 return this;
227 }

References SplayBinaryNode(), fd, fg, pere, and size.

Referenced by splay().

Here is the call graph for this function:
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 131 of file splay_tpl.h.

131 {
132 Size size_;
133 // Must be called on a node with a father
134 GUM_ASSERT(pere != 0);
135 // We chain childs
136 pere->fg = fd;
137
138 if (fd) fd->pere = pere;
139
140 fd = pere;
141
143
144 fd->pere = this;
145
146 // We compute the size of rigth child
147 size_ = 1;
148
149 if (fd->fg) size_ += fd->fg->size;
150
151 if (fd->fd) size_ += fd->fd->size;
152
153 fd->size = size_;
154
155 // size_ == fd->size
156 if (fg) size_ += fg->size;
157
158 ++size_;
159
160 size = size_;
161
162 // We chain father
163 pere = p;
164
165 if (pere) {
166 if (pere->fg == fd) {
167 // I'm left child of my father
168 pere->fg = this;
169 } else {
170 // I'm right child of my father
171 GUM_ASSERT(pere->fd == fd);
172 pere->fd = this;
173 }
174 }
175
176 return this;
177 }

References SplayBinaryNode(), fd, fg, pere, and size.

Referenced by splay().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ operator<<

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

Friendly to display.

Definition at line 794 of file splay_tpl.h.

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

References SplayBinaryNode(), elt, fd, and fg.

◆ SplayTree< Element >

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

Friendly with SplayTree.

Definition at line 220 of file splay.h.

Member Data Documentation

◆ elt

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

The content.

Definition at line 208 of file splay.h.

Referenced by SplayBinaryNode(), copy_(), getElement(), and operator<<.

◆ fd

◆ fg

◆ pere

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

The father, nullptr for the root.

Definition at line 220 of file splay.h.

Referenced by SplayBinaryNode(), copy_(), join(), position(), splay(), zag(), and zig().

◆ size

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

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