private void swapPosition(RedBlackEntry<K, V> x, RedBlackEntry<K, V> y) {
		// Save initial values.
		RedBlackEntry<K, V> px = x.parent, lx = x.left, rx = x.right;
		RedBlackEntry<K, V> py = y.parent, ly = y.left, ry = y.right;
		boolean xWasLeftChild = px != null && x == px.left;
		boolean yWasLeftChild = py != null && y == py.left;

		// Swap, handling special cases of one being the other's parent.
		if (x == py) {  // x was y's parent
			x.parent = y;
			if (yWasLeftChild) {
				y.left = x;
				y.right = rx;
			}
			else {
				y.right = x;
				y.left = lx;
			}
		}
		else {
			x.parent = py;
			if (py != null) {
				if (yWasLeftChild) {
					py.left = x;
				}
				else {
					py.right = x;
				}
			}
			y.left = lx;
			y.right = rx;
		}

		if (y == px) { // y was x's parent
			y.parent = x;
			if (xWasLeftChild) {
				x.left = y;
				x.right = ry;
			}
			else {
				x.right = y;
				x.left = ly;
			}
		}
		else {
			y.parent = px;
			if (px != null) {
				if (xWasLeftChild) {
					px.left = y;
				}
				else {
					px.right = y;
				}
			}
			x.left = ly;
			x.right = ry;
		}

		// Fix children's parent pointers
		if (x.left != null) {
			x.left.parent = x;
		}
		if (x.right != null) {
			x.right.parent = x;
		}
		if (y.left != null) {
			y.left.parent = y;
		}
		if (y.right != null) {
			y.right.parent = y;
		}

		// Swap colors
		RedBlackEntry.NodeColor c = x.color;
		x.color = y.color;
		y.color = c;

		// Check if root changed
		if (root == x) {
			root = y;
		}
		else if (root == y) {
			root = x;
		}
	}
Swap the linkages of two nodes in a tree.
Ghidra/Framework/Generic/src/main/java/ghidra/util/datastruct/RedBlackTree.java