예제 #1
0
        /// <summary>
        /// Add a section in this timetable if it fits
        /// </summary>
        /// <param name="section">A section</param>
        /// <returns>True if the section fits and has been added to the timetable; else false</returns>
        public bool AddSection(Section section)
        {
            // Check if it can fit
            if (!DoesSectionFit(section))
            {
                return(false);
            }

            foreach (Session session in section.Sessions)
            {
                collection.Add(session);
            }

            sections.Add(section);
            return(true);
        }
        public bool Add(T newItem)
        {
            // If it is an empty tree
            if (IsEmpty)
            {
                content   = newItem;
                color     = "Black";
                leftTree  = new RedBlackTree <T>();
                rightTree = new RedBlackTree <T>();
                return(true);
            }

            // Left tree
            if (newItem.CompareTo(content) < 0)
            {
                if (leftTree.IsEmpty)
                {
                    leftTree        = new RedBlackTree <T>(newItem);
                    leftTree.parent = this;
                    leftTree.color  = "Red";
                    RebalanceTree(leftTree);
                    return(true);
                }
                else
                {
                    return(leftTree.Add(newItem));
                }
            }

            // Right tree
            else if (newItem.CompareTo(content) > 0)
            {
                if (rightTree.IsEmpty)
                {
                    rightTree        = new RedBlackTree <T>(newItem);
                    rightTree.parent = this;
                    rightTree.color  = "Red";
                    RebalanceTree(rightTree);
                    return(true);
                }
                else
                {
                    return(rightTree.Add(newItem));
                }
            }
            return(false);
        }