mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Polish bst challenge and solution (#75)
Update constraints, algorithm discussion, and code.
This commit is contained in:
parent
c7e9a85db7
commit
c24a628329
|
@ -11,20 +11,19 @@ class Node(object):
|
||||||
|
|
||||||
|
|
||||||
def insert(root, data):
|
def insert(root, data):
|
||||||
|
# Constraint: Assume we are working with valid ints
|
||||||
if root is None:
|
if root is None:
|
||||||
root = Node(data)
|
root = Node(data)
|
||||||
return root
|
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 = insert(root.left, data)
|
||||||
root.left.parent = root
|
root.left.parent = root
|
||||||
return root.left
|
|
||||||
else:
|
else:
|
||||||
return insert(root.left, data)
|
insert(root.left, data)
|
||||||
else:
|
else:
|
||||||
if root.right is None:
|
if root.right is None:
|
||||||
root.right = Node(data)
|
root.right = insert(root.right, data)
|
||||||
root.right.parent = root
|
root.right.parent = root
|
||||||
return root.right
|
|
||||||
else:
|
else:
|
||||||
return insert(root.right, data)
|
insert(root.right, data)
|
|
@ -33,6 +33,8 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"* Can we insert None values?\n",
|
||||||
|
" * No\n",
|
||||||
"* Can we assume we are working with valid integers?\n",
|
"* Can we assume we are working with valid integers?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Can we assume all left descendents <= n < all right descendents?\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",
|
"* For simplicity, can we use just a Node class without a wrapper Tree class?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Do we have to keep track of the parent nodes?\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",
|
"\n",
|
||||||
"Insert will be tested through the following traversal:\n",
|
"Insert will be tested through the following traversal:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"### In-Order Traversal (Provided)\n",
|
"### In-Order Traversal\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\n",
|
"* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5\n",
|
||||||
"\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",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
|
@ -33,6 +33,8 @@
|
||||||
"source": [
|
"source": [
|
||||||
"## Constraints\n",
|
"## Constraints\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"* Can we insert None values?\n",
|
||||||
|
" * No\n",
|
||||||
"* Can we assume we are working with valid integers?\n",
|
"* Can we assume we are working with valid integers?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Can we assume all left descendents <= n < all right descendents?\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",
|
"* For simplicity, can we use just a Node class without a wrapper Tree class?\n",
|
||||||
" * Yes\n",
|
" * Yes\n",
|
||||||
"* Do we have to keep track of the parent nodes?\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",
|
"\n",
|
||||||
"Insert will be tested through the following traversal:\n",
|
"Insert will be tested through the following traversal:\n",
|
||||||
"\n",
|
"\n",
|
||||||
"### In-Order Traversal (Provided)\n",
|
"### In-Order Traversal\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\n",
|
"* 1, 2, 3, 4, 5 -> 1, 2, 3, 4, 5\n",
|
||||||
"\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",
|
||||||
"\n",
|
"\n",
|
||||||
"def insert(root, data):\n",
|
"def insert(root, data):\n",
|
||||||
|
" # Constraint: Assume we are working with valid ints\n",
|
||||||
" if root is None:\n",
|
" if root is None:\n",
|
||||||
" root = Node(data)\n",
|
" root = Node(data)\n",
|
||||||
" return root\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 = insert(root.left, data)\n",
|
||||||
" root.left.parent = root\n",
|
" root.left.parent = root\n",
|
||||||
" return root.left\n",
|
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" return insert(root.left, data)\n",
|
" insert(root.left, data)\n",
|
||||||
" else:\n",
|
" else:\n",
|
||||||
" if root.right is None:\n",
|
" if root.right is None:\n",
|
||||||
" root.right = Node(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",
|
||||||
" return insert(root.right, data)"
|
" insert(root.right, data)"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -272,7 +277,7 @@
|
||||||
"name": "python",
|
"name": "python",
|
||||||
"nbconvert_exporter": "python",
|
"nbconvert_exporter": "python",
|
||||||
"pygments_lexer": "ipython3",
|
"pygments_lexer": "ipython3",
|
||||||
"version": "3.4.3"
|
"version": "3.5.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"nbformat": 4,
|
"nbformat": 4,
|
||||||
|
|
Loading…
Reference in New Issue
Block a user