Beispiel #1
0
def build_tree(node):
    if node.is_terminal():
        return TreeLeaf(get_idx(node.name))
    else:
        children = map(build_tree, node.clades)
        node = TreeNode()
        for child in children:
            node.add_child(child)
        return node
Beispiel #2
0
def build_tree(node):
    if node.is_terminal():
        return TreeLeaf(get_idx(node.name))
    else:
        children = map(build_tree, node.clades)
        node = TreeNode()
        for child in children:
            node.add_child(child)
        return node
Beispiel #3
0
 def test_zigzagLevelOrder(self):
     self.assertEqual(
         [[3], [20, 9], [15, 7]],
         self.s.zigzagLevelOrder(
             TreeNode(
                 3,
                 TreeNode(9),
                 TreeNode(20, TreeNode(15), TreeNode(7)),
             ), ),
     )
Beispiel #4
0
 def test_buildFromInorderPostorder(self):
     self.assertEqual(
         TreeNode(
             3,
             TreeNode(9),
             TreeNode(20, TreeNode(15), TreeNode(7)),
         ),
         self.s.buildFromInorderPostorder(
             [9, 3, 15, 20, 7],
             [9, 15, 7, 20, 3],
         ),
     )
Beispiel #5
0
def make_class_tree(X, y):
    tree = Tree()
    tree.root = TreeNode()
    C = np.unique(y)
    nodes = {c: TreeNode() for c in C}
    for i, c in enumerate(y):
        node = nodes[c]
        leaf = TreeLeaf(i)
        node.add_child(leaf)
    for node in nodes.values():
        tree.root.add_child(node)
    return tree
Beispiel #6
0
 def createNodesLevel_1(self):
     self.node1 = TreeNode('1.0')
     self.root.setChild(self.node1)
     self.node1.setParent(self.root)
     self.node2 = TreeNode('2.0')
     self.root.setChild(self.node2)
     self.node2.setParent(self.root)
     self.node3 = TreeNode('3.0')
     self.root.setChild(self.node3)
     self.node3.setParent(self.root)
     self.node5 = TreeNode('5.0')
     self.root.setChild(self.node5)
     self.node5.setParent(self.root)
     self.node10 = TreeNode('10.0')
     self.root.setChild(self.node10)
     self.node10.setParent(self.root)
Beispiel #7
0
 def createBranch_1_XFEM(self):
     self.node1XFEM = TreeNode('XFEM')
     self.node1.setChild(self.node1XFEM)
     self.node1XFEM.setParent(self.node1)
     self.node1XFEMcp = TreeNode('cp')
     self.node1XFEM.setChild(self.node1XFEMcp)
     self.node1XFEMcp.setParent(self.node1XFEM)
     self.node1XFEMcpLT = TreeNode('LT')
     self.node1XFEMcp.setChild(self.node1XFEMcpLT)
     self.node1XFEMcpLT.setParent(self.node1XFEMcp)
     self.node1XFEMmp = TreeNode('mp')
     self.node1XFEMmp.setParent(self.node1XFEM)
     self.node1XFEM.setChild(self.node1XFEMmp)
     self.node1XFEMmpLR = TreeNode('LR')
     self.node1XFEMmp.setChild(self.node1XFEMmpLR)
     self.node1XFEMmpLR.setParent(self.node1XFEMmp)
Beispiel #8
0
def test_tree_node_init():
    node = TreeNode(2)
    assert node.max_depth == 2
    assert node.feature is None
    assert node.threshold is None
    assert node.predict is None
    assert node.left is None
    assert node.right is None
Beispiel #9
0
    def test_isValidSequence(self):
        t = TreeNode(
            0,
            TreeNode(
                1,
                TreeNode(0, right=TreeNode(1)),
                TreeNode(1, TreeNode(0), TreeNode(0)),
            ),
            TreeNode(0, TreeNode(0)),
        )

        self.assertTrue(self.s.isValidSequence(t, [0, 1, 0, 1]))
        self.assertFalse(self.s.isValidSequence(t, [0, 0, 1]))
        self.assertFalse(self.s.isValidSequence(t, [0, 1, 1]))
 def test_extractDataFromSims_with_nonempty_node(self):
     with patch('dataProcessing.AnalysisNodeData.extractDataFromSimId'
                ) as extrMock:
         with patch('trees.TreeNode.getSuccessfulMembers') as successMock:
             successMock.return_value = [1, 2, 3]
             an = AnalysisNodeData(TreeNode(''), ['K1'], 'areas')
             an.extractDataFromSims()
             successMock.assert_called_once_with()
             calls = [call(1), call(2), call(3)]
             self.assertEqual(calls, extrMock.mock_calls)
Beispiel #11
0
 def createBranch_10_FEM(self):
     self.node10FEM = TreeNode('FEM')
     self.node10.setChild(self.node10FEM)
     self.node10FEM.setParent(self.node10)
     self.node10FEMe = TreeNode('elliptic')
     self.node10FEMe.setParent(self.node10FEM)
     self.node10FEM.setChild(self.node10FEMe)
     self.node10FEMeQF = TreeNode('QF')
     self.node10FEMe.setChild(self.node10FEMeQF)
     self.node10FEMeQF.setParent(self.node10FEMe)
     self.node10FEMeLF = TreeNode('LF')
     self.node10FEMe.setChild(self.node10FEMeLF)
     self.node10FEMeLF.setParent(self.node10FEMe)
Beispiel #12
0
    def split(self, x, y, node):
        if len(np.unique(y)) == 1 or x is None:
            return
        if len(x) < 1:
            pass

        variance = 1
        while variance < 2:
            feature = np.random.randint(len(x.T))
            variance = len(np.unique(x[:, feature]))
        low_bound = x[:, feature].min() + 1e-10  # np.random.uniform interval is half open
        high_bound = x[:, feature].max() - 1e-10
        threshold = np.random.uniform(low_bound, high_bound)

        left = x[:, feature] < threshold
        right = ~left

        node.feature = feature
        node.threshold = threshold

        node.left = TreeNode()
        self.split(x[left], y[left], node.left)
        node.right = TreeNode()
        self.split(x[right], y[right], node.right)
Beispiel #13
0
 def createBranch_5_XFEM(self):
     self.node5XFEM = TreeNode('XFEM')
     self.node5XFEM.setParent(self.node5)
     self.node5.setChild(self.node5XFEM)
     self.node5XFEMcp = TreeNode('cp')
     self.node5XFEMcp.setParent(self.node5XFEM)
     self.node5XFEM.setChild(self.node5XFEMcp)
     self.node5XFEMcpLT = TreeNode('LT')
     self.node5XFEMcp.setChild(self.node5XFEMcpLT)
     self.node5XFEMcpLT.setParent(self.node5XFEMcp)
Beispiel #14
0
 def createBranch_10_XFEM(self):
     self.node10XFEM = TreeNode('XFEM')
     self.node10.setChild(self.node10XFEM)
     self.node10XFEM.setParent(self.node10)
     self.node10XFEMcp = TreeNode('cp')
     self.node10XFEM.setChild(self.node10XFEMcp)
     self.node10XFEMcp.setParent(self.node10XFEM)
     self.node10XFEMcpLR = TreeNode('LR')
     self.node10XFEMcp.setChild(self.node10XFEMcpLR)
     self.node10XFEMcpLR.setParent(self.node10XFEMcp)
Beispiel #15
0
 def createBranch_3_FEM(self):
     self.node3FEM = TreeNode('FEM')
     self.node3.setChild(self.node3FEM)
     self.node3FEM.setParent(self.node3)
     self.node3FEMe = TreeNode('elliptic')
     self.node3FEM.setChild(self.node3FEMe)
     self.node3FEMe.setParent(self.node3FEM)
     self.node3FEMeQR = TreeNode('QR')
     self.node3FEMe.setChild(self.node3FEMeQR)
     self.node3FEMeQR.setParent(self.node3FEMe)
Beispiel #16
0
 def createBranch_2_FEM(self):
     self.node2FEM = TreeNode('FEM')
     self.node2.setChild(self.node2FEM)
     self.node2FEM.setParent(self.node2)
     self.node2FEMs = TreeNode('scale')
     self.node2FEM.setChild(self.node2FEMs)
     self.node2FEMs.setParent(self.node2FEM)
     self.node2FEMsQF = TreeNode('QF')
     self.node2FEMsQF.setParent(self.node2FEMs)
     self.node2FEMs.setChild(self.node2FEMsQF)
Beispiel #17
0
 def createBranch_3_XFEM(self):
     self.node3XFEM = TreeNode('XFEM')
     self.node3.setChild(self.node3XFEM)
     self.node3XFEM.setParent(self.node3)
     self.node3XFEMmp = TreeNode('mp')
     self.node3XFEM.setChild(self.node3XFEMmp)
     self.node3XFEMmp.setParent(self.node3XFEM)
     self.node3XFEMmpLF = TreeNode('LF')
     self.node3XFEMmp.setChild(self.node3XFEMmpLF)
     self.node3XFEMmpLF.setParent(self.node3XFEMmp)
Beispiel #18
0
 def test_preorderTraversal(self):
     self.assertEqual(
         [1, 2, 3],
         self.s.preorderTraversal(
             TreeNode(1, right=TreeNode(2, left=TreeNode(3))), ),
     )
Beispiel #19
0
    def test_isBalanced(self):
        self.assertTrue(
            self.s.isBalanced(
                TreeNode(
                    3,
                    TreeNode(9),
                    TreeNode(20, TreeNode(15), TreeNode(7)),
                ), ))

        self.assertTrue(self.s.isBalanced(TreeNode(1)))

        self.assertFalse(
            self.s.isBalanced(
                TreeNode(
                    1,
                    TreeNode(
                        2,
                        TreeNode(3, TreeNode(4), TreeNode(4)),
                        TreeNode(3),
                    ),
                    TreeNode(2),
                ), ), )

        self.assertFalse(
            self.s.isBalanced(
                TreeNode(1, right=TreeNode(2, right=TreeNode(3))), ), )

        self.assertFalse(
            self.s.isBalanced(
                TreeNode(
                    1,
                    TreeNode(2, left=TreeNode(3, left=TreeNode(4))),
                    TreeNode(2, right=TreeNode(3, right=TreeNode(4))),
                ), ), )
Beispiel #20
0
"""
Given a binary tree, find its maximum depth.

The maximum depth is the number of nodes along the longest path from the root 
node down to the farthest leaf node.
"""

from trees import TreeNode
from trees import BinaryTree


def maximum_depth(node):
    if not node:
        return 0

    return 1 + max(maximum_depth(node.left), maximum_depth(node.right))


if __name__ == '__main__':
    root_node = TreeNode(10)
    root_node.left = TreeNode(12)
    root_node.right = TreeNode(24)
    root_node.left.left = TreeNode(1)
    root_node.left.left.left = TreeNode(1)
    binary_tree = BinaryTree(root_node)

    assert maximum_depth(binary_tree.root) == 4
    print("All test cases passed.")
Beispiel #21
0
    def test_isSubtree(self):
        self.assertTrue(
            self.s.isSubtree(
                TreeNode(3, TreeNode(4, TreeNode(1), TreeNode(2)),
                         TreeNode(5)),
                TreeNode(4, TreeNode(1), TreeNode(2)),
            ))

        self.assertFalse(
            self.s.isSubtree(
                TreeNode(
                    3,
                    TreeNode(4, TreeNode(1), TreeNode(2, left=TreeNode(0))),
                ),
                TreeNode(4, TreeNode(1), TreeNode(2)),
            ))
Beispiel #22
0
from trees.util import plot_tree
from trees import Tree, TreeNode, TreeLeaf
import matplotlib.pyplot as plt

if __name__ == "__main__":
    leaf1 = TreeLeaf(1)
    leaf2 = TreeLeaf(2)
    leaf3 = TreeLeaf(3)
    node1 = TreeNode()
    node1.add_child(leaf1)
    node1.add_child(leaf2)
    node2 = TreeNode()
    node2.add_child(node1)
    node2.add_child(leaf3)
    tree = Tree(node2)

    plot_tree(tree)
    plt.show()

    p = leaf1.detach()
    plot_tree(tree)
    plt.show()

    leaf3.attach(p)
    plot_tree(tree)
    plt.show()
Beispiel #23
0
    def createBranch_1_FEM(self):
        self.node1FEM = TreeNode('FEM')
        self.node1.setChild(self.node1FEM)
        self.node1FEM.setParent(self.node1)
        self.node1FEMs = TreeNode('scale')
        self.node1FEM.setChild(self.node1FEMs)
        self.node1FEMs.setParent(self.node1FEM)
        self.node1FEMsQR = TreeNode('QR')
        self.node1FEMs.setChild(self.node1FEMsQR)
        self.node1FEMsQR.setParent(self.node1FEMs)
        self.node1FEMsLR = TreeNode('LR')
        self.node1FEMs.setChild(self.node1FEMsLR)
        self.node1FEMsLR.setParent(self.node1FEMs)

        self.node1FEMe = TreeNode('elliptic')
        self.node1FEM.setChild(self.node1FEMe)
        self.node1FEMe.setParent(self.node1FEM)
        self.node1FEMeQF = TreeNode('QF')
        self.node1FEMe.setChild(self.node1FEMeQF)
        self.node1FEMeQF.setParent(self.node1FEMe)
        self.node1FEMeLF = TreeNode('LF')
        self.node1FEMe.setChild(self.node1FEMeLF)
        self.node1FEMeLF.setParent(self.node1FEMe)
def convert_bst(root):
    if not root:
        return root

    traverse(root)
    return root


def traverse(node):
    global greater_sum
    if node.left is None and node.right is None:
        node.value += greater_sum
        greater_sum = node.value
        return

    if node.right:
        traverse(node.right)

    node.value += greater_sum
    greater_sum = node.value

    if node.left:
        traverse(node.left)


tree_root = TreeNode(5, TreeNode(2), TreeNode(13))
convert_bst(tree_root)
print(tree_root)
print(tree_root.left)
print(tree_root.right)
Beispiel #25
0
def build_location_tree():
    root = TreeNode('Global')

    usa = TreeNode('USA')
    chl = TreeNode('Chile')

    # Build New jersie tree
    nej = TreeNode('New Jersie')
    nej.add_child(TreeNode('Princeton'))
    nej.add_child(TreeNode('Treton'))
    # Build california tree
    cal = TreeNode('California')
    cal.add_child(TreeNode('San Francisco'))
    cal.add_child(TreeNode('Mountain view'))
    cal.add_child(TreeNode('Palo Alto'))
    # Finish usa tree
    usa.add_child(nej)
    usa.add_child(cal)
    
    # Build Region Metropolitana region
    rm = TreeNode('Region Metropolitana')
    rm.add_child(TreeNode('Santiago'))
    rm.add_child(TreeNode('Maipu'))

    # Build Punta Arenas tree
    mas = TreeNode('Magallanes')
    mas.add_child(TreeNode('Punta Arenas'))
    mas.add_child(TreeNode('Puerto Natales'))

    # Finish Chile Tree
    chl.add_child(rm)
    chl.add_child(mas)

    # Finish all tree
    root.add_child(chl)
    root.add_child(usa)

    return root
Beispiel #26
0
    def test_isSameTree(self):
        self.assertTrue(
            self.s.isSameTree(
                TreeNode(1, TreeNode(2), TreeNode(3)),
                TreeNode(1, TreeNode(2), TreeNode(3)),
            ), )

        self.assertFalse(
            self.s.isSameTree(
                TreeNode(1, left=TreeNode(2)),
                TreeNode(1, right=TreeNode(2)),
            ), )

        self.assertFalse(
            self.s.isSameTree(
                TreeNode(1, TreeNode(2), TreeNode(1)),
                TreeNode(1, TreeNode(1), TreeNode(2)),
            ), )
Beispiel #27
0
class TreeSetUp(unittest.TestCase):
    def setUp(self):
        self.createRootNode()
        self.createNodesLevel_1()
        self.createBranch_1_FEM()
        self.createBranch_1_XFEM()
        self.createBranch_2_FEM()
        self.createBranch_3_FEM()
        self.createBranch_3_XFEM()
        self.createBranch_5_XFEM()
        self.createBranch_10_FEM()
        self.createBranch_10_XFEM()
        self.add_simIds_to_tree_leaf_nodes()

    def createRootNode(self):
        self.root = TreeNode('root')

    def createNodesLevel_1(self):
        self.node1 = TreeNode('1.0')
        self.root.setChild(self.node1)
        self.node1.setParent(self.root)
        self.node2 = TreeNode('2.0')
        self.root.setChild(self.node2)
        self.node2.setParent(self.root)
        self.node3 = TreeNode('3.0')
        self.root.setChild(self.node3)
        self.node3.setParent(self.root)
        self.node5 = TreeNode('5.0')
        self.root.setChild(self.node5)
        self.node5.setParent(self.root)
        self.node10 = TreeNode('10.0')
        self.root.setChild(self.node10)
        self.node10.setParent(self.root)

    def createBranch_1_FEM(self):
        self.node1FEM = TreeNode('FEM')
        self.node1.setChild(self.node1FEM)
        self.node1FEM.setParent(self.node1)
        self.node1FEMs = TreeNode('scale')
        self.node1FEM.setChild(self.node1FEMs)
        self.node1FEMs.setParent(self.node1FEM)
        self.node1FEMsQR = TreeNode('QR')
        self.node1FEMs.setChild(self.node1FEMsQR)
        self.node1FEMsQR.setParent(self.node1FEMs)
        self.node1FEMsLR = TreeNode('LR')
        self.node1FEMs.setChild(self.node1FEMsLR)
        self.node1FEMsLR.setParent(self.node1FEMs)

        self.node1FEMe = TreeNode('elliptic')
        self.node1FEM.setChild(self.node1FEMe)
        self.node1FEMe.setParent(self.node1FEM)
        self.node1FEMeQF = TreeNode('QF')
        self.node1FEMe.setChild(self.node1FEMeQF)
        self.node1FEMeQF.setParent(self.node1FEMe)
        self.node1FEMeLF = TreeNode('LF')
        self.node1FEMe.setChild(self.node1FEMeLF)
        self.node1FEMeLF.setParent(self.node1FEMe)

    def createBranch_1_XFEM(self):
        self.node1XFEM = TreeNode('XFEM')
        self.node1.setChild(self.node1XFEM)
        self.node1XFEM.setParent(self.node1)
        self.node1XFEMcp = TreeNode('cp')
        self.node1XFEM.setChild(self.node1XFEMcp)
        self.node1XFEMcp.setParent(self.node1XFEM)
        self.node1XFEMcpLT = TreeNode('LT')
        self.node1XFEMcp.setChild(self.node1XFEMcpLT)
        self.node1XFEMcpLT.setParent(self.node1XFEMcp)
        self.node1XFEMmp = TreeNode('mp')
        self.node1XFEMmp.setParent(self.node1XFEM)
        self.node1XFEM.setChild(self.node1XFEMmp)
        self.node1XFEMmpLR = TreeNode('LR')
        self.node1XFEMmp.setChild(self.node1XFEMmpLR)
        self.node1XFEMmpLR.setParent(self.node1XFEMmp)

    def createBranch_2_FEM(self):
        self.node2FEM = TreeNode('FEM')
        self.node2.setChild(self.node2FEM)
        self.node2FEM.setParent(self.node2)
        self.node2FEMs = TreeNode('scale')
        self.node2FEM.setChild(self.node2FEMs)
        self.node2FEMs.setParent(self.node2FEM)
        self.node2FEMsQF = TreeNode('QF')
        self.node2FEMsQF.setParent(self.node2FEMs)
        self.node2FEMs.setChild(self.node2FEMsQF)

    def createBranch_3_FEM(self):
        self.node3FEM = TreeNode('FEM')
        self.node3.setChild(self.node3FEM)
        self.node3FEM.setParent(self.node3)
        self.node3FEMe = TreeNode('elliptic')
        self.node3FEM.setChild(self.node3FEMe)
        self.node3FEMe.setParent(self.node3FEM)
        self.node3FEMeQR = TreeNode('QR')
        self.node3FEMe.setChild(self.node3FEMeQR)
        self.node3FEMeQR.setParent(self.node3FEMe)

    def createBranch_3_XFEM(self):
        self.node3XFEM = TreeNode('XFEM')
        self.node3.setChild(self.node3XFEM)
        self.node3XFEM.setParent(self.node3)
        self.node3XFEMmp = TreeNode('mp')
        self.node3XFEM.setChild(self.node3XFEMmp)
        self.node3XFEMmp.setParent(self.node3XFEM)
        self.node3XFEMmpLF = TreeNode('LF')
        self.node3XFEMmp.setChild(self.node3XFEMmpLF)
        self.node3XFEMmpLF.setParent(self.node3XFEMmp)

    def createBranch_5_XFEM(self):
        self.node5XFEM = TreeNode('XFEM')
        self.node5XFEM.setParent(self.node5)
        self.node5.setChild(self.node5XFEM)
        self.node5XFEMcp = TreeNode('cp')
        self.node5XFEMcp.setParent(self.node5XFEM)
        self.node5XFEM.setChild(self.node5XFEMcp)
        self.node5XFEMcpLT = TreeNode('LT')
        self.node5XFEMcp.setChild(self.node5XFEMcpLT)
        self.node5XFEMcpLT.setParent(self.node5XFEMcp)

    def createBranch_10_FEM(self):
        self.node10FEM = TreeNode('FEM')
        self.node10.setChild(self.node10FEM)
        self.node10FEM.setParent(self.node10)
        self.node10FEMe = TreeNode('elliptic')
        self.node10FEMe.setParent(self.node10FEM)
        self.node10FEM.setChild(self.node10FEMe)
        self.node10FEMeQF = TreeNode('QF')
        self.node10FEMe.setChild(self.node10FEMeQF)
        self.node10FEMeQF.setParent(self.node10FEMe)
        self.node10FEMeLF = TreeNode('LF')
        self.node10FEMe.setChild(self.node10FEMeLF)
        self.node10FEMeLF.setParent(self.node10FEMe)

    def createBranch_10_XFEM(self):
        self.node10XFEM = TreeNode('XFEM')
        self.node10.setChild(self.node10XFEM)
        self.node10XFEM.setParent(self.node10)
        self.node10XFEMcp = TreeNode('cp')
        self.node10XFEM.setChild(self.node10XFEMcp)
        self.node10XFEMcp.setParent(self.node10XFEM)
        self.node10XFEMcpLR = TreeNode('LR')
        self.node10XFEMcp.setChild(self.node10XFEMcpLR)
        self.node10XFEMcpLR.setParent(self.node10XFEMcp)

    def add_simIds_to_tree_leaf_nodes(self):
        # branch 1.0
        self.node1FEMsQR.successfulMembers = set(
            ['node1FEMsQR_sm_1', 'node1FEMsQR_sm_2'])
        self.node1FEMsQR.failedMembers = set(['node1FEMsQR_fm_1'])
        self.node1FEMsLR.successfulMembers = set(['node1FEMsLR_sm_1'])
        self.node1FEMsLR.failedMembers = set(['node1FEMsLR_fm_1'])

        self.node1FEMeQF.successfulMembers = set(['node1FEMeQF_sm_1'])
        self.node1FEMeQF.failedMembers = set(
            ['node1FEMeQF_fm_1', 'node1FEMeQF_fm_2'])
        self.node1FEMeLF.successfulMembers = set(
            ['node1FEMeLF_sm_1', 'node1FEMeLF_sm_2', 'node1FEMeLF_sm_3'])
        self.node1FEMeLF.failedMembers = set(['node1FEMeLF_fm_1'])

        self.node1XFEMcpLT.successfulMembers = set(
            ['node1XFEMcpLT_sm_1', 'node1XFEMcpLT_sm_2', 'node1XFEMcpLT_sm_3'])
        self.node1XFEMcpLT.failedMembers = set(['node1XFEMcpLT_fm_1'])
        self.node1XFEMmpLR.successfulMembers = set(
            ['node1XFEMmpLR_sm_1', 'node1XFEMmpLR_sm_2'])
        self.node1XFEMmpLR.failedMembers = set(['node1XFEMmpLR_fm_1'])

        # branch 2.0
        self.node2FEMsQF.successfulMembers = set(
            ['node2FEMsQF_sm_1', 'node2FEMsQF_sm_2'])
        # self.node2FEMsQF.failedMembers  # None
        # branch 3.0
        self.node3FEMeQR.successfulMembers = set(['node3FEMeQR_sm_1'])
        self.node3FEMeQR.failedMembers = set(
            ['node3FEMeQR_fm_1', 'node3FEMeQR_fm_2'])

        self.node3XFEMmpLF.successfulMembers = set(
            ['node3XFEMmpLF_sm_1', 'node3XFEMmpLF_sm_2'])
        self.node3XFEMmpLF.failedMembers = set(
            ['node3XFEMmpLF_fm_1', 'node3XFEMmpLF_fm_2', 'node3XFEMmpLF_fm_3'])
        # branch 5.0
        self.node5XFEMcpLT.successfulMembers = set(
            ['node5XFEMcpLT_sm_1', 'node5XFEMcpLT_sm_2'])
        self.node5XFEMcpLT.failedMembers = set(
            ['node5XFEMcpLT_fm_1', 'node5XFEMcpLT_fm_2', 'node5XFEMcpLT_fm_3'])
        # branch 10.0
        self.node10FEMeQF.successfulMembers = set(
            ['node10FEMeQF_sm_1', 'node10FEMeQF_sm_2'])
        self.node10FEMeQF.failedMembers = set(
            ['node10FEMeQF_fm_1', 'node10FEMeQF_fm_2'])
        self.node10FEMeLF.successfulMembers = set(
            ['node10FEMeLF_sm_1', 'node10FEMeLF_sm_2', 'node10FEMeLF_sm_3'])
        self.node10FEMeLF.failedMembers = set(
            ['node10FEMeLF_fm_1', 'node10FEMeLF_fm_2', 'node10FEMeLF_fm_3'])

        # self.node10XFEMcpLR.successfulMembers = set([])
        self.node10XFEMcpLR.failedMembers = set(
            ['node10XFEMcpLR_fm_1', 'node10XFEMcpLR_fm_2'])
"""

from trees import TreeNode


def sum_of_left_leaves(root):

    if not root:
        return 0

    if root.left and not root.left.left and not root.left.right:
        return root.left.value + sum_of_left_leaves(root.right)

    return sum_of_left_leaves(root.left) + sum_of_left_leaves(root.right)


if __name__ == "__main__":
    root1 = TreeNode(3, TreeNode(9), TreeNode(20, TreeNode(15), TreeNode(7)))
    assert sum_of_left_leaves(root1) == 24

    root2 = TreeNode(3)
    assert sum_of_left_leaves(root2) == 0

    root3 = TreeNode(3, TreeNode(9), TreeNode(20))
    assert sum_of_left_leaves(root3) == 9

    root4 = TreeNode(3, TreeNode(9, TreeNode(10), TreeNode(11)),
                     TreeNode(20, TreeNode(15), TreeNode(17)))

    print "All test cases passed successfully"
def invert_binary_tree(tree):
    if not tree:
        return tree

    node_queue = [tree.root]

    while node_queue:
        current_node = node_queue.pop()
        if current_node.left or current_node.right:
            current_node.left, current_node.right = current_node.right, current_node.left

        if current_node.right:
            node_queue.insert(0, current_node.right)

        if current_node.left:
            node_queue.insert(0, current_node.left)

    return tree


if __name__ == '__main__':
    root = TreeNode(4, TreeNode(2), TreeNode(7))
    root.left.left = TreeNode(1)
    root.left.right = TreeNode(3)
    root.right.left = TreeNode(6)
    root.right.right = TreeNode(9)
    binary_tree = BinaryTree(root)
    print("Before inversion: ", binary_tree)
    inverted_tree = invert_binary_tree(binary_tree)
    print("After inversion: ", inverted_tree)
    if not first_tree:
        return False

    if not second_tree:
        return False

    if first_tree.value == second_tree.value:
        left_is_equal = is_same_tree(first_tree.left, second_tree.left)
        right_is_equal = is_same_tree(first_tree.right, second_tree.right)

        if left_is_equal and right_is_equal:
            return True

    return False


if __name__ == '__main__':
    root1 = TreeNode(1)
    root1.left = TreeNode(2)
    root1.right = TreeNode(3)

    root2 = TreeNode(1)
    root2.left = TreeNode(2)
    root2.right = TreeNode(3)
    assert is_same_tree(root1, root2) == True

    root2.right.right = TreeNode(4)
    assert is_same_tree(root1, root2) == False

    print("All test cases passed.")
Beispiel #31
0
    def test_levelOrderBottom(self):
        self.assertEqual(
            self.s.levelOrderBottom(
                TreeNode(
                    3,
                    TreeNode(9),
                    TreeNode(20, TreeNode(15), TreeNode(7)),
                ), ),
            [[15, 7], [9, 20], [3]],
        )

        self.assertEqual(
            self.s.levelOrderBottom(
                TreeNode(
                    3,
                    TreeNode(9, TreeNode(1), TreeNode(4)),
                    TreeNode(20, TreeNode(15), TreeNode(7)),
                ), ),
            [[1, 4, 15, 7], [9, 20], [3]],
        )