def test_preorder(self): n1 = Node(100) n2 = Node(50) n3 = Node(150) n4 = Node(25) n5 = Node(75) n6 = Node(125) n7 = Node(175) n1.left, n1.right = n2, n3 n2.left, n2.right = n4, n5 n3.left, n3.right = n6, n7 self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder(n1)) self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder_rec(n1))
def test_preorder(self): n1 = Node(100) n2 = Node(50) n3 = Node(150) n4 = Node(25) n5 = Node(75) n6 = Node(125) n7 = Node(175) n1.left, n1.right = n2, n3 n2.left, n2.right = n4, n5 n3.left, n3.right = n6, n7 self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder(n1)) self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder_rec(n1))
def test_preorder(self): tree = create_tree() self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder(tree)) self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder_rec(tree))
def test_preorder(self): tree = create_tree() self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder(tree)) self.assertEqual([100, 50, 25, 75, 150, 125, 175], preorder_rec(tree))
from algorithms.tree.traversal import preorder class TreeNode: def __init__(self, val, left=None, right=None): self.val = val self.left = left self.right = right n1 = TreeNode(6) n2 = TreeNode(2) n3 = TreeNode(8) n4 = TreeNode(0) n5 = TreeNode(4) n6 = TreeNode(3) n7 = TreeNode(5) n8 = TreeNode(7) n9 = TreeNode(9) n1.left, n1.right = n2, n3 n2.left, n2.right = n4, n5 n5.left, n5.right = n6, n7 n3.left, n3.right = n8, n9 print(preorder(n1))