binary tree implementation

This commit is contained in:
mag6367 2015-07-18 00:55:33 -05:00
parent 5043e791b5
commit 980eabae68
3 changed files with 58 additions and 43 deletions

View File

@ -161,16 +161,9 @@
"## Unit Test" "## Unit Test"
] ]
}, },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**The following unit test is expected to fail until you solve the challenge.**"
]
},
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 4, "execution_count": 6,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -188,16 +181,23 @@
"\t\t[myTree2.insert(num) for num in range (1, 100, 10)]\n", "\t\t[myTree2.insert(num) for num in range (1, 100, 10)]\n",
"\n", "\n",
"\t\tprint(\"Test: insert checking with in order traversal\")\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\texpectVal = [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\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\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\texpectVal = [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\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",
"\n", "\n",
"\t\tprint(\"Test: insert checking with pre order traversal\")\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\texpectVal = [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\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",
"\n", "\n",
"\t\tprint(\"Success: test_insert_traversals\")\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\t[myTree.insert(x) for x in range(1, 5)]\n",
"\t\tmyTree.delete(2)\n", "\t\tmyTree.delete(2)\n",
"\t\tassert_equal(myTree.root.rightChild.data, 3)\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",
"\n", "\n",
"\t\tprint(\"Success: test_delete\")\n", "\t\tprint(\"Success: test_delete\")\n",
@ -245,8 +248,14 @@
" testing.test_delete()\n", " testing.test_delete()\n",
" \n", " \n",
"if __name__=='__main__':\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.**"
] ]
}, },
{ {

View File

@ -169,7 +169,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 27, "execution_count": 7,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -343,7 +343,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 35, "execution_count": 8,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -354,7 +354,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 36, "execution_count": 9,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -371,7 +371,7 @@
"%%writefile test_binary_search_tree.py\n", "%%writefile test_binary_search_tree.py\n",
"from nose.tools import assert_equal\n", "from nose.tools import assert_equal\n",
"\n", "\n",
"class TestBinaryTree(BinaryTree):\n", "class TestBinaryTree(object):\n",
"\n", "\n",
"\tdef test_insert_traversals (self):\n", "\tdef test_insert_traversals (self):\n",
"\t\tmyTree = BinaryTree()\n", "\t\tmyTree = BinaryTree()\n",
@ -381,16 +381,23 @@
"\t\t[myTree2.insert(num) for num in range (1, 100, 10)]\n", "\t\t[myTree2.insert(num) for num in range (1, 100, 10)]\n",
"\n", "\n",
"\t\tprint(\"Test: insert checking with in order traversal\")\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\texpectVal = [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\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\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\texpectVal = [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\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",
"\n", "\n",
"\t\tprint(\"Test: insert checking with pre order traversal\")\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\texpectVal = [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\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",
"\n", "\n",
"\t\tprint(\"Success: test_insert_traversals\")\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\t[myTree.insert(x) for x in range(1, 5)]\n",
"\t\tmyTree.delete(2)\n", "\t\tmyTree.delete(2)\n",
"\t\tassert_equal(myTree.root.rightChild.data, 3)\n", "\t\tassert_equal(myTree.root.rightChild.data, 3)\n",
" \n",
"\t\tprint(\"Test: delete invalid value\")\n", "\t\tprint(\"Test: delete invalid value\")\n",
"\t\tassert_equal(myTree.delete(100), False)\n", "\t\tassert_equal(myTree.delete(100), False)\n",
"\n", "\n",
@ -445,7 +453,7 @@
}, },
{ {
"cell_type": "code", "cell_type": "code",
"execution_count": 37, "execution_count": 10,
"metadata": { "metadata": {
"collapsed": false "collapsed": false
}, },
@ -472,15 +480,6 @@
"source": [ "source": [
"%run -i test_binary_search_tree.py" "%run -i test_binary_search_tree.py"
] ]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
} }
], ],
"metadata": { "metadata": {

View File

@ -1,6 +1,6 @@
from nose.tools import assert_equal from nose.tools import assert_equal
class TestBinaryTree(BinaryTree): class TestBinaryTree(object):
def test_insert_traversals (self): def test_insert_traversals (self):
myTree = BinaryTree() myTree = BinaryTree()
@ -10,16 +10,23 @@ class TestBinaryTree(BinaryTree):
[myTree2.insert(num) for num in range (1, 100, 10)] [myTree2.insert(num) for num in range (1, 100, 10)]
print("Test: insert checking with in order traversal") print("Test: insert checking with in order traversal")
assert_equal(myTree.printInOrder(), [7, 10, 25, 30, 38, 40, 50, 60, 70, 80]) expectVal = [7, 10, 25, 30, 38, 40, 50, 60, 70, 80]
assert_equal(myTree2.printInOrder(), [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]) 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") print("Test: insert checking with post order traversal")
assert_equal(myTree.printPostOrder(), [7, 25, 10, 38, 40, 30, 60, 80, 70, 50]) expectVal = [7, 25, 10, 38, 40, 30, 60, 80, 70, 50]
assert_equal(myTree2.printPostOrder(), [91, 81, 71, 61, 51, 41, 31, 21, 11, 1]) 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") print("Test: insert checking with pre order traversal")
assert_equal(myTree.printPreOrder(), [50, 30, 10, 7, 25, 40, 38, 70, 60, 80]) expectVal = [50, 30, 10, 7, 25, 40, 38, 70, 60, 80]
assert_equal(myTree2.printPreOrder(), [1, 11, 21, 31, 41, 51, 61, 71, 81, 91]) 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") print("Success: test_insert_traversals")