Update bst insert to return the inserted node (#81)

This commit is contained in:
Donne Martin 2016-06-25 21:03:26 -04:00 committed by GitHub
parent 7882ed9ae7
commit 042161dc3b
4 changed files with 29 additions and 25 deletions

View File

@ -19,11 +19,13 @@ def insert(root, data):
if root.left is None:
root.left = insert(root.left, data)
root.left.parent = root
return root.left
else:
insert(root.left, data)
return insert(root.left, data)
else:
if root.right is None:
root.right = insert(root.right, data)
root.right.parent = root
return root.right
else:
insert(root.right, data)
return insert(root.right, data)

View File

@ -158,18 +158,18 @@
"\n",
" def test_tree(self):\n",
" node = Node(5)\n",
" insert(node, 2)\n",
" insert(node, 8)\n",
" insert(node, 1)\n",
" insert(node, 3)\n",
" assert_equal(insert(node, 2).data, 2)\n",
" assert_equal(insert(node, 8).data, 8)\n",
" assert_equal(insert(node, 1).data, 1)\n",
" assert_equal(insert(node, 3).data, 3)\n",
" in_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
" self.results.clear_results()\n",
"\n",
" node = insert(None, 1)\n",
" insert(node, 2)\n",
" insert(node, 3)\n",
" insert(node, 4)\n",
" assert_equal(insert(node, 2).data, 2)\n",
" assert_equal(insert(node, 3).data, 3)\n",
" assert_equal(insert(node, 4).data, 4)\n",
" insert(node, 5)\n",
" in_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",

View File

@ -136,14 +136,16 @@
" if root.left is None:\n",
" root.left = insert(root.left, data)\n",
" root.left.parent = root\n",
" return root.left\n",
" else:\n",
" insert(root.left, data)\n",
" return insert(root.left, data)\n",
" else:\n",
" if root.right is None:\n",
" root.right = insert(root.right, data)\n",
" root.right.parent = root\n",
" return root.right\n",
" else:\n",
" insert(root.right, data)"
" return insert(root.right, data)"
]
},
{
@ -213,18 +215,18 @@
"\n",
" def test_tree(self):\n",
" node = Node(5)\n",
" insert(node, 2)\n",
" insert(node, 8)\n",
" insert(node, 1)\n",
" insert(node, 3)\n",
" assert_equal(insert(node, 2).data, 2)\n",
" assert_equal(insert(node, 8).data, 8)\n",
" assert_equal(insert(node, 1).data, 1)\n",
" assert_equal(insert(node, 3).data, 3)\n",
" in_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
" self.results.clear_results()\n",
"\n",
" node = insert(None, 1)\n",
" insert(node, 2)\n",
" insert(node, 3)\n",
" insert(node, 4)\n",
" assert_equal(insert(node, 2).data, 2)\n",
" assert_equal(insert(node, 3).data, 3)\n",
" assert_equal(insert(node, 4).data, 4)\n",
" insert(node, 5)\n",
" in_order_traversal(node, self.results.add_result)\n",
" assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",

View File

@ -8,18 +8,18 @@ class TestTree(object):
def test_tree(self):
node = Node(5)
insert(node, 2)
insert(node, 8)
insert(node, 1)
insert(node, 3)
assert_equal(insert(node, 2).data, 2)
assert_equal(insert(node, 8).data, 8)
assert_equal(insert(node, 1).data, 1)
assert_equal(insert(node, 3).data, 3)
in_order_traversal(node, self.results.add_result)
assert_equal(str(self.results), '[1, 2, 3, 5, 8]')
self.results.clear_results()
node = insert(None, 1)
insert(node, 2)
insert(node, 3)
insert(node, 4)
assert_equal(insert(node, 2).data, 2)
assert_equal(insert(node, 3).data, 3)
assert_equal(insert(node, 4).data, 4)
insert(node, 5)
in_order_traversal(node, self.results.add_result)
assert_equal(str(self.results), '[1, 2, 3, 4, 5]')