This notebook was prepared by [Donne Martin](https://github.com/donnemartin). Source and license info is on [GitHub](https://github.com/donnemartin/interactive-coding-challenges).

Solution Notebook

Problem: Implement depth-first searches (in-order, pre-order, post-order traversals) on a binary tree.

Constraints

  • Can we assume we already have a Node class with an insert method?
    • Yes

Test Cases

In-Order Traversal

  • 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8
  • 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5

Pre-Order Traversal

  • 5, 2, 8, 1, 3 -> 5, 2, 1, 3, 8
  • 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5

Post-Order Traversal

  • 5, 2, 8, 1, 3 -> 1, 3, 2, 8, 5
  • 1, 2, 3, 4, 5 -> 5, 4, 3, 2, 1

Algorithm

Test Cases

In-Order Traversal

  • Recursively call in-order traversal on the left child
  • Visit the current node
  • Recursively call in-order traversal on the right child

Complexity:

  • Time: O(n)
  • Space: O(h), where h is the height of the tree

Note:

  • This is a form of a depth-first traversal

Pre-Order Traversal

  • Visit the current node
  • Recursively call pre-order traversal on the left child
  • Recursively call pre-order traversal on the right child

Complexity:

  • Time: O(n)
  • Space: O(h), where h is the height of the tree

Note:

  • This is a form of a depth-first traversal

Post-Order Traversal

  • Recursively call post-order traversal on the left child
  • Recursively call post-order traversal on the right child
  • Visit the current node

Complexity:

  • Time: O(n)
  • Space: O(h), where h is the height of the tree

Note:

  • This is a form of a depth-first traversal

Code


In [1]:
%run ../bst/bst.py

In [2]:
def in_order_traversal(node, visit_func):
    if node is not None:
        in_order_traversal(node.left, visit_func)
        visit_func(node)
        in_order_traversal(node.right, visit_func)

def pre_order_traversal(node, visit_func):
    if node is not None:
        visit_func(node)
        pre_order_traversal(node.left, visit_func)
        pre_order_traversal(node.right, visit_func)

def post_order_traversal(node, visit_func):
    if node is not None:
        post_order_traversal(node.left, visit_func)
        post_order_traversal(node.right, visit_func)
        visit_func(node)

Unit Test


In [3]:
%run ../utils/results.py

In [4]:
%%writefile test_dfs.py
from nose.tools import assert_equal


class TestDfs(object):

    def __init__(self):
        self.results = Results()

    def test_dfs(self):
        node = Node(5)
        insert(node, 2)
        insert(node, 8)
        insert(node, 1)
        insert(node, 3)

        in_order_traversal(node, self.results.add_result)
        assert_equal(str(self.results), "[1, 2, 3, 5, 8]")
        self.results.clear_results()

        pre_order_traversal(node, self.results.add_result)
        assert_equal(str(self.results), "[5, 2, 1, 3, 8]")
        self.results.clear_results()

        post_order_traversal(node, self.results.add_result)
        assert_equal(str(self.results), "[1, 3, 2, 8, 5]")
        self.results.clear_results()

        node = Node(1)
        insert(node, 2)
        insert(node, 3)
        insert(node, 4)
        insert(node, 5)

        in_order_traversal(node, self.results.add_result)
        assert_equal(str(self.results), "[1, 2, 3, 4, 5]")
        self.results.clear_results()

        pre_order_traversal(node, self.results.add_result)
        assert_equal(str(self.results), "[1, 2, 3, 4, 5]")
        self.results.clear_results()

        post_order_traversal(node, self.results.add_result)
        assert_equal(str(self.results), "[5, 4, 3, 2, 1]")

        print('Success: test_dfs')


def main():
    test = TestDfs()
    test.test_dfs()


if __name__ == '__main__':
    main()


Overwriting test_dfs.py

In [5]:
%run -i test_dfs.py


Success: test_dfs