Esempio n. 1
0
        public static Node GetByIndex(BaseRedBlackTree <Node> tree, int index, GetCount getCount)
        {
            if (index < 0 || index >= tree.Count)
            {
                throw new ArgumentException($"Index out of range {index} [0-{tree.Count})");
            }

            Node node = tree.Root;

            while (true)
            {
                if (node.Left != null && index < getCount(node.Left))
                {
                    node = node.Left;
                    continue;
                }

                if (node.Right == null)
                {
                    return(node);
                }

                index -= getCount(node) - getCount(node.Right);
                if (index < 0)
                {
                    return(node);
                }

                node = node.Right;
            }
        }
Esempio n. 2
0
        public static int IndexOf(BaseRedBlackTree <Node> tree, Node node, GetCount getCount)
        {
            if (node == null)
            {
                return(-1);
            }

            Debug.Assert(node.Root == node);
            int index = node.Left != null?getCount(node.Left) : 0;

            while (node.Parent != null)
            {
                if (node.Side)                 //if we're on the right side of a parent, add all the left sum
                {
                    index += getCount(node.Parent) - getCount(node);
                }

                node = node.Parent;
            }
            return(index);
        }