Polish bst challenge and solution

Add root is None input test case.  Update time and space complexity discussion.
This commit is contained in:
Donne Martin
2016-03-01 07:03:37 -05:00
parent 56f8ea1e74
commit de0e70de4f
4 changed files with 30 additions and 17 deletions

View File

@@ -11,6 +11,9 @@ class Node(object):
def insert(root, data):
if root is None:
root = Node(data)
return root
if data <= root.data:
if root.left is None:
root.left = Node(data)

View File

@@ -56,7 +56,9 @@
"### In-Order Traversal (Provided)\n",
"\n",
"* 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8\n",
"* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5"
"* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5\n",
"\n",
"If the `root` input is `None`, return a tree with the only element being the new root node."
]
},
{
@@ -158,7 +160,7 @@
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
" self.results.clear_results()\n",
"\n",
" node = Node(1)\n",
" node = insert(None, 1)\n",
" insert(node, 2)\n",
" insert(node, 3)\n",
" insert(node, 4)\n",
@@ -190,21 +192,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,

View File

@@ -56,7 +56,9 @@
"### In-Order Traversal (Provided)\n",
"\n",
"* 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8\n",
"* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5"
"* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5\n",
"\n",
"If the `root` input is `None`, return a tree with the only element being the new root node."
]
},
{
@@ -67,6 +69,7 @@
"\n",
"### Insert\n",
"\n",
"* If the root is None, return Node(data)\n",
"* If the data is <= the current node's data\n",
" * If the current node's left child is None, set it to Node(data)\n",
" * Else, recursively call insert on the left child\n",
@@ -76,8 +79,10 @@
"\n",
"Complexity:\n",
"\n",
"* Time: O(log n)\n",
"* Space: O(1)"
"* Time: O(h), where h is the height of the tree\n",
" * In a balanced tree, the height is O(nlogn)\n",
" * In the worst case we have a linked list structure with O(n)\n",
"* Space: O(m), where m is the recursion depth, or O(1) if using an iterative approach"
]
},
{
@@ -117,6 +122,9 @@
"\n",
"\n",
"def insert(root, data):\n",
" if root is None:\n",
" root = Node(data)\n",
" return root\n",
" if data <= root.data:\n",
" if root.left is None:\n",
" root.left = Node(data)\n",
@@ -208,7 +216,7 @@
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
" self.results.clear_results()\n",
"\n",
" node = Node(1)\n",
" node = insert(None, 1)\n",
" insert(node, 2)\n",
" insert(node, 3)\n",
" insert(node, 4)\n",
@@ -250,21 +258,21 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 2",
"display_name": "Python 3",
"language": "python",
"name": "python2"
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.10"
"pygments_lexer": "ipython3",
"version": "3.4.3"
}
},
"nbformat": 4,

View File

@@ -16,7 +16,7 @@ class TestTree(object):
assert_equal(str(self.results), '[1, 2, 3, 5, 8]')
self.results.clear_results()
node = Node(1)
node = insert(None, 1)
insert(node, 2)
insert(node, 3)
insert(node, 4)