5 #ifndef DSALGLIB_SPLAYTREE_H 6 #define DSALGLIB_SPLAYTREE_H 18 splaytnode *left,*right;
28 splaytnode(type param)
37 splaytnode* create_splaytnode(type param)
39 splaytnode *temp =
new splaytnode(param);
46 void _rotateR(splaytnode * &troot)
50 troot->left = temp->right;
55 void _rotateL(splaytnode * &troot)
59 troot->right = temp->left;
64 void _splay(splaytnode * &troot, type param)
66 if (troot ==
nullptr )
return;
68 if (param < troot->data) i=-1;
69 else if(troot->data < param) i=1;
76 if (troot->left ==
nullptr) return ;
78 if ( param < troot->left->data )
80 _splay(troot->left->left, param);
83 else if (troot->left->data < param)
86 _splay(troot->left->right, param);
88 if (troot->left->right !=
nullptr)
89 _rotateL(troot->left);
92 if(troot->left !=
nullptr) _rotateR(troot);
return;
96 if (troot->right ==
nullptr) return ;
99 if (param < troot->right->data)
101 _splay(troot->right->left, param);
102 if (troot->right->left !=
nullptr) _rotateR(troot->right);
104 else if (troot->right->data < param)
106 _splay(troot->right->right, param);
110 if(troot->right !=
nullptr) _rotateL(troot);
return;
114 splaytnode* _find_min(splaytnode* temp)
116 if(temp==
nullptr)
return nullptr;
117 if(temp->left==
nullptr)
return temp;
118 return _find_min(temp->left);
121 splaytnode* _find_max(splaytnode* temp)
123 if(temp==
nullptr)
return nullptr;
124 if(temp->right==
nullptr)
return temp;
125 return _find_max(temp->right);
128 bool _insert(splaytnode *temp)
131 _splay(root,temp->data);
133 if(root->data < temp->data)
148 splaytnode* _remove(splaytnode *troot,type param)
151 if( troot ==
nullptr )
return troot;
153 if( param < troot->data ) troot->left = _remove(troot->left,param);
154 else if( troot->data < param ) troot->right =_remove(troot->right,param);
155 else if(troot->left!=
nullptr&&troot->right!=
nullptr)
157 troot->data = _find_min(troot->right)->data;
158 troot->right = _remove(troot->right,troot->data);
162 splaytnode *temp = troot;
163 troot = ( troot->left != nullptr ) ? troot->left : troot->right;
164 delete temp; count--;
169 void _clear(splaytnode *temp)
171 if(temp==
nullptr)
return;
177 void _inorder(splaytnode *temp,
void (fun)(type obj))
179 if(temp==
nullptr)
return;
181 _inorder(temp->left,fun);
183 _inorder(temp->right,fun);
186 void _preorder(splaytnode *temp,
void (fun)(type obj))
188 if(temp==
nullptr)
return;
191 _preorder(temp->left,fun);
192 _preorder(temp->right,fun);
196 void _postorder(splaytnode *temp,
void (fun)(type obj))
198 if(temp==
nullptr)
return;
200 _postorder(temp->left,fun);
201 _postorder(temp->right,fun);
205 void _bfs(
void (fun) (type obj))
216 if(temp->left!=
nullptr) q.
enqueue(temp->left);
217 if(temp->right!=
nullptr) q.
enqueue(temp->right);
223 long long int _height(splaytnode *temp)
225 if(temp==
nullptr)
return -1;
226 return maxof(_height(temp->left),_height(temp->right))+1;
229 splaytnode* _search(splaytnode *temp,type val)
244 return _find_min(root)->data;
249 return _find_max(root)->data;
254 splaytnode *temp = create_splaytnode(param);
266 bool remove(type param)
288 return _height(root);
311 _postorder(root,fun);
314 void bfs(
void (fun) (type obj))
326 #endif //DSALGLIB_SPLAYTREE_H
void postorder(void(fun)(type obj))
void inorder(void(fun)(type obj))
void bfs(void(fun)(type obj))
void preorder(void(fun)(type obj))