Ejemplo n.º 1
0
        public void DeleteTest()
        {
            //Arrange
            var bst = new BinarySearchTree<int, string>();
            bst.Add(5, "A");
            bst.Add(3, "B");
            bst.Add(6, "C");
            bst.Add(2, "D");
            bst.Add(7, "E");
            bst.Add(4, "F");
            //Act
            bst.Delete(3);
            var testResult = bst.GetNodeByKey(5).LeftNode;

            //Assert
            Assert.AreEqual(testResult.Key, 4);
        }
 public static void Delete(BinarySearchTreeNode node)
 {
     BinarySearchTree.Delete(node);
     Rebalance(node);
 }
Ejemplo n.º 3
0
        public void LevelOrderTraversalAfterRemovingTest()
        {
            //Arrange
            var bst = new BinarySearchTree<int, string>();

            //88 48 91 26 55 11 43 65 12 57 23 62
            var testData = new[]
            {
                new KeyValuePair<int, string>(88, "88"),
                new KeyValuePair<int, string>(48, "48"),
                new KeyValuePair<int, string>(91, "91"),
                new KeyValuePair<int, string>(26, "26"),
                new KeyValuePair<int, string>(55, "55"),
                new KeyValuePair<int, string>(11, "11"),
                new KeyValuePair<int, string>(43, "43"),
                new KeyValuePair<int, string>(65, "65"),
                new KeyValuePair<int, string>(12, "12"),
                new KeyValuePair<int, string>(57, "57"),
                new KeyValuePair<int, string>(23, "23"),
                new KeyValuePair<int, string>(62, "62"),
            };

            //Act
            testData.All(t =>
            {
                bst.Add(t.Key, t.Value);
                return true;
            });
            var result = bst.LevelOrderTraversal();

            //Assert
            Assert.AreEqual(result.Count(), 12);

            Assert.AreEqual(result.ElementAt(0).Key, 88);
            Assert.AreEqual(result.ElementAt(1).Key, 48);
            Assert.AreEqual(result.ElementAt(2).Key, 91);
            Assert.AreEqual(result.ElementAt(3).Key, 26);
            Assert.AreEqual(result.ElementAt(4).Key, 55);
            Assert.AreEqual(result.ElementAt(5).Key, 11);
            Assert.AreEqual(result.ElementAt(6).Key, 43);
            Assert.AreEqual(result.ElementAt(7).Key, 65);
            Assert.AreEqual(result.ElementAt(8).Key, 12);
            Assert.AreEqual(result.ElementAt(9).Key, 57);
            Assert.AreEqual(result.ElementAt(10).Key, 23);
            Assert.AreEqual(result.ElementAt(11).Key, 62);

            //91 12 48
            bst.Delete(91);
            bst.Delete(12);
            bst.Delete(48);
            result = bst.LevelOrderTraversal();

            //Assert
            Assert.AreEqual(result.Count(), 9);

            Assert.AreEqual(result.ElementAt(0).Key, 88);
            Assert.AreEqual(result.ElementAt(1).Key, 55);
            Assert.AreEqual(result.ElementAt(2).Key, 26);
            Assert.AreEqual(result.ElementAt(3).Key, 65);
            Assert.AreEqual(result.ElementAt(4).Key, 11);
            Assert.AreEqual(result.ElementAt(5).Key, 43);
            Assert.AreEqual(result.ElementAt(6).Key, 57);
            Assert.AreEqual(result.ElementAt(7).Key, 23);
            Assert.AreEqual(result.ElementAt(8).Key, 62);
        }
Ejemplo n.º 4
0
 private static BinarySearchTreeNode Delete(BinarySearchTreeNode node)
 {
     Splay(BinarySearchTree.Next(node));
     Splay(node);
     return(BinarySearchTree.Delete(node));
 }