mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
binary tree implementation
This commit is contained in:
parent
5043e791b5
commit
980eabae68
|
@ -161,16 +161,9 @@
|
|||
"## Unit Test"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**The following unit test is expected to fail until you solve the challenge.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 6,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -188,16 +181,23 @@
|
|||
"\t\t[myTree2.insert(num) for num in range (1, 100, 10)]\n",
|
||||
"\n",
|
||||
"\t\tprint(\"Test: insert checking with in order traversal\")\n",
|
||||
"\t\tassert_equal(myTree.printInOrder(), [7, 10, 25, 30, 38, 40, 50, 60, 70, 80])\n",
|
||||
"\t\tassert_equal(myTree2.printInOrder(), [1, 11, 21, 31, 41, 51, 61, 71, 81, 91])\n",
|
||||
"\t\texpectVal = [7, 10, 25, 30, 38, 40, 50, 60, 70, 80]\n",
|
||||
"\t\tassert_equal(myTree.printInOrder(), expectVal)\n",
|
||||
"\t\texpectVal = [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]\n",
|
||||
"\t\tassert_equal(myTree2.printInOrder(), expectVal)\n",
|
||||
"\n",
|
||||
"\t\tprint(\"Test: insert checking with post order traversal\")\n",
|
||||
"\t\tassert_equal(myTree.printPostOrder(), [7, 25, 10, 38, 40, 30, 60, 80, 70, 50])\n",
|
||||
"\t\tassert_equal(myTree2.printPostOrder(), [91, 81, 71, 61, 51, 41, 31, 21, 11, 1])\n",
|
||||
"\t\texpectVal = [7, 25, 10, 38, 40, 30, 60, 80, 70, 50]\n",
|
||||
"\t\tassert_equal(myTree.printPostOrder(), expectVal)\n",
|
||||
"\t\texpectVal = [91, 81, 71, 61, 51, 41, 31, 21, 11, 1]\n",
|
||||
"\t\tassert_equal(myTree2.printPostOrder(), expectVal)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\t\tprint(\"Test: insert checking with pre order traversal\")\n",
|
||||
"\t\tassert_equal(myTree.printPreOrder(), [50, 30, 10, 7, 25, 40, 38, 70, 60, 80])\n",
|
||||
"\t\tassert_equal(myTree2.printPreOrder(), [1, 11, 21, 31, 41, 51, 61, 71, 81, 91])\n",
|
||||
"\t\texpectVal = [50, 30, 10, 7, 25, 40, 38, 70, 60, 80]\n",
|
||||
"\t\tassert_equal(myTree.printPreOrder(), expectVal)\n",
|
||||
"\t\texpectVal = [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]\n",
|
||||
"\t\tassert_equal(myTree2.printPreOrder(), expectVal)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\t\tprint(\"Success: test_insert_traversals\")\n",
|
||||
|
@ -234,6 +234,9 @@
|
|||
"\t\t[myTree.insert(x) for x in range(1, 5)]\n",
|
||||
"\t\tmyTree.delete(2)\n",
|
||||
"\t\tassert_equal(myTree.root.rightChild.data, 3)\n",
|
||||
" \n",
|
||||
"\t\tprint(\"Test: delete invalid value\")\n",
|
||||
"\t\tassert_equal(myTree.delete(100), False)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\t\tprint(\"Success: test_delete\")\n",
|
||||
|
@ -245,8 +248,14 @@
|
|||
" testing.test_delete()\n",
|
||||
" \n",
|
||||
"if __name__=='__main__':\n",
|
||||
" main()\n",
|
||||
" "
|
||||
" main()"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"**The following unit test is expected to fail until you solve the challenge.**"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
|
@ -169,7 +169,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"execution_count": 7,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -343,7 +343,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 35,
|
||||
"execution_count": 8,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -354,7 +354,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 36,
|
||||
"execution_count": 9,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -371,7 +371,7 @@
|
|||
"%%writefile test_binary_search_tree.py\n",
|
||||
"from nose.tools import assert_equal\n",
|
||||
"\n",
|
||||
"class TestBinaryTree(BinaryTree):\n",
|
||||
"class TestBinaryTree(object):\n",
|
||||
"\n",
|
||||
"\tdef test_insert_traversals (self):\n",
|
||||
"\t\tmyTree = BinaryTree()\n",
|
||||
|
@ -381,16 +381,23 @@
|
|||
"\t\t[myTree2.insert(num) for num in range (1, 100, 10)]\n",
|
||||
"\n",
|
||||
"\t\tprint(\"Test: insert checking with in order traversal\")\n",
|
||||
"\t\tassert_equal(myTree.printInOrder(), [7, 10, 25, 30, 38, 40, 50, 60, 70, 80])\n",
|
||||
"\t\tassert_equal(myTree2.printInOrder(), [1, 11, 21, 31, 41, 51, 61, 71, 81, 91])\n",
|
||||
"\t\texpectVal = [7, 10, 25, 30, 38, 40, 50, 60, 70, 80]\n",
|
||||
"\t\tassert_equal(myTree.printInOrder(), expectVal)\n",
|
||||
"\t\texpectVal = [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]\n",
|
||||
"\t\tassert_equal(myTree2.printInOrder(), expectVal)\n",
|
||||
"\n",
|
||||
"\t\tprint(\"Test: insert checking with post order traversal\")\n",
|
||||
"\t\tassert_equal(myTree.printPostOrder(), [7, 25, 10, 38, 40, 30, 60, 80, 70, 50])\n",
|
||||
"\t\tassert_equal(myTree2.printPostOrder(), [91, 81, 71, 61, 51, 41, 31, 21, 11, 1])\n",
|
||||
"\t\texpectVal = [7, 25, 10, 38, 40, 30, 60, 80, 70, 50]\n",
|
||||
"\t\tassert_equal(myTree.printPostOrder(), expectVal)\n",
|
||||
"\t\texpectVal = [91, 81, 71, 61, 51, 41, 31, 21, 11, 1]\n",
|
||||
"\t\tassert_equal(myTree2.printPostOrder(), expectVal)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\t\tprint(\"Test: insert checking with pre order traversal\")\n",
|
||||
"\t\tassert_equal(myTree.printPreOrder(), [50, 30, 10, 7, 25, 40, 38, 70, 60, 80])\n",
|
||||
"\t\tassert_equal(myTree2.printPreOrder(), [1, 11, 21, 31, 41, 51, 61, 71, 81, 91])\n",
|
||||
"\t\texpectVal = [50, 30, 10, 7, 25, 40, 38, 70, 60, 80]\n",
|
||||
"\t\tassert_equal(myTree.printPreOrder(), expectVal)\n",
|
||||
"\t\texpectVal = [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]\n",
|
||||
"\t\tassert_equal(myTree2.printPreOrder(), expectVal)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"\t\tprint(\"Success: test_insert_traversals\")\n",
|
||||
|
@ -427,6 +434,7 @@
|
|||
"\t\t[myTree.insert(x) for x in range(1, 5)]\n",
|
||||
"\t\tmyTree.delete(2)\n",
|
||||
"\t\tassert_equal(myTree.root.rightChild.data, 3)\n",
|
||||
" \n",
|
||||
"\t\tprint(\"Test: delete invalid value\")\n",
|
||||
"\t\tassert_equal(myTree.delete(100), False)\n",
|
||||
"\n",
|
||||
|
@ -445,7 +453,7 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 37,
|
||||
"execution_count": 10,
|
||||
"metadata": {
|
||||
"collapsed": false
|
||||
},
|
||||
|
@ -472,15 +480,6 @@
|
|||
"source": [
|
||||
"%run -i test_binary_search_tree.py"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {
|
||||
"collapsed": true
|
||||
},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from nose.tools import assert_equal
|
||||
|
||||
class TestBinaryTree(BinaryTree):
|
||||
class TestBinaryTree(object):
|
||||
|
||||
def test_insert_traversals (self):
|
||||
myTree = BinaryTree()
|
||||
|
@ -10,16 +10,23 @@ class TestBinaryTree(BinaryTree):
|
|||
[myTree2.insert(num) for num in range (1, 100, 10)]
|
||||
|
||||
print("Test: insert checking with in order traversal")
|
||||
assert_equal(myTree.printInOrder(), [7, 10, 25, 30, 38, 40, 50, 60, 70, 80])
|
||||
assert_equal(myTree2.printInOrder(), [1, 11, 21, 31, 41, 51, 61, 71, 81, 91])
|
||||
expectVal = [7, 10, 25, 30, 38, 40, 50, 60, 70, 80]
|
||||
assert_equal(myTree.printInOrder(), expectVal)
|
||||
expectVal = [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
|
||||
assert_equal(myTree2.printInOrder(), expectVal)
|
||||
|
||||
print("Test: insert checking with post order traversal")
|
||||
assert_equal(myTree.printPostOrder(), [7, 25, 10, 38, 40, 30, 60, 80, 70, 50])
|
||||
assert_equal(myTree2.printPostOrder(), [91, 81, 71, 61, 51, 41, 31, 21, 11, 1])
|
||||
expectVal = [7, 25, 10, 38, 40, 30, 60, 80, 70, 50]
|
||||
assert_equal(myTree.printPostOrder(), expectVal)
|
||||
expectVal = [91, 81, 71, 61, 51, 41, 31, 21, 11, 1]
|
||||
assert_equal(myTree2.printPostOrder(), expectVal)
|
||||
|
||||
|
||||
print("Test: insert checking with pre order traversal")
|
||||
assert_equal(myTree.printPreOrder(), [50, 30, 10, 7, 25, 40, 38, 70, 60, 80])
|
||||
assert_equal(myTree2.printPreOrder(), [1, 11, 21, 31, 41, 51, 61, 71, 81, 91])
|
||||
expectVal = [50, 30, 10, 7, 25, 40, 38, 70, 60, 80]
|
||||
assert_equal(myTree.printPreOrder(), expectVal)
|
||||
expectVal = [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]
|
||||
assert_equal(myTree2.printPreOrder(), expectVal)
|
||||
|
||||
|
||||
print("Success: test_insert_traversals")
|
||||
|
|
Loading…
Reference in New Issue
Block a user