Exemplo n.º 1
0
    def todoTestIsBSTNegativeCase(self):
        leveOrder = [8, 4, 2, 1, 3, 6, 5, 7, 12, 10, 9, 11, 14, 13, 15]
        bt = BinaryTree()
        for d in levelOrder:
            bt.inSertInLevelOrder(d)

        self.assertFalse(bt)
Exemplo n.º 2
0
    def testMaxLengthPathEitherLeftOnlyOrRightOnlyConcentratedSearch(self):
        def solution(T):
            def get_left_depth(N):  # O(log N)
                """just finds out the left height"""
                if N:
                    if N.left:
                        l = get_left_depth(N.left)
                        return l + 1
                    return 0
                return 0

            def get_right_depth(N):  # O(log N)
                """just finds out the right height"""
                if N:
                    if N.right:
                        r = get_right_depth(N.right)
                        return r + 1
                    return 0
                return 0

            lengths = {}  # container to store the path lengths

            # standard bfs algorithm O(N)
            def bfsTraversal(N):
                q = []  # queue to store the nodes in BFS Order
                index = 0
                if N.left == None and N.right == None:
                    return
                q.append(N)
                while len(q) > 0:
                    current = q.pop(0)
                    lengths[index] = max(get_left_depth(current.left), get_right_depth(current.right)) + 1
                    index += 1
                    if current.left:
                        q.append(current.left)
                    if current.right:
                        q.append(current.right)

            bfsTraversal(T)
            sol = -1
            for k in lengths:  # O(N)
                if sol < int(lengths[k]):
                    sol = int(lengths[k])

            return sol

        # it doesn't matter if the tree is BT or BST
        """
                   5
                /    \
               3      10
             /  \     /
            20   21  1
        """
        l = [5, 3, 10, 20, 21, 1, None]
        bt = BinaryTree()
        bt.create(l)
        self.assertEqual(2, solution(bt))
Exemplo n.º 3
0
 def testRightOnlyDepthOfANode(self):
     l = [8, 4, 4, 1, 3, None, 1, None, None, None, None, None, None]
     """
               8
             /  \
            4    4
          /  \    \
         1   3     1
     """
     bt = BinaryTree()
     bt.create(l)
     self.assertEqual(bt.getRightDepth(), 2)
     self.assertEqual(bt.left.getRightDepth(), 1)
Exemplo n.º 4
0
    def testSum(self):
        l = [-15, 5, 6, -8, 1, 3, 9, 2, 6, None, None, None, None, 4, 0, None, None, None, None, None, None, None, -1, 10, None]
        s = 0
        for v in l:
            if v: s += v

        bt = BinaryTree()
        bt.create(l)

        self.assertEqual(bt.sum(), s)

        bstTwo = BinarySearchTree()
        l = [8, 2, 1, 4, 5, 3, 9, 10, 15, 13, 12, 14, 18]
        for v in l:
            bstTwo.insert(v)

        self.assertEqual(bstTwo.sum(), sum(l))
Exemplo n.º 5
0
    def testBinaryTreeCreate(self):
        l = [-15, 5, 6, -8, 1, 3, 9, 2, 6, None, None, None, None, 4, 0, None, None, None, None, None, None, None, -1, 10, None]
        """
                  -15
                 /    \
                5      6
              /  \    / \
            -8    1  3   9
           /  \        /  \
         2     6      4    6
                            \
                            -1
                           /
                         10

        """
        bt = BinaryTree()
        bt.create(l)

        expectedInorder = [2, -8, 6, 5, 1, -15, 3, 6, 4, 9, 0, 10, -1]
        g = bt.inOrder()
        for v in expectedInorder:
            self.assertEqual(v, g.next())
Exemplo n.º 6
0
    def testBSTSymmetricity(self):

        # Single node
        l = [9]
        bt = BinaryTree()
        bt.create(l)

        # case-1:
        l = [8, 4, 4, 1, 3, 3, 1]
        """
                  8     -> This Binary Tree is Symmetric
                /  \
               4    4
             /  \  / \
           1    3 3   1
        """
        bt = BinaryTree()
        bt.create(l)

        self.assertTrue(bt.isSymmetric(True))



        self.assertTrue(bt.isSymmetric(True))

        l = [8, 4, 4, None, 3, 3, None]
        """
                  8
                /  \
               4    4
                \  /
                3 3
        """
        bt = BinaryTree()
        bt.create(l)

        self.assertTrue(bt.isSymmetric(True))

        l = [8, 4, 4, None, 3, 3, 1, None, None, None, None, None, None]
        """
                  8
                /  \
               4    4
                \  / \
                3 3   1
        """
        bt = BinaryTree()
        bt.create(l)

        self.assertFalse(bt.isSymmetric())

        # case-2:
        l = [8, 4, 4, 1, None, None, 1, None, None, None, None]
        """
                  8
                /  \
               4    4
             /       \
            1         1
        """
        bt = BinaryTree()
        bt.create(l)

        self.assertTrue(bt.isSymmetric(True))

        # case-2.1:
        l = [8, 4, 4, 1, 3, None, 1, None, None, None, None, None, None]
        """
                  8
                /  \
               4    4
             /  \    \
            1   3     1
        """
        bt = BinaryTree()
        bt.create(l)

        self.assertFalse(bt.isSymmetric(True))

        # case-3
        l = [8, 4, 4, 2, 2, 2, 2, -2, 9, 9, -2, -2, 9, 9, -2, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None, None]
        """
                     8     -> This Binary Tree is Symmetric
                 /      \
               4          4
             /  \        /  \
           2     2      2    2
         /  \   / \    / \   / \
       -2   9  9  -2  -2  9 9  -2
        """
        bt = BinaryTree()
        bt.create(l)

        self.assertTrue(bt.isSymmetric(True))

        # compare just the structure and exclude data from the comparison
        l = [8, 4, 5, 4, 9, 8, 7]
        """
                  8     -> This Binary Tree is Symmetric
                /  \
               4    5
             /  \  / \
           4    9 8   7
        """
        bt = BinaryTree()
        bt.create(l)

        self.assertTrue(bt.isSymmetric(False)) # don't compare data, but structure ;)
Exemplo n.º 7
0
    def testBTGetMaxPathSum(self):
        l = [-15, 5, 6, -8, 1, 3, 9, 2, 6, None, None, None, None, 4, 0, None, None, None, None, None, None, None, -1, 10, None]
        bt = BinaryTree()
        bt.create(l)

        self.assertEqual(bt.getMaxPathSum()[1], 27)