lib/treemap/tree_map.rb
def rotate_right(root)
    pivot = root.left
    right = root.right
    pivot_left = pivot.left
    pivot_right = pivot.right

    # move the pivot's right child to the root's left
    root.left = pivot_right
    if pivot_right
      pivot_right.parent = root
    end

    replace_in_parent(root, pivot)

    # move the root to the pivot's right
    pivot.right = root
    root.parent = pivot

    # fix heights
    root.height = [right ? right.height : 0, pivot_right ? pivot_right.height : 0].max + 1
    pivot.height = [root.height, pivot_left ? pivot_left.height : 0].max + 1
  end
# Rotates the subtree so that its root's left child is the new root
Rotates the subtree so that its root's left child is the new root
Rotates the subtree so that its root ' s left child is the new root
def rotate_right(root)
    pivot = root.left
    right = root.right
    pivot_left = pivot.left
    pivot_right = pivot.right
    root.left = pivot_right
    if pivot_right
      pivot_right.parent = root
    end
    replace_in_parent(root, pivot)
    pivot.right = root
    root.parent = pivot
    root.height = [right ? right.height : 0, pivot_right ? pivot_right.height : 0].max + 1
    pivot.height = [root.height, pivot_left ? pivot_left.height : 0].max + 1
  end
def rotate_right ( root ) pivot = root . left right = root . right pivot_left = pivot . left pivot_right = pivot . right root . left = pivot_right if pivot_right pivot_right . parent = root end replace_in_parent ( root , pivot ) pivot . right = root root . parent = pivot root . height = [ right ? right . height : 0 , pivot_right ? pivot_right . height : 0 ] . max + 1 pivot . height = [ root . height , pivot_left ? pivot_left . height : 0 ] . max + 1 end
Rotates the subtree so that its root's left child is the new root
Rotates the subtree so that its root ' s left child is the new root
# move the pivot's right child to the root's left # move the root to the pivot's right # fix heights
root