-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRBT.cpp
More file actions
23 lines (21 loc) · 1.13 KB
/
RBT.cpp
File metadata and controls
23 lines (21 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// C++ code
void RBT::RightRotation(TreeNode *y){
TreeNode *x = y->leftchild; // 把x設成y的leftchild
y->leftchild = x->rightchild; // 把x的rightchild放到y的leftchild
if (x->rightchild != neel){ // 若x的rightchild不為NIL, 將其parent指向y
x->rightchild->parent = y;
}
x->parent = y->parent; // 將x的parent指向原先y的parent
// 以下一組if-else將修改原先y的parent之child
if (y->parent == neel){ // 若y原先是root, x將成為新的root
root = x;
}
else if (y == y->parent->leftchild){ // 若原先y是其parent之leftchild,
y->parent->leftchild = x; // x就成為其新的parent之leftchild
}
else{ // 若原先y是其parent之rightchild,
y->parent->rightchild = x; // x就成為其新的parent之rightchild
}
x->rightchild = y; // 將y設為x之rightchild
y->parent = x; // 將x設為y之parent
}