Polish bst min challenge and solution (#76)

Update constraints, algorithm discussion, and code.
This commit is contained in:
Donne Martin 2016-06-25 08:23:28 -04:00 committed by GitHub
parent c24a628329
commit 1321723eb9
2 changed files with 21 additions and 17 deletions

View File

@ -39,6 +39,8 @@
"* Are the array elements unique?\n",
" * Yes\n",
"* Can we assume we already have a Node class with an insert method?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
@ -90,11 +92,11 @@
"source": [
"def create_min_bst(array):\n",
" # TODO: Implement me\n",
" # Calls __create_min_bst__\n",
" # Calls _create_min_bst\n",
" pass \n",
"\n",
"\n",
"def __create_min_bst__(array, start, end):\n",
"def _create_min_bst(array, start, end):\n",
" # TODO: Implement me\n",
" pass"
]
@ -171,21 +173,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.5.0"
}
},
"nbformat": 4,

View File

@ -38,6 +38,8 @@
"* Are the array elements unique?\n",
" * Yes\n",
"* Can we assume we already have a Node class with an insert method?\n",
" * Yes\n",
"* Can we assume this fits memory?\n",
" * Yes"
]
},
@ -103,16 +105,16 @@
"def create_min_bst(array):\n",
" if array is None:\n",
" return\n",
" return __create_min_bst__(array, 0, len(array)-1)\n",
" return _create_min_bst(array, 0, len(array)-1)\n",
"\n",
"\n",
"def __create_min_bst__(array, start, end):\n",
"def _create_min_bst(array, start, end):\n",
" if end < start:\n",
" return\n",
" return None\n",
" mid = (start + end) // 2\n",
" node = Node(array[mid])\n",
" node.left = __create_min_bst__(array, start, mid-1)\n",
" node.right = __create_min_bst__(array, mid+1, end)\n",
" node.left = _create_min_bst(array, start, mid-1)\n",
" node.right = _create_min_bst(array, mid+1, end)\n",
" return node"
]
},
@ -199,21 +201,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.5.0"
}
},
"nbformat": 4,