기타/What I Learned

[TIL] 자료구조 - 좌측 회전

가죽방패 2022. 8. 4. 09:17

※ 좌측 회전

- 레드 블랙 트리에서는 좌측 회전을 하는 코드가 다음 예시와 같지만 parent 노드를 가리키는 포인터와 isLeftChild 변수를 추가로 사용해야 하기 때문에 이러한 부분들을 고려해야 한다

 

public void leftRotate (Node<K,V> node){
	Node<K,V> temp = node.right;
	node.right = temp.left;
	if(node.right != null) {
		node.right.parent = null;
		node.right.isLeftChild = false;
	}
	if(node.parent == null) {
		root = temp;
		temp.parent = null;
	}
	else {
		temp.parent = node.parent;
		if(node.isLeftChild) {
			temp.isLeftChild = true;
			temp.parent.left = temp;
		} else {			
			temp.isLeftChild = false;
			temp.parent.right = temp;
		}
		temp.left = node;
		node.isLeftChild = true;
		node.parent = temp;
	}
}