/// <summary>
        /// Rebuild the tree, in O(n).
        /// </summary>
        public void RebuildDocument()
        {
            foreach (CollapsedLineSection s in GetAllCollapsedSections())
            {
                s.Start = null;
                s.End   = null;
            }

            HeightTreeNode[] nodes = new HeightTreeNode[document.LineCount];
            int lineNumber         = 0;

            foreach (DocumentLine ls in document.Lines)
            {
                nodes[lineNumber++] = new HeightTreeNode(ls, defaultLineHeight);
            }
            Debug.Assert(nodes.Length > 0);
            // now build the corresponding balanced tree
            int height = DocumentLineTree.GetTreeHeight(nodes.Length);

            Debug.WriteLine("HeightTree will have height: " + height);
            root       = BuildTree(nodes, 0, nodes.Length, height);
            root.color = BLACK;
                        #if DEBUG
            CheckProperties();
                        #endif
        }