1

Having issues in running a test case for python Binary Search Tree Traversal. It gives an error as:

======================================================================
ERROR: test_case_1 (__main__.TestProgram)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "BST_traversal.py", line 31, in test_case_1
    self.assertEqual(root.inOrderTraversal(root,[]),inOrder)
TypeError: inOrderTraversal() takes exactly 2 arguments (3 given)

----------------------------------------------------------------------
Ran 1 test in 0.000s

FAILED (errors=1)

The code for BST is:

import unittest

class BST:
    def __init__(self,value):
        self.value = value
        self.left = None
        self.right = None

    def inOrderTraversal(self,array):
        if self is not None:
            inOrderTraversal(self.left,array)
            array.append(self.value)
            inOrderTraversal(self.right,array)
        return array

class TestProgram(unittest.TestCase):
    def test_case_1(self):
        root = BST(10)
        
        root.left = BST(5)
        root.left.left = BST(2)
        root.left.left.left = BST(1)
        root.left.right = BST(5)
        root.right = BST(15)
        root.right.right = BST(22)

        inOrder = [1, 2, 5, 5, 10, 15, 22]
        preOrder = [10, 5, 2, 1, 5, 15, 22]
        postOrder = [1, 2, 5, 5, 22, 15, 10]

        self.assertEqual(root.inOrderTraversal(root,[]),inOrder)

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

Even after making a seperate class for BST does not help me out too much.

Agnel Amodia
  • 765
  • 8
  • 18
  • Python's [`self` argument is implicit](https://stackoverflow.com/questions/7721920/when-do-you-use-self-in-python). You will need to make major adjustments to your implementation (either create separate standalone functions, or modify the `inOrderTraversal` to take in additional argument(s)). – metatoaster Jan 21 '21 at 00:52

2 Answers2

2

self is a hidden param, you not need to given

import unittest


class BST:
    def __init__(self, value):
        self.value = value
        self.left = None
        self.right = None

    def inOrderTraversal(self, data, array):
        if data is not None:
            data.inOrderTraversal(data.left, array)
            array.append(data.value)
            data.inOrderTraversal(data.right, array)
        return array


class TestProgram(unittest.TestCase):
    def test_case_1(self):
        root = BST(10)

        root.left = BST(5)
        root.left.left = BST(2)
        root.left.left.left = BST(1)
        root.left.right = BST(5)
        root.right = BST(15)
        root.right.right = BST(22)

        inOrder = [1, 2, 5, 5, 10, 15, 22]
        preOrder = [10, 5, 2, 1, 5, 15, 22]
        postOrder = [1, 2, 5, 5, 22, 15, 10]
        arr = root.inOrderTraversal(root, [])
        print(arr)
        self.assertEqual(arr, inOrder)


if __name__ == '__main__':
    unittest.main()
.
[1, 2, 5, 5, 10, 15, 22]
Para
  • 1,299
  • 4
  • 11
2
import unittest

class BST:
    def __init__(self,value):
        self.value = value
        self.left = None
        self.right = None

    def inOrderTraversal(self,array):
        if self and self.left:
            self.left.inOrderTraversal(array)
        if self:
            array.append(self.value)
        if self and self.right:
            self.right.inOrderTraversal(array)
        return array

class TestProgram(unittest.TestCase):
    def test_case_1(self):
        root = BST(10)

        root.left = BST(5)
        root.left.left = BST(2)
        root.left.left.left = BST(1)
        root.left.right = BST(5)
        root.right = BST(15)
        root.right.right = BST(22)

        inOrder = [1, 2, 5, 5, 10, 15, 22]
        preOrder = [10, 5, 2, 1, 5, 15, 22]
        postOrder = [1, 2, 5, 5, 22, 15, 10]

        self.assertEqual(root.inOrderTraversal([]),inOrder)

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

That should run. You are getting very close, just need to understand how the self parameter works. Also is not None is redundant, FYI.

Daniel Deng
  • 272
  • 1
  • 7