      "collapsed": false
     },
     "outputs": [],
     "source": [
      "def in_order_traversal(node, visit_func):\n",
      "    if node is not None:\n",
      "        in_order_traversal(node.left, visit_func)\n",
      "        visit_func(node)\n",
      "        in_order_traversal(node.right, visit_func)\n",
      "\n",
      "def pre_order_traversal(node, visit_func):\n",
      "    if node is not None:\n",
      "        visit_func(node)\n",
      "        pre_order_traversal(node.left, visit_func)\n",
      "        pre_order_traversal(node.right, visit_func)\n",
      "\n",
      "def post_order_traversal(node, visit_func):\n",
      "    if node is not None:\n",
      "        post_order_traversal(node.left, visit_func)\n",
      "        post_order_traversal(node.right, visit_func)\n",
      "        visit_func(node)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
 
</s> remove     "def in_order_traversal(node, visit_func):\n",
    "    # TODO: Implement me\n",
    "    pass\n",
 </s> add     "class BstDfs(Bst):\n", </s> remove     "def post_order_traversal(node, visit_func):\n",
    "    # TODO: Implement me\n",
    "    pass"
 </s> add     "    def pre_order_traversal(self, node, visit_func):\n",
    "        # TODO: Implement me\n",
    "        pass\n",
    "\n",
    "    def post_order_traversal(self,node, visit_func):\n",
    "        # TODO: Implement me\n",
    "        pass" </s> remove     "def pre_order_traversal(node, visit_func):\n",
 </s> add     "    def in_order_traversal(self, node, visit_func):\n", </s> remove     "        post_order_traversal(node, self.results.add_result)\n",
 </s> add     "        bst.post_order_traversal(bst.root, self.results.add_result)\n", </s> remove     "        post_order_traversal(node, self.results.add_result)\n",
 </s> add     "        bst.post_order_traversal(bst.root, self.results.add_result)\n",
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
      "collapsed": false
     },
     "outputs": [],
     "source": [
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
    "class BstDfs(Bst):\n",
    "\n",
    "    def in_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.in_order_traversal(node.left, visit_func)\n",
    "            visit_func(node)\n",
    "            self.in_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def pre_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            visit_func(node)\n",
    "            self.pre_order_traversal(node.left, visit_func)\n",
    "            self.pre_order_traversal(node.right, visit_func)\n",
    "\n",
    "    def post_order_traversal(self, node, visit_func):\n",
    "        if node is not None:\n",
    "            self.post_order_traversal(node.left, visit_func)\n",
    "            self.post_order_traversal(node.right, visit_func)\n",
    "            visit_func(node)"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace keep keep keep keep keep
 Move tree dfs to a class 
graphs_trees/tree_dfs/dfs_solution.ipynb