Simpilfied bst challenge.

This commit is contained in:
Donne Martin 2015-08-01 17:44:14 -04:00
parent e55c7ee1c6
commit 31058abf3a
5 changed files with 106 additions and 63 deletions

18
graphs_trees/bst/bst.py Normal file
View File

@ -0,0 +1,18 @@
class Node(object):
def __init__(self, data):
self.data = data
self.left = None
self.right = None
def insert(root, data):
if data <= root.data:
if root.left is None:
root.left = Node(data)
else:
insert(root.left, data)
else:
if root.right is None:
root.right = Node(data)
else:
insert(root.right, data)

View File

@ -86,15 +86,8 @@
" def __init__(self, data):\n",
" # TODO: Implement me\n",
"\n",
" def insert(self, data):\n",
" # TODO: Implement me\n",
"\n",
" def in_order_traversal(self, visit_func):\n",
" if self.left is not None:\n",
" self.left.in_order_traversal(visit_func)\n",
" visit_func(self.data)\n",
" if self.right is not None:\n",
" self.right.in_order_traversal(visit_func)"
"def insert(root, data):\n",
" # TODO: Implement me"
]
},
{
@ -122,6 +115,17 @@
"%run ../utils/captured_output.py"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run dfs.py"
]
},
{
"cell_type": "code",
"execution_count": null,
@ -138,24 +142,24 @@
"class TestTree(object):\n",
"\n",
" def test_tree(self):\n",
" root = Node(5)\n",
" root.insert(2)\n",
" root.insert(8)\n",
" root.insert(1)\n",
" root.insert(3)\n",
" node = Node(5)\n",
" insert(node, 2)\n",
" insert(node, 8)\n",
" insert(node, 1)\n",
" insert(node, 3)\n",
"\n",
" with captured_output() as (out, err):\n",
" root.in_order_traversal(sys.stdout.write)\n",
" in_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '12358')\n",
"\n",
" root = Node(1)\n",
" root.insert(2)\n",
" root.insert(3)\n",
" root.insert(4)\n",
" root.insert(5)\n",
" node = Node(1)\n",
" insert(node, 2)\n",
" insert(node, 3)\n",
" insert(node, 4)\n",
" insert(node, 5)\n",
"\n",
" with captured_output() as (out, err):\n",
" root.in_order_traversal(sys.stdout.write)\n",
" in_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '12345')\n",
"\n",
" print('Success: test_tree')\n",

View File

@ -100,24 +100,17 @@
" self.left = None\n",
" self.right = None\n",
"\n",
" def insert(self, data):\n",
" if data <= self.data:\n",
" if self.left is None:\n",
" self.left = Node(data)\n",
" else:\n",
" self.left.insert(data)\n",
"def insert(root, data):\n",
" if data <= root.data:\n",
" if root.left is None:\n",
" root.left = Node(data)\n",
" else:\n",
" if self.right is None:\n",
" self.right = Node(data)\n",
" else:\n",
" self.right.insert(data)\n",
"\n",
" def in_order_traversal(self, visit_func):\n",
" if self.left is not None:\n",
" self.left.in_order_traversal(visit_func)\n",
" visit_func(self.data)\n",
" if self.right is not None:\n",
" self.right.in_order_traversal(visit_func)"
" insert(root.left, data)\n",
" else:\n",
" if root.right is None:\n",
" root.right = Node(data)\n",
" else:\n",
" insert(root.right, data)"
]
},
{
@ -141,6 +134,17 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%run dfs.py"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
@ -162,24 +166,24 @@
"class TestTree(object):\n",
"\n",
" def test_tree(self):\n",
" root = Node(5)\n",
" root.insert(2)\n",
" root.insert(8)\n",
" root.insert(1)\n",
" root.insert(3)\n",
" node = Node(5)\n",
" insert(node, 2)\n",
" insert(node, 8)\n",
" insert(node, 1)\n",
" insert(node, 3)\n",
"\n",
" with captured_output() as (out, err):\n",
" root.in_order_traversal(sys.stdout.write)\n",
" in_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '12358')\n",
"\n",
" root = Node(1)\n",
" root.insert(2)\n",
" root.insert(3)\n",
" root.insert(4)\n",
" root.insert(5)\n",
" node = Node(1)\n",
" insert(node, 2)\n",
" insert(node, 3)\n",
" insert(node, 4)\n",
" insert(node, 5)\n",
"\n",
" with captured_output() as (out, err):\n",
" root.in_order_traversal(sys.stdout.write)\n",
" in_order_traversal(node, sys.stdout.write)\n",
" assert_equal(out.getvalue().strip(), '12345')\n",
"\n",
" print('Success: test_tree')\n",
@ -196,7 +200,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {
"collapsed": false
},

17
graphs_trees/bst/dfs.py Normal file
View File

@ -0,0 +1,17 @@
def in_order_traversal(node, visit_func):
if node is not None:
in_order_traversal(node.left, visit_func)
visit_func(node.data)
in_order_traversal(node.right, visit_func)
def pre_order_traversal(node, visit_func):
if node is not None:
visit_func(node.data)
pre_order_traversal(node.left, visit_func)
pre_order_traversal(node.right, visit_func)
def post_order_traversal(node, visit_func):
if node is not None:
post_order_traversal(node.left, visit_func)
post_order_traversal(node.right, visit_func)
visit_func(node.data)

View File

@ -5,24 +5,24 @@ from nose.tools import assert_equal
class TestTree(object):
def test_tree(self):
root = Node(5)
root.insert(2)
root.insert(8)
root.insert(1)
root.insert(3)
node = Node(5)
insert(node, 2)
insert(node, 8)
insert(node, 1)
insert(node, 3)
with captured_output() as (out, err):
root.in_order_traversal(sys.stdout.write)
in_order_traversal(node, sys.stdout.write)
assert_equal(out.getvalue().strip(), '12358')
root = Node(1)
root.insert(2)
root.insert(3)
root.insert(4)
root.insert(5)
node = Node(1)
insert(node, 2)
insert(node, 3)
insert(node, 4)
insert(node, 5)
with captured_output() as (out, err):
root.in_order_traversal(sys.stdout.write)
in_order_traversal(node, sys.stdout.write)
assert_equal(out.getvalue().strip(), '12345')
print('Success: test_tree')