K
K
Kirill Batalin2015-09-22 14:56:51
C++ / C#
Kirill Batalin, 2015-09-22 14:56:51

How to implement a position in the tree after the end and before the start?

There is a binary self-balancing tree. What is the best way to implement elements after the end and before the beginning (in order to then run through the tree using iterators)?
There was such an option: i.imgur.com/iqrWYI3.png
That is, add a parent element for the root of the tree. Then at the end of the detour I would always come there. But this method does not suit due to the fact that it will not be possible to return to the previous element. Once I get to this peak, then it will not be possible to get out of there. (In the screenshot, I showed pointers to the parent element. That is, the topmost element is the parent for itself and for root).
I also thought about adding a child vertex to the leftmost sheet, which will be an element before the start. Similarly with the bottom right sheet. But due to the constant rebuilding of the tree, he abandoned this idea.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
bak, 2015-10-01
@bak

For example, as it is done in one of the STL implementations:
1) end_node is a special node that is the parent of the root node.
2) root is the left child of end_node (parent and right child of end_node are zero).
3) Thanks to the properties listed above, when iterating to the end, we will come to end_node.
4) reverse_iterator - a separate type, constructed from a regular iterator, but behaves as follows:
- stores a regular iterator
- when accessed - returns the previous iterator (copies a regular one, calls -- from a regular one, returns what happened)
- inverts operations + +, --, +=, -=
5), respectively, rend is obtained from begin(), and rbegin() from end().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question