0
Can anyone explain me the purpose of this line in splay tree function in c++
splay* Insert(int key, splay* root) { static splay* p_node = NULL; if (!p_node) p_node = New_Node(key); else p_node->key = key; if (!root) { root = p_node; p_node = NULL; //return root; } root = Splay(key, root); /* This is BST that, all keys <= root->key is in root->lchild, all keys > root->key is in root->rchild. */ if (key < root->key) { p_node->lchild = root->lchild; p_node->rchild = root; root->lchild = NULL; root = p_node; } else if (key > root->key) { //explain the line just below thus comment p_node->rchild = root->rchild; p_node->lchild = root; root->rchild = NULL; root = p_node; } else return root; p_node = NULL; return root; }
2 Réponses
0
I think that the meaning is
If root's key is smaller, make root as left child
of newnode and copy the right child of root to newnode
0
Read this article and try the implementation in playground to understand better...
https://www.geeksforgeeks.org/splay-tree-set-2-insert-delete/