mirror of
https://github.com/donnemartin/interactive-coding-challenges.git
synced 2024-03-22 13:11:13 +08:00
Move bst successor to a class
This commit is contained in:
parent
857497115a
commit
b91b6a437b
|
@ -105,9 +105,11 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def bst_successor(node):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
"class BstSuccessor(object):\n",
|
||||
"\n",
|
||||
" def get_next(self, node):\n",
|
||||
" # TODO: Implement me\n",
|
||||
" pass"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -141,28 +143,31 @@
|
|||
"\n",
|
||||
" @raises(Exception)\n",
|
||||
" def test_bst_successor_empty(self):\n",
|
||||
" bst_successor(None)\n",
|
||||
" bst_successor = BstSuccessor()\n",
|
||||
" bst_successor.get_next(None)\n",
|
||||
"\n",
|
||||
" def test_bst_successor(self):\n",
|
||||
" nodes = {}\n",
|
||||
" node = Node(5)\n",
|
||||
" nodes[5] = node\n",
|
||||
" nodes[3] = insert(node, 3)\n",
|
||||
" nodes[8] = insert(node, 8)\n",
|
||||
" nodes[2] = insert(node, 2)\n",
|
||||
" nodes[4] = insert(node, 4)\n",
|
||||
" nodes[6] = insert(node, 6)\n",
|
||||
" nodes[12] = insert(node, 12)\n",
|
||||
" nodes[1] = insert(node, 1)\n",
|
||||
" nodes[7] = insert(node, 7)\n",
|
||||
" nodes[10] = insert(node, 10)\n",
|
||||
" nodes[15] = insert(node, 15)\n",
|
||||
" nodes[9] = insert(node, 9)\n",
|
||||
" bst = Bst(nodes[5])\n",
|
||||
" nodes[3] = bst.insert(3)\n",
|
||||
" nodes[8] = bst.insert(8)\n",
|
||||
" nodes[2] = bst.insert(2)\n",
|
||||
" nodes[4] = bst.insert(4)\n",
|
||||
" nodes[6] = bst.insert(6)\n",
|
||||
" nodes[12] = bst.insert(12)\n",
|
||||
" nodes[1] = bst.insert(1)\n",
|
||||
" nodes[7] = bst.insert(7)\n",
|
||||
" nodes[10] = bst.insert(10)\n",
|
||||
" nodes[15] = bst.insert(15)\n",
|
||||
" nodes[9] = bst.insert(9)\n",
|
||||
"\n",
|
||||
" assert_equal(bst_successor(nodes[4]), 5)\n",
|
||||
" assert_equal(bst_successor(nodes[5]), 6)\n",
|
||||
" assert_equal(bst_successor(nodes[8]), 9)\n",
|
||||
" assert_equal(bst_successor(nodes[15]), None)\n",
|
||||
" bst_successor = BstSuccessor()\n",
|
||||
" assert_equal(bst_successor.get_next(nodes[4]), 5)\n",
|
||||
" assert_equal(bst_successor.get_next(nodes[5]), 6)\n",
|
||||
" assert_equal(bst_successor.get_next(nodes[8]), 9)\n",
|
||||
" assert_equal(bst_successor.get_next(nodes[15]), None)\n",
|
||||
"\n",
|
||||
" print('Success: test_bst_successor')\n",
|
||||
"\n",
|
||||
|
|
|
@ -111,29 +111,28 @@
|
|||
},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def left_most_node(node):\n",
|
||||
" if node is None:\n",
|
||||
" return None\n",
|
||||
" if node.left is None:\n",
|
||||
"class BstSuccessor(object):\n",
|
||||
"\n",
|
||||
" def get_next(self, node):\n",
|
||||
" if node is None:\n",
|
||||
" raise Exception('Invalid input node')\n",
|
||||
" if node.right is not None:\n",
|
||||
" return self._left_most(node.right)\n",
|
||||
" return self._next_ancestor(node)\n",
|
||||
"\n",
|
||||
" def _left_most(self, node):\n",
|
||||
" if node.left is not None:\n",
|
||||
" return self._left_most(node.left)\n",
|
||||
" return node.data\n",
|
||||
" return left_most_node(node.left)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
"def bst_successor(node):\n",
|
||||
" if node is None:\n",
|
||||
" raise Exception('Invalid input node')\n",
|
||||
" if node.right is not None:\n",
|
||||
" return left_most_node(node.right)\n",
|
||||
" else:\n",
|
||||
" parent = node.parent\n",
|
||||
" while parent is not None and parent.left is not node:\n",
|
||||
" node = node.parent\n",
|
||||
" parent = node.parent\n",
|
||||
" if parent is None:\n",
|
||||
" # We reached the root, the original input node\n",
|
||||
" # must be the largest element in the tree.\n",
|
||||
" return None\n",
|
||||
" return parent.data"
|
||||
" def _next_ancestor(self, node):\n",
|
||||
" if node.parent is not None:\n",
|
||||
" if node.parent.data > node.data:\n",
|
||||
" return node.parent.data\n",
|
||||
" return self._next_ancestor(node.parent)\n",
|
||||
" # We reached the root, the original input node\n",
|
||||
" # must be the largest element in the tree.\n",
|
||||
" return None"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
@ -168,28 +167,31 @@
|
|||
"\n",
|
||||
" @raises(Exception)\n",
|
||||
" def test_bst_successor_empty(self):\n",
|
||||
" bst_successor(None)\n",
|
||||
" bst_successor = BstSuccessor()\n",
|
||||
" bst_successor.get_next(None)\n",
|
||||
"\n",
|
||||
" def test_bst_successor(self):\n",
|
||||
" nodes = {}\n",
|
||||
" node = Node(5)\n",
|
||||
" nodes[5] = node\n",
|
||||
" nodes[3] = insert(node, 3)\n",
|
||||
" nodes[8] = insert(node, 8)\n",
|
||||
" nodes[2] = insert(node, 2)\n",
|
||||
" nodes[4] = insert(node, 4)\n",
|
||||
" nodes[6] = insert(node, 6)\n",
|
||||
" nodes[12] = insert(node, 12)\n",
|
||||
" nodes[1] = insert(node, 1)\n",
|
||||
" nodes[7] = insert(node, 7)\n",
|
||||
" nodes[10] = insert(node, 10)\n",
|
||||
" nodes[15] = insert(node, 15)\n",
|
||||
" nodes[9] = insert(node, 9)\n",
|
||||
" bst = Bst(nodes[5])\n",
|
||||
" nodes[3] = bst.insert(3)\n",
|
||||
" nodes[8] = bst.insert(8)\n",
|
||||
" nodes[2] = bst.insert(2)\n",
|
||||
" nodes[4] = bst.insert(4)\n",
|
||||
" nodes[6] = bst.insert(6)\n",
|
||||
" nodes[12] = bst.insert(12)\n",
|
||||
" nodes[1] = bst.insert(1)\n",
|
||||
" nodes[7] = bst.insert(7)\n",
|
||||
" nodes[10] = bst.insert(10)\n",
|
||||
" nodes[15] = bst.insert(15)\n",
|
||||
" nodes[9] = bst.insert(9)\n",
|
||||
"\n",
|
||||
" assert_equal(bst_successor(nodes[4]), 5)\n",
|
||||
" assert_equal(bst_successor(nodes[5]), 6)\n",
|
||||
" assert_equal(bst_successor(nodes[8]), 9)\n",
|
||||
" assert_equal(bst_successor(nodes[15]), None)\n",
|
||||
" bst_successor = BstSuccessor()\n",
|
||||
" assert_equal(bst_successor.get_next(nodes[4]), 5)\n",
|
||||
" assert_equal(bst_successor.get_next(nodes[5]), 6)\n",
|
||||
" assert_equal(bst_successor.get_next(nodes[8]), 9)\n",
|
||||
" assert_equal(bst_successor.get_next(nodes[15]), None)\n",
|
||||
"\n",
|
||||
" print('Success: test_bst_successor')\n",
|
||||
"\n",
|
||||
|
|
|
@ -6,28 +6,31 @@ class TestBstSuccessor(object):
|
|||
|
||||
@raises(Exception)
|
||||
def test_bst_successor_empty(self):
|
||||
bst_successor(None)
|
||||
bst_successor = BstSuccessor()
|
||||
bst_successor.get_next(None)
|
||||
|
||||
def test_bst_successor(self):
|
||||
nodes = {}
|
||||
node = Node(5)
|
||||
nodes[5] = node
|
||||
nodes[3] = insert(node, 3)
|
||||
nodes[8] = insert(node, 8)
|
||||
nodes[2] = insert(node, 2)
|
||||
nodes[4] = insert(node, 4)
|
||||
nodes[6] = insert(node, 6)
|
||||
nodes[12] = insert(node, 12)
|
||||
nodes[1] = insert(node, 1)
|
||||
nodes[7] = insert(node, 7)
|
||||
nodes[10] = insert(node, 10)
|
||||
nodes[15] = insert(node, 15)
|
||||
nodes[9] = insert(node, 9)
|
||||
bst = Bst(nodes[5])
|
||||
nodes[3] = bst.insert(3)
|
||||
nodes[8] = bst.insert(8)
|
||||
nodes[2] = bst.insert(2)
|
||||
nodes[4] = bst.insert(4)
|
||||
nodes[6] = bst.insert(6)
|
||||
nodes[12] = bst.insert(12)
|
||||
nodes[1] = bst.insert(1)
|
||||
nodes[7] = bst.insert(7)
|
||||
nodes[10] = bst.insert(10)
|
||||
nodes[15] = bst.insert(15)
|
||||
nodes[9] = bst.insert(9)
|
||||
|
||||
assert_equal(bst_successor(nodes[4]), 5)
|
||||
assert_equal(bst_successor(nodes[5]), 6)
|
||||
assert_equal(bst_successor(nodes[8]), 9)
|
||||
assert_equal(bst_successor(nodes[15]), None)
|
||||
bst_successor = BstSuccessor()
|
||||
assert_equal(bst_successor.get_next(nodes[4]), 5)
|
||||
assert_equal(bst_successor.get_next(nodes[5]), 6)
|
||||
assert_equal(bst_successor.get_next(nodes[8]), 9)
|
||||
assert_equal(bst_successor.get_next(nodes[15]), None)
|
||||
|
||||
print('Success: test_bst_successor')
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user