Esempio n. 1
0
        private void Sort(int parentNodeIndex, IComparer <int> nodeIndexComparer, ref PartialArray <int> buffer)
        {
            int currentChild = _firstChildIndex[parentNodeIndex];

            // If no children, return
            if (currentChild <= 0)
            {
                return;
            }

            // Add all children of the current element to the buffer
            buffer.Clear();
            while (currentChild > 0)
            {
                buffer.Add(currentChild);
                currentChild = _nextSiblingIndex[currentChild];
            }

            // Sort the children by the compare function
            buffer.Sort(nodeIndexComparer);

            // Modify the FirstChild and NextSibling pointers to be in sorted order
            currentChild = buffer[0];
            _firstChildIndex[parentNodeIndex] = currentChild;

            for (int i = 1; i < buffer.Count; ++i)
            {
                int nextChild = buffer[i];
                _nextSiblingIndex[currentChild] = nextChild;
                currentChild = nextChild;
            }

            _nextSiblingIndex[currentChild] = -1;

            // Recurse on the children
            currentChild = _firstChildIndex[parentNodeIndex];
            while (currentChild > 0)
            {
                Sort(currentChild, nodeIndexComparer, ref buffer);
                currentChild = _nextSiblingIndex[currentChild];
            }
        }