示例#1
0
文件: bsttests.py 项目: nchikkam/hk
    def testInOrder(self):
        tree = BinarySearchTree()
        l = [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15]
        for d in l:
            tree.insert(d)

        gen = tree.inOrder()
        l.sort()  # inOrder gives the elements in sorted Order always in BinaryTree
        for i in l:
            self.assertEqual(i, gen.next())
示例#2
0
文件: bsttests.py 项目: nchikkam/hk
    def testMirror(self):
        tree = BinarySearchTree()
        l = [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15]
        for d in l:
            tree.insert(d)

        tree.mirror()
        # when you do a mirror of a BST, the inOrder gives you elements in descending order
        l.sort(reverse=True)
        inOrderGen = tree.inOrder()

        for v in l:
            self.assertEqual(v, inOrderGen.next())
示例#3
0
文件: bsttests.py 项目: nchikkam/hk
    def testSpaceEfficientTraversal(self):
        bstOne = BinarySearchTree()
        l = [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15]
        for d in l:
            bstOne.insert(d)

        bstTwo = bstOne
        clone = bstOne

        genOne = bstTwo.spaceEfficientTraversal()
        genTwo = bstOne.inOrder()
        l.sort()  # inOrder gives the elements in sorted Order always in BinaryTree
        for i in l:
            self.assertEqual(i, genOne.next())
            self.assertEqual(i, genTwo.next())


        #create a copyTree method in the API and use it to test the similarity[Deep Copy]
        self.assertTrue(bstOne.sameTree(clone))