Example #1
0
        public override string ToString()
        {
            var builder = new StringBuilder();
            BTreeNodeElement <T> start = FirstElement;

            while (start != null)
            {
                builder.Append(start + " - ");
                start = start.Next;
            }
            builder.Append(string.Format("self = {0}, parent = {1}", identifier,
                                         Parent == null ? "NULL" : Parent.Identifier.ToString()));
            builder.AppendLine();
            BTreeNodeElementInterstice <T> startInterstice = FirstInterstice;

            while (startInterstice != null)
            {
                if (!(startInterstice.NodePointer is NullBTreeNode <T>))
                {
                    builder.AppendLine(startInterstice.NodePointer.ToString());
                }
                startInterstice = startInterstice.Next;
            }

            return(builder.ToString());
        }
Example #2
0
        private void InsertInCurrentNode(BTreeNodeElement <T> element)
        {
            if (firstInterstice == null)
            {
                element.PreviousInterval.Smaller = null;
                element.NextInterval.Larger      = null;
                firstInterstice = element.PreviousInterval;
                element.PreviousInterval.Container = element.NextInterval.Container = this;
                return;
            }

            BTreeNodeElementInterstice <T> insertionInterstice = IntersticeToSearch(element.Value);

            if (insertionInterstice.Smaller != null)
            {
                insertionInterstice.Smaller.NextInterval = element.PreviousInterval;
            }
            if (insertionInterstice.Larger != null)
            {
                insertionInterstice.Larger.PreviousInterval = element.NextInterval;
            }
            element.PreviousInterval.Smaller = insertionInterstice.Smaller;
            element.NextInterval.Larger      = insertionInterstice.Larger;

            if (insertionInterstice == firstInterstice)
            {
                firstInterstice = element.PreviousInterval;
            }
        }
Example #3
0
        public void Insert(T value)
        {
            var element = new BTreeNodeElement <T>(value);

            element.PreviousInterval = new BTreeNodeElementInterstice <T>(null, element);
            element.NextInterval     = new BTreeNodeElementInterstice <T>(element, null);

            InternalInsert(element);
        }
Example #4
0
 private bool Equals(BTreeNodeElement <T> other)
 {
     if (ReferenceEquals(null, other))
     {
         return(false);
     }
     if (ReferenceEquals(this, other))
     {
         return(true);
     }
     return(Equals(other.value, value));
 }
Example #5
0
        private BTreeNode <T> DissectedNodeFrom(BTreeNodeElement <T> element)
        {
            BTreeNodeElement <T> start = element;
            var node = new BTreeNode <T>(order, tree);

            while (start != null)
            {
                node.Insert(start);
                start = start.Next;
            }
            return(node);
        }
Example #6
0
        private BTreeNodeElement <T> ElementAt(int index)
        {
            BTreeNodeElement <T> start = FirstElement;
            int i = 0;

            while (i < index)
            {
                start = start.NextInterval.Larger;
                ++i;
            }
            return(start);
        }
Example #7
0
        private void Promote(BTreeNodeElement <T> elementToPromote, BTreeNode <T> smallerNode, BTreeNode <T> largerNode)
        {
            if (Parent == null)
            {
                Parent = new BTreeNode <T>(order, tree);
                tree.RootIs(Parent);
            }

            elementToPromote.NextInterval.Container       = elementToPromote.PreviousInterval.Container = Parent;
            elementToPromote.PreviousInterval.NodePointer = smallerNode;
            elementToPromote.NextInterval.NodePointer     = largerNode;
            Parent.Insert(elementToPromote);
        }
Example #8
0
        private void Insert(BTreeNodeElement <T> element)
        {
            var copiedElement = new BTreeNodeElement <T>(element.Value);

            copiedElement.PreviousInterval = new BTreeNodeElementInterstice <T>(element.PreviousInterval.NodePointer,
                                                                                null, copiedElement);
            copiedElement.NextInterval = new BTreeNodeElementInterstice <T>(element.NextInterval.NodePointer,
                                                                            copiedElement,
                                                                            null);

            copiedElement.PreviousInterval.NodePointer.Parent = copiedElement.NextInterval.NodePointer.Parent = this;
            InternalInsert(copiedElement);
        }
Example #9
0
        public BTreeNodeElement <T> Search(T value)
        {
            BTreeNodeElement <T> element = ElementContaining(value);

            if (element != null)
            {
                return(element);
            }
            BTreeNodeElementInterstice <T> interstice = IntersticeToSearch(value);

            if (interstice == null || interstice.NodePointer is NullBTreeNode <T> )
            {
                return(new NullBTreeNodeElement <T>());
            }
            return(interstice.NodePointer.Search(value));
        }
Example #10
0
        public BTreeNode <T> Find(T value)
        {
            BTreeNodeElement <T> node = ElementContaining(value);

            if (node != null)
            {
                return(this);
            }
            BTreeNodeElementInterstice <T> interstice = IntersticeToSearch(value);

            if (interstice == null || interstice.NodePointer is NullBTreeNode <T> )
            {
                return(this);
            }
            return(interstice.NodePointer.Find(value));
        }
Example #11
0
        private BTreeNodeElement <T> ElementContaining(T value)
        {
            if (firstInterstice == null)
            {
                return(null);
            }
            BTreeNodeElement <T> element = firstInterstice.Larger;

            while (element != null)
            {
                if (element.Value.Equals(value))
                {
                    return(element);
                }
                element = element.Next;
            }
            return(null);
        }
Example #12
0
        private void InternalInsert(BTreeNodeElement <T> element)
        {
            InsertInCurrentNode(element);
            if (Count <= order)
            {
                return;
            }

            int indexOfValueToPromote             = (int)Math.Round((double)Count / 2) - 1;
            BTreeNodeElement <T> elementToPromote = ElementAt(indexOfValueToPromote);

            Console.Out.WriteLine("{0} will be promoted", elementToPromote.Value);
            BTreeNode <T> node = DissectedNodeFrom(elementToPromote.Next);

            if (!(FirstInterstice.NodePointer is NullBTreeNode <T>))
            {
                elementToPromote.Previous.BreakForwardLink();
            }
            elementToPromote.TerminateForwardLink();

            var promotedReplica = new BTreeNodeElement <T>(elementToPromote.Value);

            Promote(promotedReplica, this, node);
        }
Example #13
0
 public BTreeNodeElementInterstice(BTreeNode <T> pointer, BTreeNodeElement <T> smaller, BTreeNodeElement <T> larger)
 {
     Smaller     = smaller;
     Larger      = larger;
     NodePointer = pointer;
 }
Example #14
0
 public BTreeNodeElementInterstice(BTreeNodeElement <T> smaller, BTreeNodeElement <T> larger)
     : this(new NullBTreeNode <T>(), smaller, larger)
 {
 }