/// <summary>
        /// Goes to the next node in the tree
        /// </summary>
        public void Next()
        {
            if (currentNode.ChildRight != null)
            {
                currentNode = GetMinimum(currentNode.ChildRight);
                return;
            }

            BinarySearchTreeNode <T> y = currentNode.Parent;
            BinarySearchTreeNode <T> x = currentNode;

            while (y != null && x == y.ChildRight)
            {
                x = y;
                y = y.Parent;
            }

            currentNode = y;
        }
        /// <summary>
        /// Goes to the previous element in the tree
        /// </summary>
        public void Previous()
        {
            if (currentNode.ChildLeft != null)
            {
                currentNode = GetMaximum(currentNode.ChildLeft);
                return;
            }

            BinarySearchTreeNode <T> y = currentNode.Parent;;
            BinarySearchTreeNode <T> x = currentNode;

            while (y != null && x == y.ChildLeft)
            {
                x = y;
                y = y.Parent;
            }

            currentNode = y;
        }
Exemple #3
0
        /// <summary>
        /// Recursively searches for a node in the tree
        /// </summary>
        /// <param name="el"></param>
        /// <param name="root"></param>
        /// <returns></returns>
        private BinarySearchTreeNode <T> SearchElement_Rec(T el, BinarySearchTreeNode <T> root)
        {
            BinarySearchTreeNode <T> node = root;

            if (node == null)
            {
                return(null);
            }

            if (el.CompareTo(node.Element) == 0)
            {
                return(node);
            }
            else if (el.CompareTo(node.Element) < 0)
            {
                return(SearchElement_Rec(el, node.ChildLeft));
            }
            else
            {
                return(SearchElement_Rec(el, node.ChildRight));
            }
        }
 /// <summary>
 /// Initializes a new PlebCode.Infrastructure.Collections.BinarySearchTreeIterator<T>
 /// </summary>
 /// <param name="root">The root node</param>
 public BinarySearchTreeIterator(BinarySearchTreeNode <T> root)
 {
     this.currentNode = GetMinimum(root);
 }
Exemple #5
0
        /// <summary>
        /// Removes a element from the tree
        /// </summary>
        /// <param name="el">Element</param>
        public virtual void Remove(T el)
        {
            BinarySearchTreeNode <T> node = SearchElement_Rec(el, root);

            // Has no children
            if (node.ChildLeft == null && node.ChildRight == null)
            {
                if (node.Parent != null)
                {
                    if (node.Parent.ChildLeft == node)
                    {
                        node.Parent.ChildLeft = null;
                    }
                    else
                    {
                        node.Parent.ChildRight = null;
                    }
                }
                node   = null;
                Count -= 1;
            }
            // Has one child (Left)
            else if (node.ChildRight == null)
            {
                if (node.Parent != null)
                {
                    if (node.Parent.ChildLeft == node)
                    {
                        node.ChildLeft.Parent = node.Parent;
                        node.Parent.ChildLeft = node.ChildLeft;
                    }
                    else
                    {
                        node.ChildLeft.Parent  = node.Parent;
                        node.Parent.ChildRight = node.ChildLeft;
                    }
                }
                node   = null;
                Count -= 1;
            }
            // Has one child (Right)
            else if (node.ChildLeft == null)
            {
                if (node.Parent != null)
                {
                    if (node.Parent.ChildRight == node)
                    {
                        node.ChildRight.Parent = node.Parent;
                        node.Parent.ChildRight = node.ChildRight;
                    }
                    else
                    {
                        node.ChildRight.Parent = node.Parent;
                        node.Parent.ChildLeft  = node.ChildRight;
                    }
                }
                node   = null;
                Count -= 1;
            }
            // Has both children
            else
            {
                BinarySearchTreeNode <T> x = node;

                while (x.ChildLeft != null)
                {
                    x = x.ChildLeft;
                }

                node.Element = x.Element;
                BinarySearchTreeNode <T> nodeChild = x.ChildLeft == null ? x.ChildRight : x.ChildLeft;
                if (x.ChildLeft != null)
                {
                    if (x.Parent.ChildLeft == x)
                    {
                        x.Parent.ChildLeft = nodeChild;
                    }
                    else
                    {
                        x.Parent.ChildRight = nodeChild;
                    }
                }
                else
                {
                    if (x.Parent.ChildLeft == x)
                    {
                        x.Parent.ChildLeft = nodeChild;
                    }
                    else
                    {
                        x.Parent.ChildRight = nodeChild;
                    }
                }
                Count -= 1;
            }
        }
Exemple #6
0
 /// <summary>
 /// Initializes a new Binary Search Tre
 /// </summary>
 public BinarySearchTree()
 {
     root  = null;
     Count = 0;
 }
Exemple #7
0
 /// <summary>
 /// Empties the tree
 /// </summary>
 public virtual void Clear()
 {
     root  = null;
     Count = 0;
 }