mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-06 17:38:02 +00:00
Simpilfied bst challenge.
This commit is contained in:
@@ -100,24 +100,17 @@
|
||||
" 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",
|
||||
"def insert(root, data):\n",
|
||||
" if data <= root.data:\n",
|
||||
" if root.left is None:\n",
|
||||
" root.left = Node(data)\n",
|
||||
" else:\n",
|
||||
" if self.right is None:\n",
|
||||
" self.right = Node(data)\n",
|
||||
" else:\n",
|
||||
" self.right.insert(data)\n",
|
||||
"\n",
|
||||
" def in_order_traversal(self, visit_func):\n",
|
||||
" if self.left is not None:\n",
|
||||
" self.left.in_order_traversal(visit_func)\n",
|
||||
" visit_func(self.data)\n",
|
||||
" if self.right is not None:\n",
|
||||
" self.right.in_order_traversal(visit_func)"
|
||||
" insert(root.left, data)\n",
|
||||
" else:\n",
|
||||
" if root.right is None:\n",
|
||||
" root.right = Node(data)\n",
|
||||
" else:\n",
|
||||
" insert(root.right, data)"
|
||||
]
|
||||
},
|
||||
{
|
||||
@@ -141,6 +134,17 @@
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"%run dfs.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
@@ -162,24 +166,24 @@
|
||||
"class TestTree(object):\n",
|
||||
"\n",
|
||||
" def test_tree(self):\n",
|
||||
" root = Node(5)\n",
|
||||
" root.insert(2)\n",
|
||||
" root.insert(8)\n",
|
||||
" root.insert(1)\n",
|
||||
" root.insert(3)\n",
|
||||
" node = Node(5)\n",
|
||||
" insert(node, 2)\n",
|
||||
" insert(node, 8)\n",
|
||||
" insert(node, 1)\n",
|
||||
" insert(node, 3)\n",
|
||||
"\n",
|
||||
" with captured_output() as (out, err):\n",
|
||||
" root.in_order_traversal(sys.stdout.write)\n",
|
||||
" in_order_traversal(node, sys.stdout.write)\n",
|
||||
" assert_equal(out.getvalue().strip(), '12358')\n",
|
||||
"\n",
|
||||
" root = Node(1)\n",
|
||||
" root.insert(2)\n",
|
||||
" root.insert(3)\n",
|
||||
" root.insert(4)\n",
|
||||
" root.insert(5)\n",
|
||||
" node = Node(1)\n",
|
||||
" insert(node, 2)\n",
|
||||
" insert(node, 3)\n",
|
||||
" insert(node, 4)\n",
|
||||
" insert(node, 5)\n",
|
||||
"\n",
|
||||
" with captured_output() as (out, err):\n",
|
||||
" root.in_order_traversal(sys.stdout.write)\n",
|
||||
" in_order_traversal(node, sys.stdout.write)\n",
|
||||
" assert_equal(out.getvalue().strip(), '12345')\n",
|
||||
"\n",
|
||||
" print('Success: test_tree')\n",
|
||||
@@ -196,7 +200,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 5,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user