예제 #1
0
        /// <summary>
        /// Adds an item to the binary search tree
        /// </summary>
        /// <param name="item"></param>
        public void Add(IComparable item)
        {
            Count++;
            if (head == null)
            {
                head = new BinNode(item);
                return;
            }

            BinNode node = head;

            while (node != null)
            {
                int d = node.CompareTo(item);
                if (d > 0)
                {
                    if (node.HasRight())
                    {
                        node = node.right;
                    }
                    else
                    {
                        node.right = new BinNode(item);
                        return;
                    }
                }
                else
                {
                    if (node.HasLeft())
                    {
                        node = node.left;
                    }
                    else
                    {
                        node.left = new BinNode(item);
                        return;
                    }
                }
            }
        }
예제 #2
0
        private BinNode GetNode(IComparable other)
        {
            BinNode node = head;

            while (node != null)
            {
                int d = node.CompareTo(other);
                if (d == 0)
                {
                    break;
                }
                else if (d > 0)
                {
                    node = node.right;
                }
                else
                {
                    node = node.left;
                }
            }
            return(node);
        }