コード例 #1
0
        public void AssertValidTree_InvalidChildren()
        {
            var root = new BinarySearchTreeNode<int>(100);
            root.Left = new BinarySearchTreeNode<int>(150);

            BinarySearchTree<int> bst = new BinarySearchTree<int>();
            bst.Root = root;

            bst.AssertValidTree();
        }
コード例 #2
0
        public void Height_2_RightRight()
        {
            BinarySearchTreeNode<int> root = new BinarySearchTreeNode<int>(100);
            root.Left = new BinarySearchTreeNode<int>(50);
            root.Right = new BinarySearchTreeNode<int>(150);

            root.Right.Right = new BinarySearchTreeNode<int>(160);

            Assert.AreEqual<int>(2, root.Height);
        }
コード例 #3
0
        public void Height_2_FullSecondLevel()
        {
            BinarySearchTreeNode<int> root = new BinarySearchTreeNode<int>(100);
            root.Left = new BinarySearchTreeNode<int>(50);
            root.Right = new BinarySearchTreeNode<int>(150);

            root.Left.Left = new BinarySearchTreeNode<int>(40);
            root.Left.Right = new BinarySearchTreeNode<int>(60);
            root.Right.Left = new BinarySearchTreeNode<int>(110);
            root.Right.Right = new BinarySearchTreeNode<int>(160);

            Assert.AreEqual<int>(2, root.Height);
        }
コード例 #4
0
        public void Height_3_LeftLeftLeft()
        {
            BinarySearchTreeNode<int> root = new BinarySearchTreeNode<int>(100);
            root.Left = new BinarySearchTreeNode<int>(50);
            root.Right = new BinarySearchTreeNode<int>(150);

            root.Left.Left = new BinarySearchTreeNode<int>(40);
            root.Left.Right = new BinarySearchTreeNode<int>(60);
            root.Right.Left = new BinarySearchTreeNode<int>(110);
            root.Right.Right = new BinarySearchTreeNode<int>(160);

            root.Left.Left.Left = new BinarySearchTreeNode<int>(30);

            Assert.AreEqual<int>(3, root.Height);
        }
コード例 #5
0
            public BinarySearchTreeNode <T> RotateRL()
            {
                if (IsRightEmpty())
                {
                    throw new SBTNodeEmptyException(true);
                }
                else if (Left.IsLeftEmpty())
                {
                    throw new SBTNodeEmptyException(false);
                }

                BinarySearchTreeNode <T> pNode = this;
                BinarySearchTreeNode <T> cNode = pNode.Right;

                pNode.ChangeRightSubtree(RotateLL());
                pNode.RotateRR();

                return(cNode);
            }
コード例 #6
0
        public void RebuildBinarySearchTreeFromPreOrderOnValueRange()
        {
            var list = new List <int> {
                43, 23, 37, 29, 31, 41, 47, 53
            };

            var result = Solution.RebuildBinarySearchTreeFromPreOrderOnValueRange(list);

            var I = new BinarySearchTreeNode <int> {
                Data = 43
            };
            var J = new BinarySearchTreeNode <int> {
                Data = 23
            };
            var K = new BinarySearchTreeNode <int> {
                Data = 37
            };
            var L = new BinarySearchTreeNode <int> {
                Data = 29
            };
            var M = new BinarySearchTreeNode <int> {
                Data = 31
            };
            var N = new BinarySearchTreeNode <int> {
                Data = 41
            };
            var O = new BinarySearchTreeNode <int> {
                Data = 47
            };
            var P = new BinarySearchTreeNode <int> {
                Data = 53
            };

            I.Left  = J;
            J.Right = K;
            K.Left  = L;
            L.Right = M;
            K.Right = N;
            I.Right = O;
            O.Right = P;

            result.Should().BeEquivalentTo(I);
        }
コード例 #7
0
            public void Insert(T data)
            {
                BinarySearchTreeNode <T> new_node = new BinarySearchTreeNode <T>(data);

                if (_rtnd == null)
                {
                    _rtnd = new_node; return;
                }
                BinarySearchTreeNode <T> parent = _rtnd;
                U dkey = _gkey(data);
                U pkey = _gkey(parent.Data);

                while (true)
                {
                    if (dkey.CompareTo(pkey) < 0)
                    {
                        if (parent.IsLeftEmpty())
                        {
                            parent.Left = new_node; return;
                        }
                        else
                        {
                            parent = parent.Left; pkey = _gkey(parent.Data);
                        }
                    }
                    else if (dkey.CompareTo(pkey) > 0)
                    {
                        if (parent.IsRightEmpty())
                        {
                            parent.Right = new_node; return;
                        }
                        else
                        {
                            parent = parent.Right; pkey = _gkey(parent.Data);
                        }
                    }
                    else
                    {
                        throw new KeyOverlapException();
                    }
                }
            }
コード例 #8
0
        private void buttonDelete_Click(object sender, EventArgs e)
        {
            if (textBoxInput.Text.Trim().Length == 0)
            {
                return;
            }
            string[] lines = Regex.Split(textBoxInput.Text, ",");
            BinarySearchTreeNode <int> toFocus;

            try
            {
                toFocus = tree.Search(Array.ConvertAll <string, int>(lines, int.Parse).Last());
                if (toFocus.HasParent)
                {
                    selectedNode = toFocus.Parent;
                }

                foreach (int val in Array.ConvertAll <string, int>(lines, int.Parse))
                {
                    if (!tree.Delete(val))
                    {
                        MessageBox.Show(val + " may not be in tree", "Tree prompt");
                    }
                }
            }
            catch (Exception ex) when(
                ex is System.FormatException ||
                ex is System.NullReferenceException)
            {
                MessageBox.Show("Input error", "Please check your input");
            }
            finally
            {
                if (tree.Root == null)
                {
                    selectedNode = null;
                }
                Draw();
                textBoxInput.Clear();
                textBoxInput.Focus();
            }
        }
コード例 #9
0
    public IEnumerator GetEnumerator()
    {
        Stack stack = new Stack();

        stack.Push(this.root);
        while (stack.Count > 0)
        {
            BinarySearchTreeNode tempNode = (BinarySearchTreeNode)stack.Pop();
            yield return(tempNode.value);

            if (tempNode.right != null)
            {
                stack.Push(tempNode.right);
            }
            if (tempNode.left != null)
            {
                stack.Push(tempNode.left);
            }
        }
    }
コード例 #10
0
        public static BinarySearchTreeNode <int> FindFirstGreaterThanK(BinarySearchTreeNode <int> tree, int k)
        {
            var subtree = tree;
            BinarySearchTreeNode <int> firstSoFar = null;

            while (subtree != null)
            {
                if (subtree.Data > k)
                {
                    firstSoFar = subtree;
                    subtree    = subtree.Left;
                }
                else // Root and all keys in left-subtree are <= k, so skip them.
                {
                    subtree = subtree.Right;
                }
            }

            return(firstSoFar);
        }
コード例 #11
0
        private int?FindMaximum(BinarySearchTreeNode Head)
        {
            if (Head == null)
            {
                return(null);
            }
            if (Head.right == null)
            {
                return(Head.val);
            }

            Int64 x = Int64.MinValue;
            int   a = 1;

            if (a > x)
            {
            }

            return(FindMaximum(Head.right));
        }
コード例 #12
0
        public BSTForm()
        {
            InitializeComponent();

            bool Debug = false;

            if (Debug)
            {
                tree.Insert(63);
                tree.Insert(38);
                tree.Insert(97);
                tree.Insert(66);
                tree.Insert(31);
                selectedNode = tree.Root;
            }

            panel1.AutoScroll = true;
            panel1.Controls.Add(pictureBox1);
            pictureBox1.SizeMode = PictureBoxSizeMode.AutoSize;
        }
コード例 #13
0
                public void Insert(T value)
                {
                    if (head == null)
                    {
                        head = new BinarySearchTreeNode(value);
                    }
                    else
                    {
                        BinarySearchTreeNode parent  = null;
                        BinarySearchTreeNode current = head;
                        int compareResult;

                        do
                        {
                            parent        = current;
                            compareResult = current.Value.value.CompareTo(value);
                            if (compareResult > 0)
                            {
                                current = current.FirstChild;
                            }
                            else if (compareResult == 0)
                            {
                                current.Increment();
                                return;
                            }
                            else
                            {
                                current = current.SecondChild;
                            }
                        } while (current != null);

                        if (compareResult > 0)
                        {
                            parent.FirstChild = new BinarySearchTreeNode(value);
                        }
                        else
                        {
                            parent.SecondChild = new BinarySearchTreeNode(value);
                        }
                    }
                }
コード例 #14
0
 public void Remove(int value)
 {
     if (value == Root.Value)
     {
         if (Root.IsLeaf())
         {
             Root = null;
         }
         else if (Root.Left == null && Root.Right != null)
         {
             Root = Root.Right;
         }
         else if (Root.Left != null && Root.Right == null)
         {
             Root = Root.Left;
         }
         else
         {
             if (Root.Left.Right != null)
             {
                 int temp = Root.Left.Right.Value;
                 Root.Left.Remove(temp, Root);
                 Root.Value = temp;
             }
             else
             {
                 int temp = Root.Left.Value;
                 Root.Value = temp;
                 Root.Left.Remove(temp, Root);
             }
         }
     }
     else if (value > Root.Value)
     {
         Root.Right.Remove(value, Root);
     }
     else
     {
         Root.Left.Remove(value, Root);
     }
 }
コード例 #15
0
ファイル: BinarySearchTree.cs プロジェクト: arthurkamsu/DataX
 public bool Insert(T dataToInsert)
 {
     if (Root == null)
     {
         Root = new BinarySearchTreeNode(dataToInsert);
         Count++;
         return(true);
     }
     else
     {
         if (Root.Insert(dataToInsert, AcceptDuplicates))
         {
             Count++;
             return(true);
         }
         else
         {
             return(false);//send a message to the developer to tell him why
         }
     }
 }
コード例 #16
0
ファイル: OnBinarySearchTree.cs プロジェクト: phougatv/dsa
        public static bool IsValidBstNode(BinarySearchTreeNode <int> node)
        {
            if (node == null)
            {
                return(true);
            }
            if (node.Left == null && node.Right == null)
            {
                return(true);
            }
            if (node.Left != null && node.Left.Item > node.Item)
            {
                return(false);
            }
            if (node.Right != null && node.Right.Item < node.Item)
            {
                return(false);
            }

            return(true);
        }
コード例 #17
0
        public static BinarySearchTreeNode <int> AncestorOfTwoNodesIterative(int node1Data, int node2Data, BinarySearchTree <int> bst)
        {
            // Make sure that node1 has the smaller of the 2 nodes
            if (node1Data > node2Data)
            {
                int temp = node2Data;
                node2Data = node1Data;
                node1Data = temp;
            }

            // Make sure that node1 and node2 are present in the Binary Search Tree
            BinarySearchTreeNode <int> node1 = bst.SearchBSTIterative(node1Data);
            BinarySearchTreeNode <int> node2 = bst.SearchBSTIterative(node2Data);

            if (node1 == null || node2 == null)
            {
                return(null);    // The node is not present in the BST
            }
            BinarySearchTreeNode <int> currentNode = bst.Head;

            while (currentNode != null)
            {
                if (currentNode.Data >= node1.Data && currentNode.Data <= node2.Data)
                {
                    // We have found the ancestor node
                    return(currentNode);
                }
                else if (currentNode.Data >= node1.Data && currentNode.Data >= node2.Data)
                {
                    // both the nodes are less than the current node and hence they lie on the left subtree
                    currentNode = currentNode.Left;
                }
                else
                {
                    // both the nodes are on the right subtree
                    currentNode = currentNode.Right;
                }
            }
            return(null);
        }
コード例 #18
0
 public static BinarySearchTreeNode <int> AncestorOfTwoNodesRecursive(BinarySearchTreeNode <int> smaller, BinarySearchTreeNode <int> larger, BinarySearchTreeNode <int> currentNode)
 {
     if (smaller == null || larger == null || currentNode == null)
     {
         return(null);    // Base condition
     }
     if (smaller.Data <= currentNode.Data && larger.Data >= currentNode.Data)
     {
         // We have found the ancestor node
         return(currentNode);
     }
     else if (smaller.Data <= currentNode.Data && larger.Data <= currentNode.Data)
     {
         // both the nodes are less than the current node and hence they lie on the left subtree
         return(AncestorOfTwoNodesRecursive(smaller, larger, currentNode.Left));
     }
     else
     {
         // both the nodes are on the right subtree
         return(AncestorOfTwoNodesRecursive(smaller, larger, currentNode.Right));
     }
 }
コード例 #19
0
        /// <summary>
        /// Performs the action on all nodes of the subtree in a level-order, right-to-left fashion.
        /// </summary>
        private static void LevelOrderRightToLeftVisitor <T>(BinarySearchTreeNode <T> root, Action <BinarySearchTreeNode <T> > action)
        {
            if (root == null)
            {
                return;
            }

            action(root);

            var queue = new Queue <BinarySearchTreeNode <T> >();

            if (root.Right != null)
            {
                action(root.Right);
                queue.Enqueue(root.Right);
            }
            if (root.Left != null)
            {
                action(root.Left);
                queue.Enqueue(root.Left);
            }


            while (queue.Count > 0)
            {
                var curNode = queue.Dequeue();

                if (curNode.Right != null)
                {
                    action(curNode.Right);
                    queue.Enqueue(curNode.Right);
                }
                if (curNode.Left != null)
                {
                    action(curNode.Left);
                    queue.Enqueue(curNode.Left);
                }
            }
        }
コード例 #20
0
        // Input nodes are not null and the key at s is less than or equal to that at b.
        public static BinarySearchTreeNode <int> FindLowestCommonAncestor(BinarySearchTreeNode <int> tree,
                                                                          BinarySearchTreeNode <int> smallKey,
                                                                          BinarySearchTreeNode <int> bigKey)
        {
            var p = tree;

            while (p.Data < smallKey.Data || p.Data > bigKey.Data)
            {
                // Keep searching since p is outside of [s, b].
                while (p.Data < smallKey.Data)
                {
                    p = p.Right; // LCA must be in p’s right child.
                }

                while (p.Data > bigKey.Data)
                {
                    p = p.Left; // LCA must be in p’s left child.
                }
            }

            //Now, s.data >= p.data && p.data <- b.data.
            return(p);
        }
コード例 #21
0
            public BinarySearchTreeNode <T> Search(U targetkey)
            {
                BinarySearchTreeNode <T> parent = _rtnd;
                U pkey;

                while (parent != null)
                {
                    pkey = _gkey(parent.Data);
                    if (targetkey.CompareTo(pkey) < 0)
                    {
                        parent = parent.Left;
                    }
                    else if (targetkey.CompareTo(pkey) > 0)
                    {
                        parent = parent.Right;
                    }
                    else
                    {
                        return(parent);
                    }
                }
                throw new NotFound();
            }
コード例 #22
0
            private void ReplaceInParentNode(BinarySearchTreeNode <T> node, BinarySearchTreeNode <T> newNode)
            {
                if (node.Parent != null)
                {
                    if (node.Parent.Left == node)
                    {
                        node.Parent.Left = newNode;
                    }
                    else
                    {
                        node.Parent.Right = newNode;
                    }
                }
                else
                {
                    Root = newNode;
                }

                if (newNode != null)
                {
                    newNode.Parent = node.Parent;
                }
            }
コード例 #23
0
        private static void RangeLookUpInBinarySearchTreeHelper(BinarySearchTreeNode <int> tree, Interval interval,
                                                                ICollection <int> result)
        {
            if (tree == null)
            {
                return;
            }

            if (interval.Left <= tree.Data && tree.Data <= interval.Right)
            {
                RangeLookUpInBinarySearchTreeHelper(tree.Left, interval, result);
                result.Add(tree.Data);
                RangeLookUpInBinarySearchTreeHelper(tree.Right, interval, result);
            }
            else if (interval.Left > tree.Data)
            {
                RangeLookUpInBinarySearchTreeHelper(tree.Right, interval, result);
            }
            else // interval.right >= tree.data
            {
                RangeLookUpInBinarySearchTreeHelper(tree.Left, interval, result);
            }
        }
コード例 #24
0
 private void Remove(BinarySearchTreeNode <T> node, T data)
 {
     if (node == null)
     {
         throw new ArgumentException($"The node {data} does not exists.");
     }
     else if (data.CompareTo(node.Data) < 0)
     {
         Remove(node.Left, data);
     }
     else if (data.CompareTo(node.Data) > 0)
     {
         Remove(node.Right, data);
     }
     else
     {
         if (node.Left == null && node.Right == null)
         {
             ReplaceInParentNode(node, null);
             Count--;
         }
         else if (node.Right == null)
         {
             ReplaceInParentNode(node, node.Left);
         }
         else if (node.Left == null)
         {
             ReplaceInParentNode(node, node.Right);
         }
         else
         {
             var successor = FindMinimunInSubtree(node.Right);
             node.Data = successor.Data;
             Remove(successor, successor.Data);
         }
     }
 }
コード例 #25
0
    BinarySearchTreeNode Find(T item)
    {
        // binary search
        BinarySearchTreeNode node = _root;

        while (node != null)
        {
            int result = _comparer.Compare(item, node.Value);
            if (result == 0)
            {
                return(node);
            }
            else if (result < 0)
            {
                node = node.LesserChild;
            }
            else
            {
                node = node.GreaterChild;
            }
        }

        return(null);
    }
コード例 #26
0
            public void Add(T data)
            {
                var parent = GetParentForNewNode(data);
                var node   = new BinarySearchTreeNode <T>
                {
                    Data   = data,
                    Parent = parent,
                };

                if (parent == null)
                {
                    Root = node;
                }
                else if (data.CompareTo(parent.Data) < 0)
                {
                    parent.Left = node;
                }
                else
                {
                    parent.Right = node;
                }

                Count++;
            }
コード例 #27
0
ファイル: BinarySearchTree.cs プロジェクト: arthurkamsu/DataX
 public BinarySearchTree(T rootData)
 {
     Root = new BinarySearchTreeNode(rootData); Count = 1;
 }
コード例 #28
0
 public void Insert(int n)
 {
     head = Insert(head, n);
 }
コード例 #29
0
 public BinarySearchTree()
 {
     head = null;
 }
コード例 #30
0
        public void Height_Root()
        {
            BinarySearchTreeNode<int> root = new BinarySearchTreeNode<int>(100);

            Assert.AreEqual<int>(0, root.Height);
        }
コード例 #31
0
            public void Remove(int val, BinarySearchTreeNode Previous)
            {
                bool wentLeft = Previous.Value >= Value;

                if (Value == val)
                {
                    if (IsLeaf())
                    {
                        if (wentLeft)
                        {
                            Previous.Left = null;
                        }
                        else
                        {
                            Previous.Right = null;
                        }
                    }
                    else if (Left != null && Right == null)
                    {
                        if (wentLeft)
                        {
                            Previous.Left = Left;
                        }
                        else
                        {
                            Previous.Right = Left;
                        }
                    }
                    else if (Right != null && Left == null)
                    {
                        if (wentLeft)
                        {
                            Previous.Left = Right;
                        }
                        else
                        {
                            Previous.Right = Right;
                        }
                    }
                    else
                    {
                        if (Left.Right != null)
                        {
                            int temp = Left.Right.Value;
                            Left.Remove(temp, this);
                            Value = temp;
                        }
                        else
                        {
                            int temp = Left.Value;
                            Left.Remove(temp, this);
                            Value = temp;
                        }
                    }
                }
                else if (val > Value)
                {
                    Right.Remove(val, this);
                }
                else
                {
                    Left.Remove(val, this);
                }
            }
コード例 #32
0
 public static void IsRoot(this BinarySearchTreeNode node)
 {
     node.Parent.Should().BeNull();
 }
コード例 #33
0
 public static void IsRightChildOf(this BinarySearchTreeNode child, BinarySearchTreeNode parent)
 {
     parent.Right.Should().Be(child);
     child.Parent.Should().Be(parent);
 }
コード例 #34
0
ファイル: BinarySearchTree.cs プロジェクト: arthurkamsu/DataX
 /*public BinarySearchTreeNode(T data, BinarySearchTreeNode<T> left = null, BinarySearchTreeNode<T> right = null)
  * {
  *  Data = data;
  *  Left = left;
  *  Right = right;
  *  //We have to check if the node is BST Node
  * }*/
 public BinarySearchTreeNode(T data)
 {
     Data = data;
     Left = Right = null;
 }
コード例 #35
0
        public void ToString_RootRight()
        {
            BinarySearchTreeNode<int> node = new BinarySearchTreeNode<int>(50)
            {
                Right = new BinarySearchTreeNode<int>(75),
            };

            Assert.AreEqual<string>("50; Left=null; Right=75", node.ToString());
        }
コード例 #36
0
        public void InOrderPredecessor_RootLeftRight()
        {
            BinarySearchTreeNode<int> rootLeftRight = new BinarySearchTreeNode<int>(50)
            {
                Left = new BinarySearchTreeNode<int>(25)
                {
                    Right = new BinarySearchTreeNode<int>(30)
                }
            };

            Assert.AreEqual<int>(30, rootLeftRight.InOrderPredecessor.Value);
        }
コード例 #37
0
        public void InOrderSuccessor_RootRightRight()
        {
            BinarySearchTreeNode<int> rootRightRight = new BinarySearchTreeNode<int>(50)
            {
                Right = new BinarySearchTreeNode<int>(75)
                {
                    Right = new BinarySearchTreeNode<int>(100)
                }
            };

            Assert.AreEqual<int>(75, rootRightRight.InOrderSuccessor.Value);
        }
コード例 #38
0
        public void InOrderPredecessor_RootRight()
        {
            BinarySearchTreeNode<int> rootRight = new BinarySearchTreeNode<int>(50)
            {
                Right = new BinarySearchTreeNode<int>(75)
            };

            Assert.IsNull(rootRight.InOrderPredecessor);
        }
コード例 #39
0
 public void InOrderSuccessor_Leaf()
 {
     BinarySearchTreeNode<int> leaf = new BinarySearchTreeNode<int>(50);
     Assert.IsNull(leaf.InOrderSuccessor);
 }
コード例 #40
0
        public void InOrderSuccessor_RootLeft()
        {
            BinarySearchTreeNode<int> rootLeft = new BinarySearchTreeNode<int>(50)
            {
                Left = new BinarySearchTreeNode<int>(25)
            };

            Assert.IsNull(rootLeft.InOrderSuccessor);
        }
コード例 #41
0
 public BinarySearchTreeNode(BinarySearchTreeNode <T> left, BinarySearchTreeNode <T> right)
 {
     _left  = left;
     _right = right;
 }
コード例 #42
0
        public void Height_RootWithRightChild()
        {
            BinarySearchTreeNode<int> root = new BinarySearchTreeNode<int>(100);
            root.Right = new BinarySearchTreeNode<int>(150);

            Assert.AreEqual<int>(1, root.Height);
        }