From 62393005b29f874473c69a2128229e2533d5b4b7 Mon Sep 17 00:00:00 2001 From: mandakini Date: Wed, 12 Nov 2025 21:40:12 +0530 Subject: [PATCH] Fix incorrect Binary Search explanation in Decrease and Conquer chapter --- .../chapter_decrease_and_conquer.ipynb | 932 +++++++++--------- 1 file changed, 479 insertions(+), 453 deletions(-) diff --git a/Colab_Codes/chapter_decrease_and_conquer.ipynb b/Colab_Codes/chapter_decrease_and_conquer.ipynb index 3fac7d9..d5a7721 100644 --- a/Colab_Codes/chapter_decrease_and_conquer.ipynb +++ b/Colab_Codes/chapter_decrease_and_conquer.ipynb @@ -1,25 +1,10 @@ { - "nbformat": 4, - "nbformat_minor": 0, - "metadata": { - "colab": { - "name": "chapter_decrease_and_conquer.ipynb", - "provenance": [], - "collapsed_sections": [], - "toc_visible": true, - "include_colab_link": true - }, - "kernelspec": { - "name": "python3", - "display_name": "Python 3" - } - }, "cells": [ { "cell_type": "markdown", "metadata": { - "id": "view-in-github", - "colab_type": "text" + "colab_type": "text", + "id": "view-in-github" }, "source": [ "\"Open" @@ -28,8 +13,8 @@ { "cell_type": "markdown", "metadata": { - "id": "hIhy6_m3lvTV", - "colab_type": "text" + "colab_type": "text", + "id": "hIhy6_m3lvTV" }, "source": [ "## Binary Search" @@ -37,24 +22,40 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "HoaV8uZHl1G-", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "HoaV8uZHl1G-" }, + "outputs": [], "source": [ "nums = [1, 3, 4, 6, 7, 8, 10, 13, 14, 18, 19, 21, 24, 37, 40, 45, 71]" - ], - "execution_count": 0, - "outputs": [] + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# **Corrected explanation:**\n", + "\n", + "# - If the middle element is **smaller** than the target → search in the **right half** \n", + "# → `l = mid + 1`\n", + "# - If the middle element is **larger** than the target → search in the **left half** \n", + "# → `r = mid - 1`\n" + ] }, { "cell_type": "code", + "execution_count": 1, "metadata": { - "id": "Q9iK9qbomHqz", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "Q9iK9qbomHqz" }, + "outputs": [], "source": [ "#@title Standard binary search\n", "def standard_binary_search(lst, target):\n", @@ -68,123 +69,123 @@ " else:\n", " r = mid - 1\n", " return -1 # target is not found " - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "3d2wlsFgp7Ge", - "colab_type": "code", - "outputId": "37174701-48d6-4659-b876-b5c54e761808", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "3d2wlsFgp7Ge", + "outputId": "37174701-48d6-4659-b876-b5c54e761808" }, - "source": [ - "# When target exists\n", - "standard_binary_search(nums, 7)" - ], - "execution_count": 0, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "4" ] }, + "execution_count": 4, "metadata": { "tags": [] }, - "execution_count": 4 + "output_type": "execute_result" } + ], + "source": [ + "# When target exists\n", + "standard_binary_search(nums, 7)" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "gsYY1xYYq2b8", - "colab_type": "code", - "outputId": "7939b051-a09a-4acb-95fe-1176acf1992b", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "gsYY1xYYq2b8", + "outputId": "7939b051-a09a-4acb-95fe-1176acf1992b" }, - "source": [ - "# When target does not exist\n", - "standard_binary_search(nums, 42)" - ], - "execution_count": 0, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "-1" ] }, + "execution_count": 5, "metadata": { "tags": [] }, - "execution_count": 5 + "output_type": "execute_result" } + ], + "source": [ + "# When target does not exist\n", + "standard_binary_search(nums, 42)" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "soMDN2h2rBAi", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "soMDN2h2rBAi" }, + "outputs": [], "source": [ "nums = [1, 3, 4, 4, 4, 4, 6, 7, 8]" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "S2xNrvB3rGnH", - "colab_type": "code", - "outputId": "f8e71161-bb9f-4a7c-a835-4d35b478d758", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "S2xNrvB3rGnH", + "outputId": "f8e71161-bb9f-4a7c-a835-4d35b478d758" }, - "source": [ - "# When there exists duplicates of the target\n", - "standard_binary_search(nums, 4)" - ], - "execution_count": 0, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "4" ] }, + "execution_count": 7, "metadata": { "tags": [] }, - "execution_count": 7 + "output_type": "execute_result" } + ], + "source": [ + "# When there exists duplicates of the target\n", + "standard_binary_search(nums, 4)" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "E7xyot8ou6dh", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "E7xyot8ou6dh" }, + "outputs": [], "source": [ "#@title Binary Search with Lower Bound \n", "def lower_bound_bs(nums, t):\n", @@ -197,45 +198,45 @@ " l = mid + 1\n", " return l\n", " " - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "FYrRsAqwwQvl", - "colab_type": "code", - "outputId": "83763de7-f709-4393-b9df-4a21a4a5354e", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "FYrRsAqwwQvl", + "outputId": "83763de7-f709-4393-b9df-4a21a4a5354e" }, - "source": [ - "# Binary Search with lower bound\n", - "l1 = lower_bound_bs(nums, 4)\n", - "l2 = lower_bound_bs(nums, 5)\n", - "print(l1, l2)" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "2 6\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "# Binary Search with lower bound\n", + "l1 = lower_bound_bs(nums, 4)\n", + "l2 = lower_bound_bs(nums, 5)\n", + "print(l1, l2)" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "nGWDrHR12Rtq", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "nGWDrHR12Rtq" }, + "outputs": [], "source": [ "#@title Binary Search with Upper Bound \n", "def upper_bound_bs(nums, t):\n", @@ -248,49 +249,57 @@ " r = mid - 1\n", " return l\n", " " - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "VkCtss6P23RH", - "colab_type": "code", - "outputId": "d94adadf-a113-4de8-f1d0-73521ad65e13", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "VkCtss6P23RH", + "outputId": "d94adadf-a113-4de8-f1d0-73521ad65e13" }, - "source": [ - "# Binary Search with upper bound\n", - "l1 = upper_bound_bs(nums, 4)\n", - "l2 = upper_bound_bs(nums, 5)\n", - "print(l1, l2)" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "6 6\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "# Binary Search with upper bound\n", + "l1 = upper_bound_bs(nums, 4)\n", + "l2 = upper_bound_bs(nums, 5)\n", + "print(l1, l2)" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "Mn8qGzZezFqe", - "colab_type": "code", - "outputId": "6dea4b52-ba92-43e1-a79a-85be699d343d", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "Mn8qGzZezFqe", + "outputId": "6dea4b52-ba92-43e1-a79a-85be699d343d" }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "2 6 6 6\n" + ] + } + ], "source": [ "#@title Use Python Module bisect\n", "from bisect import bisect_left,bisect_right, bisect\n", @@ -300,51 +309,41 @@ "r2 = bisect_right(nums, 5)\n", "p3 = bisect(nums, 5)\n", "print(l1, r1, l2, r2)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "2 6 6 6\n" - ], - "name": "stdout" - } ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "ZYxs9WYC2DUk", - "colab_type": "code", - "outputId": "6cbfb9de-c542-437d-839c-1410ebdfebfd", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "ZYxs9WYC2DUk", + "outputId": "6cbfb9de-c542-437d-839c-1410ebdfebfd" }, - "source": [ - "p1 = bisect_left(nums, 4)\n", - "p2 = bisect_right(nums, 4)\n", - "p3 = bisect(nums, 4)\n", - "print(p1, p2, p3)" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "2 6 6\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "p1 = bisect_left(nums, 4)\n", + "p2 = bisect_right(nums, 4)\n", + "p3 = bisect(nums, 4)\n", + "print(p1, p2, p3)" ] }, { "cell_type": "markdown", "metadata": { - "id": "0Rfi8qZndFdd", - "colab_type": "text" + "colab_type": "text", + "id": "0Rfi8qZndFdd" }, "source": [ "### Applications\n", @@ -355,8 +354,8 @@ { "cell_type": "markdown", "metadata": { - "id": "A6ewzRSkRM5R", - "colab_type": "text" + "colab_type": "text", + "id": "A6ewzRSkRM5R" }, "source": [ "#### Rotated Array" @@ -364,11 +363,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "V_TzJ-H_kHTZ", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "V_TzJ-H_kHTZ" }, + "outputs": [], "source": [ "# First bad Version\n", "def firstBadVersion(self, n):\n", @@ -381,17 +382,17 @@ " l = mid + 1 \n", " return l\n", " " - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "310ELp0zcyWm", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "310ELp0zcyWm" }, + "outputs": [], "source": [ "def RotatedBinarySearch(nums, t): \n", " l, r = 0, len(nums)-1\n", @@ -415,46 +416,44 @@ " else: \n", " l = mid + 1\n", " return -1" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "abgMk_jNfSPV", - "colab_type": "code", - "outputId": "8dce0c66-850b-405a-dcf9-c031b5a98d2a", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "abgMk_jNfSPV", + "outputId": "8dce0c66-850b-405a-dcf9-c031b5a98d2a" }, - "source": [ - "nums = [7,0,1,2,3,4,5,6]\n", - "RotatedBinarySearch(nums, 3)" - ], - "execution_count": 0, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "4" ] }, + "execution_count": 16, "metadata": { "tags": [] }, - "execution_count": 16 + "output_type": "execute_result" } + ], + "source": [ + "nums = [7,0,1,2,3,4,5,6]\n", + "RotatedBinarySearch(nums, 3)" ] }, { "cell_type": "markdown", "metadata": { - "id": "DYQbczWuQ-UM", - "colab_type": "text" + "colab_type": "text", + "id": "DYQbczWuQ-UM" }, "source": [ "#### Binary Search to Solve Math Problem" @@ -462,56 +461,58 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "6nq9BPxuRSI2", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "6nq9BPxuRSI2" }, + "outputs": [], "source": [ "import math\n", "def arrangeCoins(n: int) -> int:\n", " return int((math.sqrt(1+8*n)-1) // 2)" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "hF5v8wnARdAR", - "colab_type": "code", - "outputId": "6431df36-a202-4e41-aeec-1f5246592d7b", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "hF5v8wnARdAR", + "outputId": "6431df36-a202-4e41-aeec-1f5246592d7b" }, - "source": [ - "arrangeCoins(8)" - ], - "execution_count": 0, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, + "execution_count": 18, "metadata": { "tags": [] }, - "execution_count": 18 + "output_type": "execute_result" } + ], + "source": [ + "arrangeCoins(8)" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "8GAML-APRt4m", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "8GAML-APRt4m" }, + "outputs": [], "source": [ "# Use Binary Search\n", "def arrangeCoins(n):\n", @@ -530,45 +531,43 @@ " return l\n", " return bisect_right() - 1\n", " " - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "cf1D07VYUYch", - "colab_type": "code", - "outputId": "98316869-95ad-4cef-8acb-790e05764cf1", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "cf1D07VYUYch", + "outputId": "98316869-95ad-4cef-8acb-790e05764cf1" }, - "source": [ - "arrangeCoins(8)" - ], - "execution_count": 0, "outputs": [ { - "output_type": "execute_result", "data": { "text/plain": [ "3" ] }, + "execution_count": 20, "metadata": { "tags": [] }, - "execution_count": 20 + "output_type": "execute_result" } + ], + "source": [ + "arrangeCoins(8)" ] }, { "cell_type": "markdown", "metadata": { - "id": "aVJePJSD3e16", - "colab_type": "text" + "colab_type": "text", + "id": "aVJePJSD3e16" }, "source": [ "## Binary Search Tree" @@ -576,11 +575,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "bi9eVUn93YcR", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "bi9eVUn93YcR" }, + "outputs": [], "source": [ "#@title Binary Tree Node\n", "class BiNode:\n", @@ -588,17 +589,17 @@ " self.left = None\n", " self.right = None\n", " self.val = val" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "23mTR1N2wf4y", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "23mTR1N2wf4y" }, + "outputs": [], "source": [ "# A helper function to print out the tree in order\n", "'''\n", @@ -610,15 +611,13 @@ " yield from inorder_print(root.left)\n", " yield root.val\n", " yield from inorder_print(root.right)" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "id": "rh4tS_qnunWL", - "colab_type": "text" + "colab_type": "text", + "id": "rh4tS_qnunWL" }, "source": [ "### Search" @@ -626,11 +625,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "uPKnMRSAtX5d", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "uPKnMRSAtX5d" }, + "outputs": [], "source": [ "#@title Recursive Search\n", "def search(root, t):\n", @@ -642,17 +643,17 @@ " return search(root.left, t)\n", " else:\n", " return search(root.right, t)\n" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "yC6VSC6Gt6Rp", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "yC6VSC6Gt6Rp" }, + "outputs": [], "source": [ "#@title Iterative Search\n", "def searchItr(root, t):\n", @@ -664,15 +665,13 @@ " else:\n", " root = root.right\n", " return None" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "id": "Ps9yX8EgXGp2", - "colab_type": "text" + "colab_type": "text", + "id": "Ps9yX8EgXGp2" }, "source": [ "### Minimum and Maximum Node" @@ -680,11 +679,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "93wNIzgDXFdN", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "93wNIzgDXFdN" }, + "outputs": [], "source": [ "# minimum recursive\n", "def minimum(root):\n", @@ -693,17 +694,17 @@ " if not root.left:\n", " return root\n", " return minimum(root.left)" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "YKk-aYiZXej7", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "YKk-aYiZXej7" }, + "outputs": [], "source": [ "# minimum iterative\n", "def minimumIter(root):\n", @@ -712,17 +713,17 @@ " return root\n", " root = root.left\n", " return None" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "eb-sC2VbYa4k", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "eb-sC2VbYa4k" }, + "outputs": [], "source": [ "# maximum recursive\n", "def maximum(root):\n", @@ -731,17 +732,17 @@ " if not root.right:\n", " return root\n", " return maximum(root.right)" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "5NLBVCo-YfcI", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "5NLBVCo-YfcI" }, + "outputs": [], "source": [ "# maximum iterative\n", "def maximumIter(root):\n", @@ -750,15 +751,13 @@ " return root\n", " root = root.right\n", " return None" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "id": "vRjlkcWbfuJ4", - "colab_type": "text" + "colab_type": "text", + "id": "vRjlkcWbfuJ4" }, "source": [ "### Predecessor and Successor" @@ -766,11 +765,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "JUS3EHJ_Rb8C", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "JUS3EHJ_Rb8C" }, + "outputs": [], "source": [ "# Successor found with inorder\n", "def successorInorder(root, node):\n", @@ -789,17 +790,17 @@ " else:\n", " break\n", " return succ" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "LJSGRwcpfy_x", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "LJSGRwcpfy_x" }, + "outputs": [], "source": [ "def reverse(node):\n", " if not node or not node.p:\n", @@ -839,17 +840,17 @@ " \n", " root.p = None\n", " return helper(root, t)" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "9EYUc_lwoKSi", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "9EYUc_lwoKSi" }, + "outputs": [], "source": [ "# Separate the above code into two steps\n", "def findNodeAddParent(root, t):\n", @@ -872,15 +873,13 @@ " return minimum(root.right)\n", " else:\n", " return reverse(root) " - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "id": "9mwSazjPUEEN", - "colab_type": "text" + "colab_type": "text", + "id": "9mwSazjPUEEN" }, "source": [ "Predecessor" @@ -888,11 +887,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "AOinBcdcUGRG", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "AOinBcdcUGRG" }, + "outputs": [], "source": [ "def reverse_right(node):\n", " if not node or not node.p:\n", @@ -909,17 +910,17 @@ " return maximum(root.left)\n", " else:\n", " return reverse_right(root) " - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "nRiIVBZsUucH", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "nRiIVBZsUucH" }, + "outputs": [], "source": [ "# predecessor inorder\n", "def predecessorInorder(root, node):\n", @@ -938,15 +939,13 @@ " else:\n", " break\n", " return pred" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "id": "t2ATJStuvPvu", - "colab_type": "text" + "colab_type": "text", + "id": "t2ATJStuvPvu" }, "source": [ "### Insert" @@ -954,11 +953,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "9HdUHb-yvbO3", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "9HdUHb-yvbO3" }, + "outputs": [], "source": [ "# Clean Recursive Insert\n", "def insert(root, t):\n", @@ -972,17 +973,17 @@ " else:\n", " root.right = insert(root.right, t) \n", " return root" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "Cqb911P_1ocX", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "Cqb911P_1ocX" }, + "outputs": [], "source": [ "# Recursive Insert 2\n", "def insert2(root, t):\n", @@ -1000,17 +1001,17 @@ " root.right = BiNode(t)\n", " else:\n", " insert2(root.right, t) " - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "QN6MwXa16jUI", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "QN6MwXa16jUI" }, + "outputs": [], "source": [ "# Iterative insertion\n", "def insertItr(root, t):\n", @@ -1035,15 +1036,13 @@ " p.left = BiNode(t)\n", " return root\n", " " - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "markdown", "metadata": { - "id": "BJUGXsoBYI5O", - "colab_type": "text" + "colab_type": "text", + "id": "BJUGXsoBYI5O" }, "source": [ "### Delete" @@ -1051,11 +1050,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "nRVCru44YORz", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "nRVCru44YORz" }, + "outputs": [], "source": [ "def deleteMinimum(root):\n", " if not root:\n", @@ -1103,178 +1104,188 @@ " root.left = delete(root.left, t)\n", " return root\n", " \n" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "8pAODLP1ktWy", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "8pAODLP1ktWy" }, + "outputs": [], "source": [ "keys = [8, 3, 10, 1, 6, 14, 4, 7, 13]" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "xW555Xb6wCb4", - "colab_type": "code", - "outputId": "127d98c4-453d-4873-f714-8ddaec9356ff", "colab": { "base_uri": "https://localhost:8080/", "height": 54 - } + }, + "colab_type": "code", + "id": "xW555Xb6wCb4", + "outputId": "127d98c4-453d-4873-f714-8ddaec9356ff" }, - "source": [ - "# Construct the examplary tree\n", - "%%time\n", - "root = None\n", - "for k in keys:\n", - " root = insert(root, k)" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 21 µs, sys: 4 µs, total: 25 µs\n", "Wall time: 28.8 µs\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "# Construct the examplary tree\n", + "%%time\n", + "root = None\n", + "for k in keys:\n", + " root = insert(root, k)" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "TL9-6VMI2Ldn", - "colab_type": "code", - "outputId": "e206db54-d2e4-46fe-a5ef-33c8c40f9346", "colab": { "base_uri": "https://localhost:8080/", "height": 54 - } + }, + "colab_type": "code", + "id": "TL9-6VMI2Ldn", + "outputId": "e206db54-d2e4-46fe-a5ef-33c8c40f9346" }, - "source": [ - "# Construct the examplary tree\n", - "%%time\n", - "root = BiNode(keys[0])\n", - "for k in keys[1:]:\n", - " insert2(root, k)" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 54 µs, sys: 0 ns, total: 54 µs\n", "Wall time: 60.6 µs\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "# Construct the examplary tree\n", + "%%time\n", + "root = BiNode(keys[0])\n", + "for k in keys[1:]:\n", + " insert2(root, k)" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "NoBJCgvF7WVo", - "colab_type": "code", - "outputId": "2905fd04-9de0-4e1b-d809-562698194c57", "colab": { "base_uri": "https://localhost:8080/", "height": 54 - } + }, + "colab_type": "code", + "id": "NoBJCgvF7WVo", + "outputId": "2905fd04-9de0-4e1b-d809-562698194c57" }, - "source": [ - "# Construct the examplary tree\n", - "%%time\n", - "root = BiNode(keys[0])\n", - "for k in keys:\n", - " root = insertItr(root, k)" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "CPU times: user 49 µs, sys: 0 ns, total: 49 µs\n", "Wall time: 103 µs\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "# Construct the examplary tree\n", + "%%time\n", + "root = BiNode(keys[0])\n", + "for k in keys:\n", + " root = insertItr(root, k)" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "vsV8Y2vkwbDI", - "colab_type": "code", - "outputId": "d7491e7c-606c-4d0e-9cf0-d4424d7cf3fd", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "vsV8Y2vkwbDI", + "outputId": "d7491e7c-606c-4d0e-9cf0-d4424d7cf3fd" }, - "source": [ - "# insert key 9 \n", - "out_keys = inorder_print(root)\n", - "for k in out_keys:\n", - " print(k, end = ' ')" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "1 3 4 6 7 8 10 13 14 " - ], - "name": "stdout" + ] } + ], + "source": [ + "# insert key 9 \n", + "out_keys = inorder_print(root)\n", + "for k in out_keys:\n", + " print(k, end = ' ')" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "nRzeShLMXuIP", - "colab_type": "code", - "outputId": "ab5374c1-af90-4292-9e84-2688ce6d6674", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "nRzeShLMXuIP", + "outputId": "ab5374c1-af90-4292-9e84-2688ce6d6674" }, - "source": [ - "## Test Minimum and maximum\n", - "print(minimum(root).val, minimumIter(root).val,maximum(root).val, maximumIter(root).val)" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "1 1 14 14\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "## Test Minimum and maximum\n", + "print(minimum(root).val, minimumIter(root).val,maximum(root).val, maximumIter(root).val)" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "oJONUENtl654", - "colab_type": "code", - "outputId": "fc34abec-bc1d-408e-be64-c1a9c51edf35", "colab": { "base_uri": "https://localhost:8080/", "height": 72 - } + }, + "colab_type": "code", + "id": "oJONUENtl654", + "outputId": "fc34abec-bc1d-408e-be64-c1a9c51edf35" }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "4 6 8\n", + "6\n", + "4 7\n" + ] + } + ], "source": [ "# Test successor and predecessor\n", "s1 = successor(root, 14)\n", @@ -1289,31 +1300,30 @@ "suc = successor2(node)\n", "print(suc.val)\n", "print(predecessorInorder(root, suc).val, successorInorder(root, suc).val)" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "4 6 8\n", - "6\n", - "4 7\n" - ], - "name": "stdout" - } ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "Ee9SU288b91o", - "colab_type": "code", - "outputId": "7bc7bdd8-e5ec-4c75-a631-a4950bb80b00", "colab": { "base_uri": "https://localhost:8080/", "height": 54 - } + }, + "colab_type": "code", + "id": "Ee9SU288b91o", + "outputId": "7bc7bdd8-e5ec-4c75-a631-a4950bb80b00" }, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "1 3 4 6 7 8 10 13 14 ,\n", + "1 4 6 7 8 10 13 14 " + ] + } + ], "source": [ "# Test Delete\n", "out_keys = inorder_print(root)\n", @@ -1324,24 +1334,13 @@ "out_keys = inorder_print(root1)\n", "for k in out_keys:\n", " print(k, end = ' ')\n" - ], - "execution_count": 0, - "outputs": [ - { - "output_type": "stream", - "text": [ - "1 3 4 6 7 8 10 13 14 ,\n", - "1 4 6 7 8 10 13 14 " - ], - "name": "stdout" - } ] }, { "cell_type": "markdown", "metadata": { - "id": "muAC9qyz3sXJ", - "colab_type": "text" + "colab_type": "text", + "id": "muAC9qyz3sXJ" }, "source": [ "## Segment Tree" @@ -1349,11 +1348,13 @@ }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "weAxbsyJ3rrO", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "weAxbsyJ3rrO" }, + "outputs": [], "source": [ "class TreeNode:\n", " def __init__(self, val, s, e):\n", @@ -1362,17 +1363,17 @@ " self.e = e\n", " self.left = None\n", " self.right = None" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "DWTvjBm06N7N", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "DWTvjBm06N7N" }, + "outputs": [], "source": [ "def getNodes(root):\n", " if not root:\n", @@ -1380,17 +1381,17 @@ " left = getNodes(root.left)\n", " right = getNodes(root.right)\n", " return left + [(root.s, root.e, root.val)] + right" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "3tPVDCeE4MFX", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "3tPVDCeE4MFX" }, + "outputs": [], "source": [ "def merge(left, right, s, e):\n", " return TreeNode(left.val + right.val, s, e)\n", @@ -1414,17 +1415,17 @@ " node.left = left\n", " node.right = right\n", " return node" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "_11QSpH8901s", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "_11QSpH8901s" }, + "outputs": [], "source": [ "# Range Query\n", "def _rangeQuery(root, i, j, s, e): \n", @@ -1437,17 +1438,17 @@ " return _rangeQuery(root.right, i, j, m+1, e)\n", " else:\n", " return _rangeQuery(root.left, i, m, s, m) + _rangeQuery(root.right, m+1, j, m+1, e)" - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "aAc6LT911CDs", + "colab": {}, "colab_type": "code", - "colab": {} + "id": "aAc6LT911CDs" }, + "outputs": [], "source": [ "# Update\n", "def _update(root, s, e, i, val):\n", @@ -1461,90 +1462,115 @@ " _update(root.right, m + 1, e, i, val)\n", " root.val = root.left.val + root.right.val\n", " return " - ], - "execution_count": 0, - "outputs": [] + ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "YaBuyWTr5WHH", - "colab_type": "code", - "outputId": "450f71db-3588-47cf-d556-46f2436a735c", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "YaBuyWTr5WHH", + "outputId": "450f71db-3588-47cf-d556-46f2436a735c" }, - "source": [ - "# Test tree construction\n", - "nums = [2, 9, 4, 5, 8, 7]\n", - "root = _buildSegmentTree(nums, 0, len(nums) - 1)\n", - "print(getNodes(root))" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "[(0, 0, 2), (0, 1, 11), (1, 1, 9), (0, 2, 15), (2, 2, 4), (0, 5, 35), (3, 3, 5), (3, 4, 13), (4, 4, 8), (3, 5, 20), (5, 5, 7)]\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "# Test tree construction\n", + "nums = [2, 9, 4, 5, 8, 7]\n", + "root = _buildSegmentTree(nums, 0, len(nums) - 1)\n", + "print(getNodes(root))" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "mEzfRgUS-UH4", - "colab_type": "code", - "outputId": "a0f13949-6215-49fc-ceae-b6cfdc500788", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "mEzfRgUS-UH4", + "outputId": "a0f13949-6215-49fc-ceae-b6cfdc500788" }, - "source": [ - "# Test Range Query\n", - "print(_rangeQuery(root, 0, 2, 0, len(nums) - 1))" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "15\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "# Test Range Query\n", + "print(_rangeQuery(root, 0, 2, 0, len(nums) - 1))" ] }, { "cell_type": "code", + "execution_count": 0, "metadata": { - "id": "sqy_bZWr2AAw", - "colab_type": "code", - "outputId": "10c13b8d-9c44-41d3-e0aa-3f0caff4bb3e", "colab": { "base_uri": "https://localhost:8080/", "height": 35 - } + }, + "colab_type": "code", + "id": "sqy_bZWr2AAw", + "outputId": "10c13b8d-9c44-41d3-e0aa-3f0caff4bb3e" }, - "source": [ - "# Test updates\n", - "_update(root, 0, len(nums) - 1, 1, 3)\n", - "print(_rangeQuery(root, 0, 2, 0, len(nums) - 1))" - ], - "execution_count": 0, "outputs": [ { + "name": "stdout", "output_type": "stream", "text": [ "9\n" - ], - "name": "stdout" + ] } + ], + "source": [ + "# Test updates\n", + "_update(root, 0, len(nums) - 1, 1, 3)\n", + "print(_rangeQuery(root, 0, 2, 0, len(nums) - 1))" ] } - ] -} \ No newline at end of file + ], + "metadata": { + "colab": { + "collapsed_sections": [], + "include_colab_link": true, + "name": "chapter_decrease_and_conquer.ipynb", + "provenance": [], + "toc_visible": true + }, + "kernelspec": { + "display_name": "Python 3", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.13.9" + } + }, + "nbformat": 4, + "nbformat_minor": 0 +}