Exemplo n.º 1
0
        public bool Insert(int iKey, Data data)
        {
            if (data == null)
            {
                return false;
            }

            Node n = new Node(iKey,data);

            return Insert(n);
        }
Exemplo n.º 2
0
        public void Test_Find_On_Non_Existant_Key_Fails_For_Single_Node_Tree()
        {
            // Arrange
            BinaryTree bt = new BinaryTree();
            Data data = new Data();
            bt.Insert(17, data);

            // Act
            data = bt.FindNode(23);   // Should be no node to find (23 != 17)...

            // Assert
            Assert.IsNull(data);
        }
Exemplo n.º 3
0
        public void Test_Find_Succeeds_For_Single_Node_Tree()
        {
            // Arrange
            BinaryTree bt = new BinaryTree();
            Data data = new Data();
            bt.Insert(17, data);

            // Act
            data = bt.FindNode(17);   // Should find the single node (17 == 17)...

            // Assert
            Assert.IsNotNull(data);
        }
Exemplo n.º 4
0
        public void Test_Find_Succeeds_When_Searching_A_Depth_Equals_2_Tree()
        {
            // Arrange
            BinaryTree bt = new BinaryTree();
            BuildLeftUnbalancedTree(bt);
            Data data = new Data(17, "Data4");
            bt.Insert(17,data);   // Tree (should have a depth of two, new leaf node.

            // Act
            data = bt.FindNode(17);   // Should find the single node (17 == 17)...

            // Assert
            Assert.IsNotNull(data);
        }
Exemplo n.º 5
0
        public void Test_Delete_On_Single_Node_Tree()
        {
            // Arrange
            BinaryTree bt = new BinaryTree();
            Data data = new Data(17, "Data17");

            bt.Insert(17, data);

            // Act
            bool removed = bt.Remove(17);   // Should find the single node (17 == 17)...

            // Assert
            Assert.IsTrue(removed);
            Assert.IsTrue(bt.TreeDepth() == 0);
        }
Exemplo n.º 6
0
 public Node(int iNewKey, Data newData)
 {
     iKey = iNewKey;
     data = newData;
 }
Exemplo n.º 7
0
 public Node(int iNewKey)
 {
     iKey = iNewKey;
     data = null;
 }
Exemplo n.º 8
0
 public Node()
 {
     iKey = 0;
     data = null;
 }
Exemplo n.º 9
0
 private void BuildSevenNodeBalancedTree(BinaryTree bt)
 {
     // Building a 7-node tree. This as a node with only a left child, a node with only a right child,
     // and nodes that have both left and right children. Tree that looks like this and has a depth of four:
     //     4
     //    /  \
     //   3    9
     //  /    / \
     // 1    7   11
     //           \
     //            15
     int[] iKeys = { 4, 3, 9, 1, 7, 11, 15 };
     string[] dataStrings = { "Data1", "Data2", "Data3", "Data4", "Data5", "Data6", "Data7" };
     for (int i=0; i<7; i++)
     {
         Data data = new Data(iKeys[i], dataStrings[i]);
         bt.Insert(iKeys[i], data);
     }
 }
Exemplo n.º 10
0
 private void BuildRightUnbalancedTree(BinaryTree bt)
 {
     int[] iKeys = { 4, 8, 16 };
     string[] dataStrings = { "Data1", "Data2", "Data3" };
     for (int i = 0; i < 7; i++)
     {
         Data data = new Data(iKeys[i], dataStrings[i]);
         bt.Insert(iKeys[i], data);
     }
 }
Exemplo n.º 11
0
        public void Test_Tree_Depth_For_Single_Node_Tree()
        {
            // Arrange
            BinaryTree b = new BinaryTree();
            Data data = new Data();
            b.Insert(1, data);

            // Act
            int depth = b.TreeDepth();

            // Assert
            Assert.IsTrue(depth == 1);
        }
Exemplo n.º 12
0
        public void Test_TreeIsBalanced2_For_Single_Node_Tree()
        {
            // Arrange
            BinaryTree bt = new BinaryTree();
            Data data = new Data();
            bt.Insert(5, data);

            // Act
            bool balanced = bt.TreeIsBalanced2();

            // Assert
            Assert.IsTrue(balanced);
        }