Ejemplo n.º 1
0
        internal static BSTNodeBase <T> FindMax <T>(this BSTNodeBase <T> node) where T : IComparable
        {
            if (node == null)
            {
                return(null);
            }

            while (true)
            {
                if (node.Right == null)
                {
                    return(node);
                }
                node = node.Right;
            }
        }
Ejemplo n.º 2
0
        internal static void UpdateCounts <T>(this BSTNodeBase <T> node, bool spiralUp = false) where T : IComparable
        {
            while (node != null)
            {
                var leftCount  = node.Left?.Count ?? 0;
                var rightCount = node.Right?.Count ?? 0;

                node.Count = leftCount + rightCount + 1;

                node = node.Parent;

                if (!spiralUp)
                {
                    break;
                }
            }
        }
Ejemplo n.º 3
0
        internal static BSTNodeBase <T> NextHigher <T>(this BSTNodeBase <T> node) where T : IComparable
        {
            if (node.Parent == null || node.IsLeftChild)
            {
                if (node.Right != null)
                {
                    node = node.Right;

                    while (node.Left != null)
                    {
                        node = node.Left;
                    }

                    return(node);
                }

                return(node.Parent);
            }

            if (node.Right != null)
            {
                node = node.Right;

                while (node.Left != null)
                {
                    node = node.Left;
                }

                return(node);
            }

            while (node.Parent != null && node.IsRightChild)
            {
                node = node.Parent;
            }

            return(node?.Parent);
        }
Ejemplo n.º 4
0
        public bool MoveNext()
        {
            if (root == null)
            {
                return(false);
            }

            if (current == null)
            {
                current = asc ? root.FindMin() : root.FindMax();
                return(true);
            }

            var next = asc ? current.NextHigher() : current.NextLower();

            if (next != null)
            {
                current = next;
                return(true);
            }

            return(false);
        }
Ejemplo n.º 5
0
        internal static int Position <T>(this BSTNodeBase <T> node, T item) where T : IComparable
        {
            if (node == null)
            {
                return(-1);
            }

            var leftCount = node.Left?.Count ?? 0;

            if (node.Value.CompareTo(item) == 0)
            {
                return(leftCount);
            }

            if (item.CompareTo(node.Value) < 0)
            {
                return(Position(node.Left, item));
            }

            var position = Position(node.Right, item);

            return(position < 0 ? position : position + leftCount + 1);
        }
Ejemplo n.º 6
0
 public void Dispose()
 {
     current = null;
 }
Ejemplo n.º 7
0
 public void Reset()
 {
     current = root;
 }
Ejemplo n.º 8
0
 internal BSTEnumerator(BSTNodeBase <T> root, bool asc = true)
 {
     this.root = root;
     this.asc  = asc;
 }