mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Update bst insert to return the inserted node (#81)
This commit is contained in:
parent
7882ed9ae7
commit
042161dc3b
|
@ -19,11 +19,13 @@ def insert(root, data):
|
||||||
if root.left is None:
|
if root.left is None:
|
||||||
root.left = insert(root.left, data)
|
root.left = insert(root.left, data)
|
||||||
root.left.parent = root
|
root.left.parent = root
|
||||||
|
return root.left
|
||||||
else:
|
else:
|
||||||
insert(root.left, data)
|
return insert(root.left, data)
|
||||||
else:
|
else:
|
||||||
if root.right is None:
|
if root.right is None:
|
||||||
root.right = insert(root.right, data)
|
root.right = insert(root.right, data)
|
||||||
root.right.parent = root
|
root.right.parent = root
|
||||||
|
return root.right
|
||||||
else:
|
else:
|
||||||
insert(root.right, data)
|
return insert(root.right, data)
|
|
@ -158,18 +158,18 @@
|
||||||
"\n",
|
"\n",
|
||||||
" def test_tree(self):\n",
|
" def test_tree(self):\n",
|
||||||
" node = Node(5)\n",
|
" node = Node(5)\n",
|
||||||
" insert(node, 2)\n",
|
" assert_equal(insert(node, 2).data, 2)\n",
|
||||||
" insert(node, 8)\n",
|
" assert_equal(insert(node, 8).data, 8)\n",
|
||||||
" insert(node, 1)\n",
|
" assert_equal(insert(node, 1).data, 1)\n",
|
||||||
" insert(node, 3)\n",
|
" assert_equal(insert(node, 3).data, 3)\n",
|
||||||
" in_order_traversal(node, self.results.add_result)\n",
|
" in_order_traversal(node, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
|
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" node = insert(None, 1)\n",
|
" node = insert(None, 1)\n",
|
||||||
" insert(node, 2)\n",
|
" assert_equal(insert(node, 2).data, 2)\n",
|
||||||
" insert(node, 3)\n",
|
" assert_equal(insert(node, 3).data, 3)\n",
|
||||||
" insert(node, 4)\n",
|
" assert_equal(insert(node, 4).data, 4)\n",
|
||||||
" insert(node, 5)\n",
|
" insert(node, 5)\n",
|
||||||
" in_order_traversal(node, self.results.add_result)\n",
|
" in_order_traversal(node, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",
|
" assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",
|
||||||
|
|
|
@ -136,14 +136,16 @@
|
||||||
" if root.left is None:\n",
|
" if root.left is None:\n",
|
||||||
" root.left = insert(root.left, data)\n",
|
" root.left = insert(root.left, data)\n",
|
||||||
" root.left.parent = root\n",
|
" root.left.parent = root\n",
|
||||||
|
" return root.left\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" insert(root.left, data)\n",
|
" return insert(root.left, data)\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" if root.right is None:\n",
|
" if root.right is None:\n",
|
||||||
" root.right = insert(root.right, data)\n",
|
" root.right = insert(root.right, data)\n",
|
||||||
" root.right.parent = root\n",
|
" root.right.parent = root\n",
|
||||||
|
" return root.right\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" insert(root.right, data)"
|
" return insert(root.right, data)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -213,18 +215,18 @@
|
||||||
"\n",
|
"\n",
|
||||||
" def test_tree(self):\n",
|
" def test_tree(self):\n",
|
||||||
" node = Node(5)\n",
|
" node = Node(5)\n",
|
||||||
" insert(node, 2)\n",
|
" assert_equal(insert(node, 2).data, 2)\n",
|
||||||
" insert(node, 8)\n",
|
" assert_equal(insert(node, 8).data, 8)\n",
|
||||||
" insert(node, 1)\n",
|
" assert_equal(insert(node, 1).data, 1)\n",
|
||||||
" insert(node, 3)\n",
|
" assert_equal(insert(node, 3).data, 3)\n",
|
||||||
" in_order_traversal(node, self.results.add_result)\n",
|
" in_order_traversal(node, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
|
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" node = insert(None, 1)\n",
|
" node = insert(None, 1)\n",
|
||||||
" insert(node, 2)\n",
|
" assert_equal(insert(node, 2).data, 2)\n",
|
||||||
" insert(node, 3)\n",
|
" assert_equal(insert(node, 3).data, 3)\n",
|
||||||
" insert(node, 4)\n",
|
" assert_equal(insert(node, 4).data, 4)\n",
|
||||||
" insert(node, 5)\n",
|
" insert(node, 5)\n",
|
||||||
" in_order_traversal(node, self.results.add_result)\n",
|
" in_order_traversal(node, self.results.add_result)\n",
|
||||||
" assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",
|
" assert_equal(str(self.results), '[1, 2, 3, 4, 5]')\n",
|
||||||
|
|
|
@ -8,18 +8,18 @@ class TestTree(object):
|
||||||
|
|
||||||
def test_tree(self):
|
def test_tree(self):
|
||||||
node = Node(5)
|
node = Node(5)
|
||||||
insert(node, 2)
|
assert_equal(insert(node, 2).data, 2)
|
||||||
insert(node, 8)
|
assert_equal(insert(node, 8).data, 8)
|
||||||
insert(node, 1)
|
assert_equal(insert(node, 1).data, 1)
|
||||||
insert(node, 3)
|
assert_equal(insert(node, 3).data, 3)
|
||||||
in_order_traversal(node, self.results.add_result)
|
in_order_traversal(node, self.results.add_result)
|
||||||
assert_equal(str(self.results), '[1, 2, 3, 5, 8]')
|
assert_equal(str(self.results), '[1, 2, 3, 5, 8]')
|
||||||
self.results.clear_results()
|
self.results.clear_results()
|
||||||
|
|
||||||
node = insert(None, 1)
|
node = insert(None, 1)
|
||||||
insert(node, 2)
|
assert_equal(insert(node, 2).data, 2)
|
||||||
insert(node, 3)
|
assert_equal(insert(node, 3).data, 3)
|
||||||
insert(node, 4)
|
assert_equal(insert(node, 4).data, 4)
|
||||||
insert(node, 5)
|
insert(node, 5)
|
||||||
in_order_traversal(node, self.results.add_result)
|
in_order_traversal(node, self.results.add_result)
|
||||||
assert_equal(str(self.results), '[1, 2, 3, 4, 5]')
|
assert_equal(str(self.results), '[1, 2, 3, 4, 5]')
|
||||||
|
|
Loading…
Reference in New Issue
Block a user