  
  class TestBfs(object):
  
      def test_bfs(self):
          root = Node(5)
          root.insert(2)
          root.insert(8)
          root.insert(1)
          root.insert(3)
  
          with captured_output() as (out, err):
              root.bfs(sys.stdout.write)
              assert_equal(out.getvalue().strip(), '52813')
  
 
</s> remove             root.bfs(sys.stdout.write)
 </s> add             bfs(node, sys.stdout.write) </s> remove     "        root = Node(5)\n",
    "        root.insert(2)\n",
    "        root.insert(8)\n",
    "        root.insert(1)\n",
    "        root.insert(3)\n",
 </s> add     "        node = Node(5)\n",
    "        insert(node, 2)\n",
    "        insert(node, 8)\n",
    "        insert(node, 1)\n",
    "        insert(node, 3)\n", </s> remove     "            root.bfs(sys.stdout.write)\n",
 </s> add     "            bfs(node, sys.stdout.write)\n", </s> remove     "class Node(object):\n",
    "\n",
    "    def __init__(self, data):\n",
    "        self.data = data\n",
    "        self.left = None\n",
    "        self.right = None\n",
    "\n",
    "    def insert(self, data):\n",
    "        if data <= self.data:\n",
    "            if self.left is None:\n",
    "                self.left = Node(data)\n",
    "            else:\n",
    "                self.left.insert(data)\n",
    "        else:\n",
    "            if self.right is None:\n",
    "                self.right = Node(data)\n",
    "            else:\n",
    "                self.right.insert(data)\n",
    "\n",
    "    def bfs(self, visit_func):\n",
    "        # TODO: Implement me"
 </s> add     "def bfs(self, visit_func):\n",
    "    # TODO: Implement me" </s> remove     "class Node(object):\n",
    "\n",
    "    def __init__(self, data):\n",
    "        self.data = data\n",
    "        self.left = None\n",
    "        self.right = None\n",
    "\n",
    "    def insert(self, data):\n",
    "        if data <= self.data:\n",
    "            if self.left is None:\n",
    "                self.left = Node(data)\n",
    "            else:\n",
    "                self.left.insert(data)\n",
    "        else:\n",
    "            if self.right is None:\n",
    "                self.right = Node(data)\n",
    "            else:\n",
    "                self.right.insert(data)\n",
    "\n",
    "    def bfs(self, visit_func):\n",
    "        queue = deque()\n",
    "        queue.append(self)\n",
    "        while len(queue) > 0:\n",
    "            node = queue.popleft()\n",
    "            visit_func(node.data)\n",
    "            if node.left is not None:\n",
    "                queue.append(node.left)\n",
    "            if node.right is not None:\n",
    "                queue.append(node.right)"
 </s> add     "def bfs(root, visit_func):\n",
    "    queue = deque()\n",
    "    queue.append(root)\n",
    "    while len(queue) > 0:\n",
    "        node = queue.popleft()\n",
    "        visit_func(node.data)\n",
    "        if node.left is not None:\n",
    "            queue.append(node.left)\n",
    "        if node.right is not None:\n",
    "            queue.append(node.right)"
        node = Node(5)
        insert(node, 2)
        insert(node, 8)
        insert(node, 1)
        insert(node, 3)
  
  class TestBfs(object):
  
      def test_bfs(self):
        node = Node(5)
        insert(node, 2)
        insert(node, 8)
        insert(node, 1)
        insert(node, 3)
        node = Node(5)
        insert(node, 2)
        insert(node, 8)
        insert(node, 1)
        insert(node, 3)
        node = Node(5)
        insert(node, 2)
        insert(node, 8)
        insert(node, 1)
        insert(node, 3)
        node = Node(5)
        insert(node, 2)
        insert(node, 8)
        insert(node, 1)
        insert(node, 3)
        node = Node(5)
        insert(node, 2)
        insert(node, 8)
        insert(node, 1)
        insert(node, 3)
  
          with captured_output() as (out, err):
              root.bfs(sys.stdout.write)
              assert_equal(out.getvalue().strip(), '52813')
  
keep keep keep keep replace replace replace replace replace keep keep keep keep keep
 Simpilfied breadth first search challenge. 
graphs_trees/tree_bfs/test_bfs.py