コード例 #1
0
        private BT.Node <T>?FindNode(T value)
        {
            BT.Node <T> current = this._root !;

            do
            {
                int comparison = COMPARER.Compare(value, current.Value);

                if (comparison == 0) // value == current.value
                {
                    break;
                }

                if (comparison < 0) // value < current.value
                {
                    current = current.Left !;
                }
                else // value > current.value
                {
                    current = current.Right !;
                }
            } while (current != null);

            return(current);
        }
コード例 #2
0
        public void Add(T value)
        {
            this._root = Insert(this._root !, value);

            BT.Node <T> Insert(BT.Node <T>?current, T value)
            {
                if (current == null)
                {
                    return(new BT.Node <T>(value));
                }

                if (COMPARER.Compare(value, current.Value) < 1)
                {
                    current.Left = Insert(current.Left, value);
                }
                else
                {
                    current.Right = Insert(current.Right, value);
                }

                int balance = this.CheckBalance(current.Left, current.Right);

                if (balance > BALANCE_FACTOR) //Left overloaded
                {
                    if (this.CheckBalance(current.Left !.Left, current.Left.Right) > 0)
                    {
                        current = this.RightRotate(current);
                    }
                    else
                    {
                        current.Left = this.LeftRotate(current.Left);
                        current      = this.RightRotate(current);
                    }
                }
コード例 #3
0
        public void Test_Add_Then_Verify_Properties()
        {
            //         30
            //        /  \
            //       20   40
            //      /      \
            //     10       50
            //    /        / \
            //   5      ->45  60
            //  /              \
            // 3               70
            //  \              /
            //   4            65
            IBinarySearchTree <byte> bt = this.CreateFullBinaryTree();

            bt.Add(45);

            BT.Node <byte>?node = bt.Get(45);

            Assert.False(bt.IsEmpty);
            Assert.NotNull(node);
            Assert.NotNull(node !.Parent);
            Assert.NotNull(node !.Parent !.Right);
            Assert.Equal(50, node.Parent !.Value);
            Assert.Null(node.Left);
            Assert.Null(node.Right);
        }
コード例 #4
0
        public void Delete(T value)
        {
            this.ThrowIfEmpty();

            BT.Node <T>?nodeToDelete = this.FindNode(value);

            if (nodeToDelete == null)
            {
                throw new InvalidOperationException();
            }

            this._root = Delete(this._root !, value);

            BT.Node <T>?Delete(BT.Node <T>?current, T value)
            {
                if (current == null)
                {
                    return(null);
                }

                int comparison = COMPARER.Compare(value, current.Value);

                if (comparison < 0)
                {
                    current.Left = Delete(current.Left, value);
                }
                else if (comparison > 0)
                {
                    current.Right = Delete(current.Right, value);
                }
                else
                {
                    if (current.HasAllChildren)
                    {
                        BT.Node <T> temp            = current;
                        BT.Node <T> minNodeForRight = this.FindMinimumElement(temp.Right !);

                        current.Value = minNodeForRight.Value;

                        current.Right = Delete(current.Right, minNodeForRight.Value);
                    }
                    else if (current.Left != null)
                    {
                        current = current.Left;
                    }
                    else if (current.Right != null)
                    {
                        current = current.Right;
                    }
                    else
                    {
                        current = null;
                    }
                }

                return(current);
            }
        }
コード例 #5
0
        private BT.Node <T> FindMinimumElement(BT.Node <T> node)
        {
            if (node.Left == null)
            {
                return(node);
            }

            return(this.FindMinimumElement(node.Left));
        }
コード例 #6
0
        public void Test_Get()
        {
            IBinarySearchTree <byte> bt = this.CreateFullBinaryTree();

            BT.Node <byte> node = bt.Get(3) !;

            Assert.Equal(3, node.Value);
            Assert.NotNull(node.Parent);
            Assert.Null(node.Left);
            Assert.NotNull(node.Right);
            Assert.Equal(4, node.Right !.Value);
        }
コード例 #7
0
        public void Test_Delete_Then_Verify_Properties()
        {
            //     10                  10
            //    /   \               /   \
            //   4     50            4     50
            //  / \   /  \      =>  / \   /  \
            // 3   5 30   65       3   5 30   65
            //      / \   / \           / \   / \ 
            //     20 40 60  70        20 40 60  70
            IBinarySearchTree <byte> bt = this.CreateFullBinaryTree();

            bt.Delete(60);
            bt.Delete(70);

            BT.Node <byte> node = bt.Get(65) !;

            Assert.False(bt.IsEmpty);
            Assert.NotNull(node.Parent);
            Assert.Equal(50, node.Parent !.Value);
            Assert.Null(node.Left !);
            Assert.Null(node.Right !);
        }
コード例 #8
0
        public void Test_Add_Then_Verify_Properties()
        {
            //     10                    10
            //    /   \                 /   \
            //   4     50              4     50
            //  / \   /  \      =>    / \   /  \
            // 3   5 30   65         3   5 30   70
            //      / \   / \       /     / \   / \
            //     20 40 60  70    2     20 40 65  70
            IBinarySearchTree <byte> bt = this.CreateFullBinaryTree();

            bt.Add(2);

            BT.Node <byte> node = bt.Get(2) !;

            Assert.False(bt.IsEmpty);
            Assert.NotNull(node.Parent);
            Assert.NotNull(node.Parent !.Left);
            Assert.Null(node.Left);
            Assert.Null(node.Right);
            Assert.Equal(3, node.Parent !.Value);
        }
コード例 #9
0
        public void Test_Delete_Then_Verify_Properties()
        {
            //       ->30                    40
            //        /  \                  /  \
            //       20   40               20   50
            //      /      \              /      \
            //     10       50    =>     10       60
            //    /          \          /          \
            //   5            60       5            70
            //  /              \      /             /
            // 3               70    3             65
            //  \              /      \
            //   4            65       4
            IBinarySearchTree <byte> bt = this.CreateFullBinaryTree();

            bt.Delete(30);

            BT.Node <byte> node = bt.Get(40) !;

            Assert.Null(node.Parent);
            Assert.Equal(20, node.Left !.Value);
            Assert.Equal(50, node.Right !.Value);
        }
コード例 #10
0
        public IEnumerator <T> GetEnumerator()
        {
            QE.SingleLinkedListBasedQueue <BT.Node <T> > queue = new QE.SingleLinkedListBasedQueue <BT.Node <T> >();

            queue.Enqueue(this._root !);

            while (!queue.IsEmpty)
            {
                BT.Node <T> node = queue.Dequeue();

                yield return(node.Value);

                if (node.Left != null)
                {
                    queue.Enqueue(node.Left);
                }

                if (node.Right != null)
                {
                    queue.Enqueue(node.Right);
                }
            }
        }
コード例 #11
0
        public void Add(T value)
        {
            if (this.IsEmpty)
            {
                this._root = new BT.Node <T>(value);
            }
            else
            {
                BT.Node <T> current = this._root !;

                do
                {
                    if (COMPARER.Compare(value, current.Value) < 1)
                    {
                        if (current.Left == null)
                        {
                            current.Left = new BT.Node <T>(value);

                            break;
                        }

                        current = current.Left;
                    }
                    else
                    {
                        if (current.Right == null)
                        {
                            current.Right = new BT.Node <T>(value);

                            break;
                        }

                        current = current.Right;
                    }
                } while (true);
            }
        }