public ITreeSortNode <T> Insert(T aValue)
 {
     if (root == null)
     {
         if (DuplicateHandling == DuplicateHandlingType.CollectDuplicates)
         {
             root = new MultiInstanceAVLTreeSortNode <T> (aValue, Sentinel);
         }
         else
         {
             root = new SingleInstanceAVLTreeSortNode <T> (aValue, Sentinel);
         }
         NodeCount++;
         return(root);
     }
     else
     {
         ITreeSortNode <T> newNode = null;
         if (Root.InsertNode(args, ref root, aValue, out newNode) == true)
         {
             return(newNode);
         }
         return(null);
     }
 }
        private T[] _GetKeyInstances(T aValue)
        {
            ITreeSortNode <T> n = tree.Find(aValue);

            if (n == null)
            {
                return(null);
            }
            return(n.GetInstances());
        }
Exemple #3
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);
        }