Beispiel #1
0
        public bool Insert(int iKey, Data data)
        {
            if (data == null)
            {
                return false;
            }

            Node n = new Node(iKey,data);

            return Insert(n);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
Beispiel #6
0
 public Node(int iNewKey, Data newData)
 {
     iKey = iNewKey;
     data = newData;
 }
Beispiel #7
0
 public Node(int iNewKey)
 {
     iKey = iNewKey;
     data = null;
 }
Beispiel #8
0
 public Node()
 {
     iKey = 0;
     data = null;
 }
 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);
     }
 }
 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);
     }
 }
        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);
        }
        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);
        }