Polish bst challenge and solution (#75)

Update constraints, algorithm discussion, and code.
This commit is contained in:
Donne Martin 2016-06-24 07:23:20 -04:00 committed by GitHub
parent c7e9a85db7
commit c24a628329
3 changed files with 30 additions and 20 deletions

View File

@ -11,20 +11,19 @@ class Node(object):
def insert(root, data):
# Constraint: Assume we are working with valid ints
if root is None:
root = Node(data)
return root
if data <= root.data:
if root.left is None:
root.left = Node(data)
root.left = insert(root.left, data)
root.left.parent = root
return root.left
else:
return insert(root.left, data)
insert(root.left, data)
else:
if root.right is None:
root.right = Node(data)
root.right = insert(root.right, data)
root.right.parent = root
return root.right
else:
return insert(root.right, data)
insert(root.right, data)

View File

@ -33,6 +33,8 @@
"source": [
"## Constraints\n",
"\n",
"* Can we insert None values?\n",
" * No\n",
"* Can we assume we are working with valid integers?\n",
" * Yes\n",
"* Can we assume all left descendents <= n < all right descendents?\n",
@ -40,7 +42,9 @@
"* For simplicity, can we use just a Node class without a wrapper Tree class?\n",
" * Yes\n",
"* Do we have to keep track of the parent nodes?\n",
" * This is optional"
" * This is optional\n",
"* Can we assume this fits in memory?\n",
" * Yes"
]
},
{
@ -53,12 +57,14 @@
"\n",
"Insert will be tested through the following traversal:\n",
"\n",
"### In-Order Traversal (Provided)\n",
"### In-Order Traversal\n",
"\n",
"* 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8\n",
"* 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."
"If the `root` input is `None`, return a tree with the only element being the new root node.\n",
"\n",
"You do not have to code the in-order traversal, it is part of the unit test."
]
},
{
@ -206,7 +212,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.5.0"
}
},
"nbformat": 4,

View File

@ -33,6 +33,8 @@
"source": [
"## Constraints\n",
"\n",
"* Can we insert None values?\n",
" * No\n",
"* Can we assume we are working with valid integers?\n",
" * Yes\n",
"* Can we assume all left descendents <= n < all right descendents?\n",
@ -40,7 +42,9 @@
"* For simplicity, can we use just a Node class without a wrapper Tree class?\n",
" * Yes\n",
"* Do we have to keep track of the parent nodes?\n",
" * This is optional"
" * This is optional\n",
"* Can we assume this fits in memory?\n",
" * Yes"
]
},
{
@ -53,12 +57,14 @@
"\n",
"Insert will be tested through the following traversal:\n",
"\n",
"### In-Order Traversal (Provided)\n",
"### In-Order Traversal\n",
"\n",
"* 5, 2, 8, 1, 3 -> 1, 2, 3, 5, 8\n",
"* 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."
"If the `root` input is `None`, return a tree with the only element being the new root node.\n",
"\n",
"You do not have to code the in-order traversal, it is part of the unit test."
]
},
{
@ -122,23 +128,22 @@
"\n",
"\n",
"def insert(root, data):\n",
" # Constraint: Assume we are working with valid ints\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",
" root.left = insert(root.left, data)\n",
" root.left.parent = root\n",
" return root.left\n",
" else:\n",
" return insert(root.left, data)\n",
" insert(root.left, data)\n",
" else:\n",
" if root.right is None:\n",
" root.right = Node(data)\n",
" root.right = insert(root.right, data)\n",
" root.right.parent = root\n",
" return root.right\n",
" else:\n",
" return insert(root.right, data)"
" insert(root.right, data)"
]
},
{
@ -272,7 +277,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.4.3"
"version": "3.5.0"
}
},
"nbformat": 4,