public MerkleProofElement BuildFromBinaryTreeInternal(BinaryTreeElement currentElement, MerkleHashCalculator calculator)
        {
            if (currentElement is EmptyLeaf)
            {
                return(new ProofHashedLeaf(new byte[32]));
            }
            else if (currentElement is Leaf)
            {
                var leafElement = (Leaf)currentElement;
                var pathElem    = currentElement.PathElem;
                if (!(pathElem is null))
                {
                    if (pathElem is PathLeafElement)
                    {
                        return(new ProofValueLeaf(leafElement.Content, pathElem.Previous));
                    }
                    else
                    {
                        throw new System.Exception("The path and structure don't match. We are at a leaf, but path elem is not a leaf: " + pathElem);
                    }
                }
                else
                {
                    var hash = calculator.CalculateLeafHash(leafElement.Content);

                    return(new ProofHashedLeaf(hash));
                }
            }
예제 #2
0
        /// <summary>
        /// Insert element by using of comparator.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="comparator"></param>
        public void InsertElement(ElementType value)
        {
            BinaryTreeElement pointerOfValue;
            BinaryTreeElement newElement = new BinaryTreeElement();

            pointerOfValue = head;

            newElement.Value = value;
            newElement.Right = null;
            newElement.Left  = null;

            if (pointerOfValue == null)
            {
                head = newElement;
                return;
            }

            while (true)
            {
                if (Comparator.Compare(value, pointerOfValue.Value) == 0)
                {
                    newElement = null;
                    return;
                }
                if (Comparator.Compare(value, pointerOfValue.Value) == 1)
                {
                    if (pointerOfValue.Right == null)
                    {
                        pointerOfValue.Right = newElement;
                        return;
                    }

                    pointerOfValue = pointerOfValue.Right;
                }
                else
                {
                    if (pointerOfValue.Left == null)
                    {
                        pointerOfValue.Left = newElement;
                        return;
                    }
                    pointerOfValue = pointerOfValue.Left;
                }
            }
        }
예제 #3
0
 private int MaxLevelInternal(BinaryTreeElement node)
 {
     if (node is EmptyLeaf)
     {
         return(0);
     }
     else if (node is Leaf)
     {
         return(1);
     }
     else if (node is Node)
     {
         var castedNode = (Node)node;
         return(System.Math.Max(this.MaxLevelInternal(castedNode.Left), this.MaxLevelInternal(castedNode.Right)) + 1);
     }
     else
     {
         throw new System.Exception("What is this type? " + node.GetType().ToString());
     }
 }
예제 #4
0
 public SubTreeRootNode(BinaryTreeElement left, BinaryTreeElement right, T content, PathElement pathElem = null) : base(left, right)
 {
     this.Content = content;
     SetPathElement(pathElem);
 }
예제 #5
0
 public Node(BinaryTreeElement left, BinaryTreeElement right)
 {
     this.Left  = left;
     this.Right = right;
 }
예제 #6
0
 public DictHeadNode(BinaryTreeElement left, BinaryTreeElement right, T content, int size, PathElement pathElem = null) : base(left, right, content, pathElem)
 {
     this.Size = size;
 }
예제 #7
0
 public BinaryTree(BinaryTreeElement root)
 {
     this.Root = root;
 }
예제 #8
0
 /// <summary>
 /// Delete tree.
 /// </summary>
 public void Remove()
 {
     head = null;
 }
예제 #9
0
        /// <summary>
        /// Delete element by using of comparator.
        /// </summary>
        /// <param name="value"></param>
        /// <param name="comparator"></param>
        public void RemoveElement(ElementType value)
        {
            if ((Comparator.Compare(value, head.Value) == 0) && (head.Left == null) && (head.Right == null))
            {
                head = null;
                return;
            }
            BinaryTreeElement pointerOfValue;

            pointerOfValue = head;

            BinaryTreeElement pointerOfParent;

            pointerOfParent = head;

            BinaryTreeElement previousPointer;

            previousPointer = head;

            while (Comparator.Compare(value, pointerOfValue.Value) != 0)
            {
                if (Comparator.Compare(value, pointerOfValue.Value) == 1)
                {
                    if (pointerOfValue.Right != null)
                    {
                        pointerOfParent = pointerOfValue;
                        pointerOfValue  = pointerOfValue.Right;
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    if (pointerOfValue.Left != null)
                    {
                        pointerOfParent = pointerOfValue;
                        pointerOfValue  = pointerOfValue.Left;
                    }
                    else
                    {
                        return;
                    }
                }
            }

            if (pointerOfParent == pointerOfValue)
            {
                pointerOfValue = pointerOfValue.Right;
                while (pointerOfValue.Left != null)
                {
                    previousPointer = pointerOfValue;
                    pointerOfValue  = pointerOfValue.Left;
                }
                if (pointerOfValue.Right != null)
                {
                    previousPointer.Left  = pointerOfValue.Right;
                    pointerOfParent.Value = pointerOfValue.Value;
                    pointerOfValue        = null;
                }
                else
                {
                    pointerOfParent.Value = pointerOfValue.Value;
                    pointerOfValue        = null;
                }
                return;
            }

            if (pointerOfParent.Right == pointerOfValue)
            {
                if ((pointerOfValue.Right != null) && (pointerOfValue.Left == null))
                {
                    pointerOfParent.Right = pointerOfValue.Right;
                    pointerOfValue        = null;
                }
                else if ((pointerOfValue.Right == null) && (pointerOfValue.Left != null))
                {
                    pointerOfParent.Right = pointerOfValue.Left;
                    pointerOfValue        = null;
                }
                else if ((pointerOfValue.Right == null) && (pointerOfValue.Left == null))
                {
                    pointerOfValue        = null;
                    pointerOfParent.Right = null;
                }
                else if ((pointerOfValue.Right != null) && (pointerOfValue.Left != null))
                {
                    BinaryTreeElement pointerOfRemoving;
                    pointerOfRemoving = pointerOfValue;
                    pointerOfValue    = pointerOfValue.Right;
                    while (pointerOfValue.Left != null)
                    {
                        pointerOfValue = pointerOfValue.Left;
                    }

                    ElementType tempValue = pointerOfValue.Value;
                    RemoveElement(pointerOfValue.Value);
                    pointerOfRemoving.Value = tempValue;
                }
            }

            if ((pointerOfParent.Left == pointerOfValue) && (pointerOfParent.Left != null))
            {
                if ((pointerOfValue.Right != null) && (pointerOfValue.Left == null))
                {
                    pointerOfParent.Left = pointerOfValue.Right;
                    pointerOfValue       = null;
                }
                else if ((pointerOfValue.Right == null) && (pointerOfValue.Left != null))
                {
                    pointerOfParent.Left = pointerOfValue.Left;
                    pointerOfValue       = null;
                }
                else if ((pointerOfValue.Right == null) && (pointerOfValue.Left == null))
                {
                    pointerOfParent.Left = null;
                    pointerOfValue       = null;
                }
                else if ((pointerOfValue.Right != null) && (pointerOfValue.Left != null))
                {
                    BinaryTreeElement pointerOfRemoving;
                    pointerOfRemoving = pointerOfValue;
                    pointerOfValue    = pointerOfValue.Right;
                    while (pointerOfValue.Left != null)
                    {
                        pointerOfValue = pointerOfValue.Left;
                    }

                    ElementType tempValue = pointerOfValue.Value;
                    RemoveElement(pointerOfValue.Value);
                    pointerOfRemoving.Value = tempValue;
                }
            }
        }