Beispiel #1
0
        public bool Delete(SortEventArgs <T> aArgs, ref IAVLTreeSortNode <T> aNode, T value)
        {
            if (aNode == aArgs.Tree.Sentinel)
            {
                return(aArgs.Tree.Deleted != null);
            }

            int compare = -aArgs.OnCompare(value, aNode.Value);

            if (compare < 0)
            {
                if (Delete(aArgs, ref (aNode as AVLTreeSortNode <T, Store>).highValueChild, value) == false)
                {
                    return(false);
                }
                (aNode as AVLTreeSortNode <T, Store>).HighValueChild.Weight = -1;
            }
            else
            {
                if (compare == 0)
                {
                    aArgs.Tree.Deleted = aNode;
                }
                if (Delete(aArgs, ref (aNode as AVLTreeSortNode <T, Store>).lowValueChild, value) == false)
                {
                    return(false);
                }
                (aNode as AVLTreeSortNode <T, Store>).LowValueChild.Weight = -1;
            }

            if (aArgs.Tree.Deleted != null)
            {
                aArgs.Tree.DeletedNode = aArgs.Tree.Deleted;
                aArgs.Tree.Deleted     = null;
                aNode        = aNode.LowValueChild;
                aNode.Weight = -1;
                aNode.LowValueChild.Weight = -1;
            }
            else if ((aNode.HighValueChild.Level < aNode.Level - 1) || (aNode.LowValueChild.Level < aNode.Level - 1))
            {
                --aNode.Level;
                if (aNode.LowValueChild.Level > aNode.Level)
                {
                    aNode.LowValueChild.Level = aNode.Level;
                }
                Skew(ref aNode);
                Skew(ref (aNode as AVLTreeSortNode <T, Store>).lowValueChild);
                Skew(ref (aNode.LowValueChild as AVLTreeSortNode <T, Store>).lowValueChild);
                Split(ref aNode);
                Split(ref (aNode as AVLTreeSortNode <T, Store>).highValueChild);
            }

            return(true);
        }
Beispiel #2
0
        public bool InsertNode(SortEventArgs <T> aArgs, ref IAVLTreeSortNode <T> aNode, T value, out ITreeSortNode <T> aNewNode)
        {
            aNewNode = null;
            if (aNode == aArgs.Tree.Sentinel)
            {
                aNode    = CreateNode(value, aArgs.Tree.Sentinel);
                aNewNode = aNode;
                return(true);
            }

            int compare = -aArgs.OnCompare(value, aNode.Value);

            if (compare < 0)
            {
                if (InsertNode(aArgs, ref (aNode as AVLTreeSortNode <T, Store>).highValueChild, value, out aNewNode) == false)
                {
                    return(false);
                }
                (aNode as AVLTreeSortNode <T, Store>).HighValueChild.Weight = -1;
            }
            else if (compare > 0)
            {
                if (InsertNode(aArgs, ref (aNode as AVLTreeSortNode <T, Store>).lowValueChild, value, out aNewNode) == false)
                {
                    return(false);
                }
                (aNode as AVLTreeSortNode <T, Store>).LowValueChild.Weight = -1;
            }
            else
            {
                switch (aArgs.DuplicateHandling)
                {
                case DuplicateHandlingType.CollectDuplicates:
                    aNode.AddValue(value);
                    aNewNode = aNode;
                    return(true);

                case DuplicateHandlingType.ThrowException:
                    throw new DuplicateItemException();

                default:
                    return(false);
                }
                return(false);
            }

            Weight = -1;

            Skew(ref aNode);
            Split(ref aNode);

            return(true);
        }