mirror of
https://github.com/donnemartin/interactive-coding-challenges
synced 2026-01-07 09:58:03 +00:00
Polish bst challenge and solution
Add root is None input test case. Update time and space complexity discussion.
This commit is contained in:
@@ -11,6 +11,9 @@ class Node(object):
|
|||||||
|
|
||||||
|
|
||||||
def insert(root, data):
|
def insert(root, data):
|
||||||
|
if root is None:
|
||||||
|
root = Node(data)
|
||||||
|
return root
|
||||||
if data <= root.data:
|
if data <= root.data:
|
||||||
if root.left is None:
|
if root.left is None:
|
||||||
root.left = Node(data)
|
root.left = Node(data)
|
||||||
|
|||||||
@@ -56,7 +56,9 @@
|
|||||||
"### In-Order Traversal (Provided)\n",
|
"### In-Order Traversal (Provided)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8\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",
|
" assert_equal(str(self.results), '[1, 2, 3, 5, 8]')\n",
|
||||||
" self.results.clear_results()\n",
|
" self.results.clear_results()\n",
|
||||||
"\n",
|
"\n",
|
||||||
" node = Node(1)\n",
|
" node = insert(None, 1)\n",
|
||||||
" insert(node, 2)\n",
|
" insert(node, 2)\n",
|
||||||
" insert(node, 3)\n",
|
" insert(node, 3)\n",
|
||||||
" insert(node, 4)\n",
|
" insert(node, 4)\n",
|
||||||
@@ -190,21 +192,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.4.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -56,7 +56,9 @@
|
|||||||
"### In-Order Traversal (Provided)\n",
|
"### In-Order Traversal (Provided)\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8\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",
|
"\n",
|
||||||
"### Insert\n",
|
"### Insert\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"* If the root is None, return Node(data)\n",
|
||||||
"* If the data is <= the current node's 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",
|
" * If the current node's left child is None, set it to Node(data)\n",
|
||||||
" * Else, recursively call insert on the left child\n",
|
" * Else, recursively call insert on the left child\n",
|
||||||
@@ -76,8 +79,10 @@
|
|||||||
"\n",
|
"\n",
|
||||||
"Complexity:\n",
|
"Complexity:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"* Time: O(log n)\n",
|
"* Time: O(h), where h is the height of the tree\n",
|
||||||
"* Space: O(1)"
|
" * 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",
|
||||||
"\n",
|
"\n",
|
||||||
"def insert(root, data):\n",
|
"def insert(root, data):\n",
|
||||||
|
" if root is None:\n",
|
||||||
|
" root = Node(data)\n",
|
||||||
|
" return root\n",
|
||||||
" if data <= root.data:\n",
|
" if data <= root.data:\n",
|
||||||
" if root.left is None:\n",
|
" if root.left is None:\n",
|
||||||
" root.left = Node(data)\n",
|
" root.left = Node(data)\n",
|
||||||
@@ -208,7 +216,7 @@
|
|||||||
" 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 = Node(1)\n",
|
" node = insert(None, 1)\n",
|
||||||
" insert(node, 2)\n",
|
" insert(node, 2)\n",
|
||||||
" insert(node, 3)\n",
|
" insert(node, 3)\n",
|
||||||
" insert(node, 4)\n",
|
" insert(node, 4)\n",
|
||||||
@@ -250,21 +258,21 @@
|
|||||||
],
|
],
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"kernelspec": {
|
"kernelspec": {
|
||||||
"display_name": "Python 2",
|
"display_name": "Python 3",
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"name": "python2"
|
"name": "python3"
|
||||||
},
|
},
|
||||||
"language_info": {
|
"language_info": {
|
||||||
"codemirror_mode": {
|
"codemirror_mode": {
|
||||||
"name": "ipython",
|
"name": "ipython",
|
||||||
"version": 2
|
"version": 3
|
||||||
},
|
},
|
||||||
"file_extension": ".py",
|
"file_extension": ".py",
|
||||||
"mimetype": "text/x-python",
|
"mimetype": "text/x-python",
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython2",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "2.7.10"
|
"version": "3.4.3"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ class TestTree(object):
|
|||||||
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 = Node(1)
|
node = insert(None, 1)
|
||||||
insert(node, 2)
|
insert(node, 2)
|
||||||
insert(node, 3)
|
insert(node, 3)
|
||||||
insert(node, 4)
|
insert(node, 4)
|
||||||
|
|||||||
Reference in New Issue
Block a user