public bool MoveForward()
 {
     if (Enumerator.RightChild != null)
     {
         Enumerator = Enumerator.RightChild;
         while (Enumerator.LeftChild != null)
         {
             Enumerator = Enumerator.LeftChild;
         }
     }
     else if (Enumerator.Parent != null)
     {
         while (Enumerator.Parent != null && Enumerator.Parent.Value.CompareTo(Enumerator.Value) < 0)
         {
             Enumerator = Enumerator.Parent;
         }
         Enumerator = Enumerator.Parent;
         if (Enumerator == null)
         {
             Reset();
             return(false);
         }
     }
     else
     {
         Reset();
         return(false);
     }
     return(true);
 }
 public void Reset()
 {
     Enumerator = Root;
     if (Enumerator != null)
     {
         while (Enumerator.LeftChild != null)
         {
             Enumerator = Enumerator.LeftChild;
         }
     }
 }
Пример #3
0
            public void Reset()
            {
                current = new BinaryTreeVertex();
                var first = tree.Root;

                if (first != null)
                {
                    while (first.LeftChild != null)
                    {
                        first = first.LeftChild;
                    }
                }
                current.RightChild = first;
            }
Пример #4
0
        private void Print(IBinaryTreeVertex vertex, int level = 0)
        {
            if (vertex == null)
            {
                return;
            }

            Print(vertex.LeftChild, level + 1);

            for (int i = 0; i < level; ++i)
            {
                Console.Write(".");
            }
            Console.WriteLine(vertex.Value);

            Print(vertex.RightChild, level + 1);
        }
        private IBinaryTreeVertex SearchForVertex(IBinaryTreeVertex vertex)
        {
            var current = Root;

            while (current != null && current.Value.CompareTo(vertex.Value) != 0)
            {
                if (current.Value.CompareTo(vertex.Value) > 0)
                {
                    current = current.LeftChild;
                }
                else if (current.Value.CompareTo(vertex.Value) < 0)
                {
                    current = current.RightChild;
                }
            }
            return(current);
        }
Пример #6
0
 private void DeletionCheck(object sender, DeletionArgs args)
 {
     if (current == args.DeletedVertex)
     {
         if (current.Value.CompareTo(args.NewVertex.Value) > 0)
         {
             current = null;
         }
         else
         {
             current = args.NewVertex;
         }
     }
     if (current == null)
     {
         Reset();
     }
 }
 private bool Add(IBinaryTreeVertex vertex)
 {
     if (Root == null)
     {
         Root = vertex;
     }
     else
     {
         var current = Root;
         while (current.Value.CompareTo(vertex.Value) > 0 && current.LeftChild != null ||
                current.Value.CompareTo(vertex.Value) < 0 && current.RightChild != null)
         {
             if (current.Value.CompareTo(vertex.Value) > 0)
             {
                 current = current.LeftChild;
             }
             else if (current.Value.CompareTo(vertex.Value) < 0)
             {
                 current = current.RightChild;
             }
         }
         if (current.Value.CompareTo(vertex.Value) == 0)
         {
             return(false);
         }
         if (current.Value.CompareTo(vertex.Value) > 0)
         {
             current.LeftChild        = vertex;
             current.LeftChild.Parent = current;
         }
         else if (current.Value.CompareTo(vertex.Value) < 0)
         {
             current.RightChild        = vertex;
             current.RightChild.Parent = current;
         }
     }
     Reset();
     return(true);
 }
Пример #8
0
 public bool MoveNext()
 {
     if (this.IsEmpty())
     {
         return(false);
     }
     if (current.RightChild != null)
     {
         current = current.RightChild;
         while (current.LeftChild != null)
         {
             current = current.LeftChild;
         }
     }
     else if (current.Parent != null)
     {
         while (current.Parent != null && current.Parent.Value.CompareTo(current.Value) < 0)
         {
             current = current.Parent;
         }
         current = current.Parent;
     }
     return(!IsEmpty());
 }
Пример #9
0
 public DeletionArgs(IBinaryTreeVertex deletedVertex, IBinaryTreeVertex newVertex)
 {
     this.DeletedVertex = deletedVertex;
     this.NewVertex     = newVertex;
 }
Пример #10
0
 private bool Search(IBinaryTreeVertex vertex)
 {
     return(SearchForVertex(vertex) != null);
 }
Пример #11
0
        private bool Delete(IBinaryTreeVertex vertex)
        {
            var deleted = SearchForVertex(vertex);
            var current = deleted;

            if (current == null)
            {
                return(false);
            }
            if (current.LeftChild == null && current.RightChild == null)
            {
                current = current.Parent;
                if (current == null)
                {
                    Root = null;
                }
                else if (current.Value.CompareTo(deleted.Value) > 0)
                {
                    current.LeftChild = null;
                }
                else
                {
                    current.RightChild = null;
                    var prevVal = deleted.Value;
                    while (current != Root && current.Value.CompareTo(prevVal) < 0)
                    {
                        prevVal = current.Value;
                        current = current.Parent;
                    }
                }
                Deletion(this, new DeletionArgs(vertex, current));
                return(true);
            }
            if (current.RightChild != null)
            {
                current = current.RightChild;
                if (current.LeftChild != null)
                {
                    while (current.LeftChild != null)
                    {
                        current = current.LeftChild;
                    }
                }
            }
            else
            {
                current = current.LeftChild;
                if (current.RightChild != null)
                {
                    while (current.RightChild != null)
                    {
                        current = current.RightChild;
                    }
                }
            }

            if (current.Parent.Value.CompareTo(current.Value) > 0)
            {
                if (current.LeftChild == null)
                {
                    current.Parent.LeftChild = current.RightChild;
                    if (current.RightChild != null)
                    {
                        current.RightChild.Parent = current.Parent;
                    }
                }
                else
                {
                    current.Parent.LeftChild = current.LeftChild;
                    if (current.LeftChild != null)
                    {
                        current.LeftChild.Parent = current.Parent;
                    }
                }
            }
            else
            {
                if (current.RightChild == null)
                {
                    current.Parent.RightChild = current.LeftChild;
                    if (current.LeftChild != null)
                    {
                        current.LeftChild.Parent = current.Parent;
                    }
                }
                else
                {
                    current.Parent.RightChild = current.RightChild;
                    if (current.RightChild != null)
                    {
                        current.RightChild.Parent = current.Parent;
                    }
                }
            }

            deleted.Value = current.Value;
            Deletion(this, new DeletionArgs(vertex, deleted));

            return(true);
        }
Пример #12
0
 private IBinaryTreeVertex SearchForVertex(IBinaryTreeVertex vertex)
 {
     return(SearchForVertex(vertex.Value));
 }
Пример #13
0
 public BinaryTree(T value)
 {
     this.Root = new BinaryTreeVertex(value);
 }
Пример #14
0
 public BinaryTree()
 {
     this.Root = null;
 }
        private bool Delete(IBinaryTreeVertex vertex)
        {
            var deleted = SearchForVertex(vertex);
            var current = deleted;

            if (current == null)
            {
                return(false);
            }
            if (current.LeftChild == null && current.RightChild == null)
            {
                current = current.Parent;
                if (current == null)
                {
                    Root = null;
                }
                else if (current.Value.CompareTo(deleted.Value) > 0)
                {
                    current.LeftChild = null;
                }
                else
                {
                    current.RightChild = null;
                }
                return(true);
            }
            if (current.RightChild != null)
            {
                current = current.RightChild;
                if (current.LeftChild != null)
                {
                    while (current.LeftChild != null)
                    {
                        current = current.LeftChild;
                    }
                }
            }
            else
            {
                current = current.LeftChild;
                if (current.RightChild != null)
                {
                    while (current.RightChild != null)
                    {
                        current = current.RightChild;
                    }
                }
            }

            if (current.Parent.Value.CompareTo(current.Value) > 0)
            {
                if (current.LeftChild == null)
                {
                    current.Parent.LeftChild = current.RightChild;
                    if (current.RightChild != null)
                    {
                        current.RightChild.Parent = current.Parent;
                    }
                }
                else
                {
                    current.Parent.LeftChild = current.LeftChild;
                    if (current.LeftChild != null)
                    {
                        current.LeftChild.Parent = current.Parent;
                    }
                }
            }
            else
            {
                if (current.RightChild == null)
                {
                    current.Parent.RightChild = current.LeftChild;
                    if (current.LeftChild != null)
                    {
                        current.LeftChild.Parent = current.Parent;
                    }
                }
                else
                {
                    current.Parent.RightChild = current.RightChild;
                    if (current.RightChild != null)
                    {
                        current.RightChild.Parent = current.Parent;
                    }
                }
            }

            Reset();
            deleted.Value = current.Value;

            return(true);
        }
 public BinaryTree()
 {
     this.Root = null;
     Reset();
 }