Skip to content

Commit

Permalink
Add test cases for inorder traversal (keon#473)
Browse files Browse the repository at this point in the history
* Add test cases for inorder traversal

* Fix k_closest_points test cases
  • Loading branch information
bsushmith authored and keon committed Feb 15, 2019
1 parent 7a4de8d commit f4b346c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 32 deletions.
8 changes: 4 additions & 4 deletions tests/test_heap.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ def test_max_sliding_window(self):

def test_k_closest_points(self):
points = [(1, 0), (2, 3), (5, 2), (1, 1), (2, 8), (10, 2), (-1, 0), (-2, -2)]
self.assertEqual([(-1, 0), (1, 0)], k_closest(points, 2))
self.assertEqual([(1, 1), (-1, 0), (1, 0)], k_closest(points, 3))
self.assertEqual([(-2, -2), (1, 1), (1, 0), (-1, 0)], k_closest(points, 4))
self.assertEqual([(-1, 0), (1, 0)], k_closest_points.k_closest(points, 2))
self.assertEqual([(1, 1), (-1, 0), (1, 0)], k_closest_points.k_closest(points, 3))
self.assertEqual([(-2, -2), (1, 1), (1, 0), (-1, 0)], k_closest_points.k_closest(points, 4))
self.assertEqual([(10, 2), (2, 8), (5, 2), (-2, -2), (2, 3),
(1, 0), (-1, 0), (1, 1)], k_closest(points, 8))
(1, 0), (-1, 0), (1, 1)], k_closest_points.k_closest(points, 8))


if __name__ == "__main__":
Expand Down
64 changes: 36 additions & 28 deletions tests/test_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,53 @@
preorder,
preorder_rec,
postorder,
postorder_rec
postorder_rec,
inorder,
inorder_rec
)

import unittest


class Node:

def __init__(self, val, left=None, right=None):
self.val = val
self.left = left
self.right = right


class TestTraversal(unittest.TestCase):

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))

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_postorder(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([25, 75, 50, 125, 175, 150, 100], postorder(n1))
self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder_rec(n1))
tree = create_tree()
self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder(tree))
self.assertEqual([25, 75, 50, 125, 175, 150, 100], postorder_rec(tree))

def test_inorder(self):
tree = create_tree()
self.assertEqual([25, 50, 75, 100, 125, 150, 175], inorder(tree))
self.assertEqual([25, 50, 75, 100, 125, 150, 175], inorder_rec(tree))


def create_tree():
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
return n1


if __name__ == '__main__':
unittest.main()

0 comments on commit f4b346c

Please sign in to comment.