private void Insert(IOrderStatisticTreeNode <TKey, TValue> root, IOrderStatisticTreeNode <TKey, TValue> newNode)
 {
     if (root.Key.CompareTo(newNode.Key) > 0)
     {
         if (root.LeftChild == null)
         {
             root.LeftSubtreeCount = 1;
             root.LeftChild        = newNode;
         }
         else
         {
             Insert((IOrderStatisticTreeNode <TKey, TValue>)root.LeftChild, newNode);
             root.LeftSubtreeCount++;
         }
     }
     else
     {
         if (root.RightChild == null)
         {
             root.RightSubtreeCount = 1;
             root.RightChild        = newNode;
         }
         else
         {
             Insert((IOrderStatisticTreeNode <TKey, TValue>)root.RightChild, newNode);
             root.RightSubtreeCount++;
         }
     }
 }
        private int GetRank(IOrderStatisticTreeNode <TKey, TValue> root, IOrderStatisticTreeNode <TKey, TValue> nodeToSearch, int shift)
        {
            if (root == null)
            {
                return(-1);
            }

            if (root == nodeToSearch)
            {
                return(root.LeftSubtreeCount + shift);
            }
            else if (root.Key.CompareTo(nodeToSearch.Key) > 0)
            {
                return(GetRank((IOrderStatisticTreeNode <TKey, TValue>)root.LeftChild, nodeToSearch, shift));
            }
            else
            {
                return(GetRank((IOrderStatisticTreeNode <TKey, TValue>)root.RightChild, nodeToSearch, shift + root.LeftSubtreeCount + 1));
            }
        }
        private IOrderStatisticTreeNode <TKey, TValue> FindTheKthSmallestNode(IOrderStatisticTreeNode <TKey, TValue> node, int k)
        {
            if (node == null)
            {
                return(null);
            }

            if (node.LeftSubtreeCount == k)
            {
                return(node);
            }
            else if (k > node.LeftSubtreeCount)
            {
                return(FindTheKthSmallestNode((IOrderStatisticTreeNode <TKey, TValue>)node.RightChild, k - node.LeftSubtreeCount - 1));
            }
            else
            {
                return(FindTheKthSmallestNode((IOrderStatisticTreeNode <TKey, TValue>)node.LeftChild, k));
            }
        }
 public int GetRank(IOrderStatisticTreeNode <TKey, TValue> node)
 {
     return(GetRank((IOrderStatisticTreeNode <TKey, TValue>)root, node, 0));
 }