private NearestSmallerNode <TKey, TValue> Insert(TKey key, TValue value, NearestSmallerNode <TKey, TValue> node) { if (node == null) { return(new NearestSmallerNode <TKey, TValue>(key, value)); } if (key.CompareTo(node.Key) < 0) { node.Left = Insert(key, value, node.Left); if (node.GetBalance() == 2) { node = key.CompareTo(node.Left.Key) < 0 ? RotateRight(node) : RotateLeftRight(node); } } else if (key.CompareTo(node.Key) > 0) { node.Right = Insert(key, value, node.Right); if (node.GetBalance() == -2) { node = key.CompareTo(node.Right.Key) > 0 ? RotateLeft(node) : RotateRightLeft(node); } } else { node.Value = value; } node.RecalculateHeight(); return(node); }
private NearestSmallerNode <TKey, TValue> RotateRight(NearestSmallerNode <TKey, TValue> node1) { var node2 = node1.Left; node1.Left = node2.Right; node2.Right = node1; node1.RecalculateHeight(); node2.RecalculateHeight(); return(node2); }
public void Insert(TKey key, TValue value) { root = Insert(key, value, root); }
private NearestSmallerNode <TKey, TValue> RotateRightLeft(NearestSmallerNode <TKey, TValue> node1) { node1.Right = RotateRight(node1.Right); return(RotateLeft(node1)); }