      "collapsed": false
     },
     "outputs": [],
     "source": [
      "def validate_bst(node):\n",
      "    if node is None:\n",
      "        raise Exception('No root node')\n",
      "    return _validate_bst(node, None, None)\n",
      "\n",
      "\n",
      "def _validate_bst(node, mininum, maximum):\n",
      "    if node is None:\n",
      "        return True\n",
      "    if mininum is not None and node.data <= mininum:\n",
      "        return False\n",
      "    if maximum is not None and node.data > maximum:\n",
      "        return False\n",
      "    if not _validate_bst(node.left, mininum, node.data):\n",
      "        return False\n",
      "    if not _validate_bst(node.right, node.data, maximum):\n",
      "        return False\n",
      "    return True"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
 
</s> remove     "def validate_bst(node):\n",
    "    # TODO: Implement me\n",
    "    pass"
 </s> add     "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        # TODO: Implement me\n",
    "        pass" </s> remove     "        node = Node(5)\n",
    "        insert(node, 8)\n",
    "        insert(node, 5)\n",
    "        insert(node, 6)\n",
    "        insert(node, 4)\n",
    "        insert(node, 7)\n",
    "        assert_equal(validate_bst(node), True)\n",
    "\n",
    "        root = Node(5)\n",
 </s> add     "        bst = BstValidate(Node(5))\n",
    "        bst.insert(8)\n",
    "        bst.insert(5)\n",
    "        bst.insert(6)\n",
    "        bst.insert(4)\n",
    "        bst.insert(7)\n",
    "        assert_equal(bst.validate(), True)\n",
    "\n",
    "        bst = BstValidate(Node(5))\n", </s> remove     "        node = Node(5)\n",
    "        insert(node, 8)\n",
    "        insert(node, 5)\n",
    "        insert(node, 6)\n",
    "        insert(node, 4)\n",
    "        insert(node, 7)\n",
    "        assert_equal(validate_bst(node), True)\n",
    "\n",
    "        root = Node(5)\n",
 </s> add     "        bst = BstValidate(Node(5))\n",
    "        bst.insert(8)\n",
    "        bst.insert(5)\n",
    "        bst.insert(6)\n",
    "        bst.insert(4)\n",
    "        bst.insert(7)\n",
    "        assert_equal(bst.validate(), True)\n",
    "\n",
    "        bst = BstValidate(Node(5))\n", </s> remove     "        root.left = left\n",
    "        root.right = right\n",
    "        root.left.right = invalid\n",
    "        assert_equal(validate_bst(root), False)\n",
 </s> add     "        bst.root.left = left\n",
    "        bst.root.right = right\n",
    "        bst.root.left.right = invalid\n",
    "        assert_equal(bst.validate(), False)\n", </s> remove     "        root.left = left\n",
    "        root.right = right\n",
    "        root.left.right = invalid\n",
    "        assert_equal(validate_bst(root), False)\n",
 </s> add     "        bst.root.left = left\n",
    "        bst.root.right = right\n",
    "        bst.root.left.right = invalid\n",
    "        assert_equal(bst.validate(), False)\n",
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
      "collapsed": false
     },
     "outputs": [],
     "source": [
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
    "import sys\n",
    "\n",
    "\n",
    "class BstValidate(Bst):\n",
    "\n",
    "    def validate(self):\n",
    "        if self.root is None:\n",
    "            raise Exception('No root node')\n",
    "        return self._validate(self.root)\n",
    "\n",
    "    def _validate(self, node, mininum=-sys.maxsize, maximum=sys.maxsize):\n",
    "        if node is None:\n",
    "            return True\n",
    "        if node.data <= mininum or node.data > maximum:\n",
    "            return False\n",
    "        if not self._validate(node.left, mininum, node.data):\n",
    "            return False\n",
    "        if not self._validate(node.right, node.data, maximum):\n",
    "            return False\n",
    "        return True"
     ]
    },
    {
     "cell_type": "markdown",
     "metadata": {},
keep keep keep keep replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace replace keep keep keep keep keep
 Move bst validate to a class (#98) 
graphs_trees/bst_validate/bst_validate_solution.ipynb