I am trying to code a splay tree, for which i try to map the path from node to tree root. the problem is though that the mapping sequence gets called multiple times depending on which level the node is in => from what i can see by debugging.
So if the node is in heigh level 2 => the path function will printout the path and insert the node twice at the same location.
I have no idea why it is doing so, so i was hoping some of you might have an answer.
#include <iostream>
#include <string>
usingnamespace std;
//----------------------------------------------------------------//
struct splay_node{
int value;
splay_node* left;
splay_node* right;
splay_node* parent;
};
class splay_tree{
public:
splay_tree(int);
splay_node *a;
splay_node *root;
void insert(int);
void display(splay_node*);
private:
void insertp(splay_node*, int);
void splayingp(splay_node*,int);
};
splay_tree::splay_tree(int i) : a(new splay_node)
{
a = new splay_node;
a->value = i;
a->left = NULL;
a->right = NULL;
a->parent = NULL;
root = a;
cout << "Splay tree created root being " << root->value << endl;
}
void splay_tree::insert(int i)
{
insertp(root, i);
}
void splay_tree::insertp(splay_node *pointer,int i)
{
if (i < pointer->value) {
if (pointer->left != NULL) {
insertp(pointer->left, i);
}else
{
a = new splay_node;
a->value = i;
pointer->left = a;
a->left = NULL;
a->right = NULL;
a->parent = pointer;
}
}
elseif (i>pointer->value){
if (pointer->right != NULL) {
insertp(pointer->right, i);
}
else
{
a = new splay_node;
a->value = i;
pointer->right = a;
a->left = NULL;
a->right = NULL;
a->parent = pointer;
}
}
else
{
cout << "Value " << i << " Is already in the tree" << endl;
}
cout << "value " << i << " has been inserted" << endl;
splayingp(a,i);
}
void splay_tree::display(splay_node *root){
if(root != NULL)
{
display(root->left);
cout << root -> value << " " ;
display(root -> right);
}
}
void splay_tree::splayingp(splay_node *pointer, int value)
{
int level = 0;
string path;
while (pointer->parent != NULL) {
if (pointer->parent != NULL) {
level++;
if (pointer->parent -> left == pointer) {
path.push_back('L');
}
if (pointer->parent->right == pointer) {
path.push_back('R');
}
}
pointer = pointer->parent;
}
cout << "level: " << level << endl;
cout << path << endl;
}
int main(int argc, constchar * argv[]){
splay_tree splayed(5);
cout << endl;
splayed.insert(3);
cout << endl;
splayed.insert(4);
cout << endl;
splayed.insert(2);
cout << endl;
splayed.insert(1);
splayed.display(splayed.root);
cout << endl;
cout << splayed.root->left->right->parent->value << endl;
return 0;
}
And the output
Splay tree created root being 5
value 3 has been inserted
level: 1
L
value 4 has been inserted
level: 2
RL
value 4 has been inserted
level: 2
RL
value 2 has been inserted
level: 2
LL
value 2 has been inserted
level: 2
LL
value 1 has been inserted
level: 3
LLL
value 1 has been inserted
level: 3
LLL
value 1 has been inserted
level: 3
LLL
1 2 3 4 5
3
Program ended with exit code: 0
When a function returns, its caller continues executing the code that immediately follows the call. When the deepest recursive insertp() returns, the control flow will not resume in insert(), it will resume in insertp() and continue executing that function.
For example, a sample run might be
Call to insertp() (first call)
Line 46
Line 47
Line 48
Call to insertp() (second call)
Line 46
Line 61
Line 76
Line 81
Line 82
Call to splayingp()
(Omitted)
splayingp() returns
Execution of insertp() (second call) resumes
Line 83
insertp() (second call) returns
Execution of insertp() (first call) resumes
Line 49
Line 81
Line 82
Call to splayingp()
(Omitted)
splayingp() returns
Execution of insertp() (first call) resumes
insertp() (first call) returns
You need a return statement after the recursive calls to avoid calling splayingp() repeatedly.