[{"data":1,"prerenderedAt":4632},["ShallowReactive",2],{"post-algorithms-working-with-trees":3,"rel-algorithms-working-with-trees":1305,"sib-algorithms-working-with-trees":4556},{"_path":4,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":8,"description":9,"date":10,"layout":11,"subtitle":9,"image":12,"optimized_image":12,"category":13,"tags":14,"author":18,"paginate":6,"body":19,"_type":1299,"_id":1300,"_source":1301,"_file":1302,"_stem":1303,"_extension":1304},"/posts/algorithms-working-with-trees","posts",false,"","Algorithms - Binary tree traversals in kotlin","A simple walkthrough of binary tree traversals algorithms with kotlin examples","2020-11-24T11:00:00.000Z","post","/assets/img/uploads/rubiks-cube.jpg","algorithms",[15,16,17,13],"code","kotlin","binary-tree","jaimedearcos",{"type":20,"children":21,"toc":1292},"root",[22,31,56,61,105,111,116,181,188,200,208,352,360,539,547,699,707,861,867,874,879,910,1274,1286],{"type":23,"tag":24,"props":25,"children":27},"element","h2",{"id":26},"traversals",[28],{"type":29,"value":30},"text","Traversals :",{"type":23,"tag":32,"props":33,"children":34},"blockquote",{},[35],{"type":23,"tag":36,"props":37,"children":38},"p",{},[39,45,47],{"type":23,"tag":40,"props":41,"children":42},"em",{},[43],{"type":29,"value":44},"\"Tree traversal refers to the process of visiting (checking and/or updating) each node in a tree data structure,\nexactly once. Such traversals are classified by the order in which the nodes are visited.\"",{"type":29,"value":46}," -— ",{"type":23,"tag":48,"props":49,"children":53},"a",{"href":50,"rel":51},"https://en.wikipedia.org/wiki/Tree_traversal",[52],"nofollow",[54],{"type":29,"value":55},"Wikipedia",{"type":23,"tag":36,"props":57,"children":58},{},[59],{"type":29,"value":60},"There are 2 options:",{"type":23,"tag":62,"props":63,"children":64},"ul",{},[65,83],{"type":23,"tag":66,"props":67,"children":68},"li",{},[69,75,77],{"type":23,"tag":70,"props":71,"children":72},"strong",{},[73],{"type":29,"value":74},"DFS",{"type":29,"value":76}," : Depth-First Search _“is an algorithm for traversing or searching tree data structure. One starts at the\nroot and explores as far as possible along each branch before backtracking.” -— ",{"type":23,"tag":48,"props":78,"children":81},{"href":79,"rel":80},"https://en.wikipedia.org/wiki/Depth-first_search",[52],[82],{"type":29,"value":55},{"type":23,"tag":66,"props":84,"children":85},{},[86,91,93,98,99],{"type":23,"tag":70,"props":87,"children":88},{},[89],{"type":29,"value":90},"BFS",{"type":29,"value":92}," : Breadth-First Search ",{"type":23,"tag":40,"props":94,"children":95},{},[96],{"type":29,"value":97},"“is an algorithm for traversing or searching tree data structure. It starts at the tree root and explores the\nneighbor nodes first, before moving to the next level neighbors.\"",{"type":29,"value":46},{"type":23,"tag":48,"props":100,"children":103},{"href":101,"rel":102},"https://en.wikipedia.org/wiki/Breadth-first_search",[52],[104],{"type":29,"value":55},{"type":23,"tag":24,"props":106,"children":108},{"id":107},"dfs-depth-first-search",[109],{"type":29,"value":110},"DFS - Depth-First Search",{"type":23,"tag":36,"props":112,"children":113},{},[114],{"type":29,"value":115},"DFS algorithms start from the root and goes deep as possible until they find a leaf before backtracking and exploring another\npath. The main 3 DFS types are:",{"type":23,"tag":62,"props":117,"children":118},{},[119,141,161],{"type":23,"tag":66,"props":120,"children":121},{},[122,127,129,134,136],{"type":23,"tag":70,"props":123,"children":124},{},[125],{"type":29,"value":126},"Pre-Order",{"type":29,"value":128},": ",{"type":23,"tag":40,"props":130,"children":131},{},[132],{"type":29,"value":133},"(NLR)",{"type":29,"value":135}," process node data, left child and finally right child\n",{"type":23,"tag":137,"props":138,"children":140},"img",{"alt":7,"src":139},"https://jaimedearcos-resources.s3-eu-west-1.amazonaws.com/blog/pre-order.gif",[],{"type":23,"tag":66,"props":142,"children":143},{},[144,149,150,155,157],{"type":23,"tag":70,"props":145,"children":146},{},[147],{"type":29,"value":148},"In-Order",{"type":29,"value":128},{"type":23,"tag":40,"props":151,"children":152},{},[153],{"type":29,"value":154},"(LNR)",{"type":29,"value":156}," process left child, node data and finally right child\n",{"type":23,"tag":137,"props":158,"children":160},{"alt":7,"src":159},"https://jaimedearcos-resources.s3-eu-west-1.amazonaws.com/blog/in-order.gif",[],{"type":23,"tag":66,"props":162,"children":163},{},[164,169,170,175,177],{"type":23,"tag":70,"props":165,"children":166},{},[167],{"type":29,"value":168},"Post-Order",{"type":29,"value":128},{"type":23,"tag":40,"props":171,"children":172},{},[173],{"type":29,"value":174},"(LRN)",{"type":29,"value":176}," process left child, right child and finally node data\n",{"type":23,"tag":137,"props":178,"children":180},{"alt":7,"src":179},"https://jaimedearcos-resources.s3-eu-west-1.amazonaws.com/blog/post-order.gif",[],{"type":23,"tag":182,"props":183,"children":185},"h3",{"id":184},"kotlin-examples",[186],{"type":29,"value":187},"Kotlin Examples:",{"type":23,"tag":36,"props":189,"children":190},{},[191,193,198],{"type":29,"value":192},"Usually the DFS algorithms are ease to implement with ",{"type":23,"tag":70,"props":194,"children":195},{},[196],{"type":29,"value":197},"recursion",{"type":29,"value":199},".",{"type":23,"tag":36,"props":201,"children":202},{},[203],{"type":23,"tag":70,"props":204,"children":205},{},[206],{"type":29,"value":207},"Binary tree",{"type":23,"tag":209,"props":210,"children":213},"pre",{"className":211,"code":212,"language":16,"meta":7,"style":7},"language-kotlin shiki shiki-themes github-dark github-light","data class BinaryTreeNode(\n        val data:Int,\n        val left:BinaryTreeNode? = null,\n        val right:BinaryTreeNode? = null\n)\n",[214],{"type":23,"tag":15,"props":215,"children":216},{"__ignoreMap":7},[217,246,275,313,343],{"type":23,"tag":218,"props":219,"children":222},"span",{"class":220,"line":221},"line",1,[223,229,234,240],{"type":23,"tag":218,"props":224,"children":226},{"style":225},"--shiki-default:#F97583;--shiki-light:#D73A49",[227],{"type":29,"value":228},"data",{"type":23,"tag":218,"props":230,"children":231},{"style":225},[232],{"type":29,"value":233}," class",{"type":23,"tag":218,"props":235,"children":237},{"style":236},"--shiki-default:#B392F0;--shiki-light:#6F42C1",[238],{"type":29,"value":239}," BinaryTreeNode",{"type":23,"tag":218,"props":241,"children":243},{"style":242},"--shiki-default:#E1E4E8;--shiki-light:#24292E",[244],{"type":29,"value":245},"(\n",{"type":23,"tag":218,"props":247,"children":249},{"class":220,"line":248},2,[250,255,260,265,270],{"type":23,"tag":218,"props":251,"children":252},{"style":225},[253],{"type":29,"value":254},"        val",{"type":23,"tag":218,"props":256,"children":257},{"style":225},[258],{"type":29,"value":259}," data",{"type":23,"tag":218,"props":261,"children":262},{"style":242},[263],{"type":29,"value":264},":",{"type":23,"tag":218,"props":266,"children":267},{"style":236},[268],{"type":29,"value":269},"Int",{"type":23,"tag":218,"props":271,"children":272},{"style":242},[273],{"type":29,"value":274},",\n",{"type":23,"tag":218,"props":276,"children":278},{"class":220,"line":277},3,[279,283,288,293,298,303,309],{"type":23,"tag":218,"props":280,"children":281},{"style":225},[282],{"type":29,"value":254},{"type":23,"tag":218,"props":284,"children":285},{"style":242},[286],{"type":29,"value":287}," left:",{"type":23,"tag":218,"props":289,"children":290},{"style":236},[291],{"type":29,"value":292},"BinaryTreeNode",{"type":23,"tag":218,"props":294,"children":295},{"style":242},[296],{"type":29,"value":297},"? ",{"type":23,"tag":218,"props":299,"children":300},{"style":225},[301],{"type":29,"value":302},"=",{"type":23,"tag":218,"props":304,"children":306},{"style":305},"--shiki-default:#79B8FF;--shiki-light:#005CC5",[307],{"type":29,"value":308}," null",{"type":23,"tag":218,"props":310,"children":311},{"style":242},[312],{"type":29,"value":274},{"type":23,"tag":218,"props":314,"children":316},{"class":220,"line":315},4,[317,321,326,330,334,338],{"type":23,"tag":218,"props":318,"children":319},{"style":225},[320],{"type":29,"value":254},{"type":23,"tag":218,"props":322,"children":323},{"style":242},[324],{"type":29,"value":325}," right:",{"type":23,"tag":218,"props":327,"children":328},{"style":236},[329],{"type":29,"value":292},{"type":23,"tag":218,"props":331,"children":332},{"style":242},[333],{"type":29,"value":297},{"type":23,"tag":218,"props":335,"children":336},{"style":225},[337],{"type":29,"value":302},{"type":23,"tag":218,"props":339,"children":340},{"style":305},[341],{"type":29,"value":342}," null\n",{"type":23,"tag":218,"props":344,"children":346},{"class":220,"line":345},5,[347],{"type":23,"tag":218,"props":348,"children":349},{"style":242},[350],{"type":29,"value":351},")\n",{"type":23,"tag":36,"props":353,"children":354},{},[355],{"type":23,"tag":70,"props":356,"children":357},{},[358],{"type":29,"value":359},"Preorder traversal",{"type":23,"tag":209,"props":361,"children":363},{"className":211,"code":362,"language":16,"meta":7,"style":7},"fun preOrderTraversal(node: BinaryTreeNode?, \n                      operation: (BinaryTreeNode) -> Unit){\n    if (node == null) return;\n    // Visit node\n    operation.invoke(node)\n    // Traverse left\n    preOrderTraversal(node.left,operation);\n    // Traverse right\n    preOrderTraversal(node.right,operation);\n}\n",[364],{"type":23,"tag":15,"props":365,"children":366},{"__ignoreMap":7},[367,394,421,458,467,485,494,508,517,530],{"type":23,"tag":218,"props":368,"children":369},{"class":220,"line":221},[370,375,380,385,389],{"type":23,"tag":218,"props":371,"children":372},{"style":225},[373],{"type":29,"value":374},"fun",{"type":23,"tag":218,"props":376,"children":377},{"style":236},[378],{"type":29,"value":379}," preOrderTraversal",{"type":23,"tag":218,"props":381,"children":382},{"style":242},[383],{"type":29,"value":384},"(node: ",{"type":23,"tag":218,"props":386,"children":387},{"style":236},[388],{"type":29,"value":292},{"type":23,"tag":218,"props":390,"children":391},{"style":242},[392],{"type":29,"value":393},"?, \n",{"type":23,"tag":218,"props":395,"children":396},{"class":220,"line":248},[397,402,406,411,416],{"type":23,"tag":218,"props":398,"children":399},{"style":242},[400],{"type":29,"value":401},"                      operation: (",{"type":23,"tag":218,"props":403,"children":404},{"style":236},[405],{"type":29,"value":292},{"type":23,"tag":218,"props":407,"children":408},{"style":242},[409],{"type":29,"value":410},") -> ",{"type":23,"tag":218,"props":412,"children":413},{"style":236},[414],{"type":29,"value":415},"Unit",{"type":23,"tag":218,"props":417,"children":418},{"style":242},[419],{"type":29,"value":420},"){\n",{"type":23,"tag":218,"props":422,"children":423},{"class":220,"line":277},[424,429,434,439,443,448,453],{"type":23,"tag":218,"props":425,"children":426},{"style":225},[427],{"type":29,"value":428},"    if",{"type":23,"tag":218,"props":430,"children":431},{"style":242},[432],{"type":29,"value":433}," (node ",{"type":23,"tag":218,"props":435,"children":436},{"style":225},[437],{"type":29,"value":438},"==",{"type":23,"tag":218,"props":440,"children":441},{"style":305},[442],{"type":29,"value":308},{"type":23,"tag":218,"props":444,"children":445},{"style":242},[446],{"type":29,"value":447},") ",{"type":23,"tag":218,"props":449,"children":450},{"style":225},[451],{"type":29,"value":452},"return",{"type":23,"tag":218,"props":454,"children":455},{"style":242},[456],{"type":29,"value":457},";\n",{"type":23,"tag":218,"props":459,"children":460},{"class":220,"line":315},[461],{"type":23,"tag":218,"props":462,"children":464},{"style":463},"--shiki-default:#6A737D;--shiki-light:#6A737D",[465],{"type":29,"value":466},"    // Visit node\n",{"type":23,"tag":218,"props":468,"children":469},{"class":220,"line":345},[470,475,480],{"type":23,"tag":218,"props":471,"children":472},{"style":242},[473],{"type":29,"value":474},"    operation.",{"type":23,"tag":218,"props":476,"children":477},{"style":236},[478],{"type":29,"value":479},"invoke",{"type":23,"tag":218,"props":481,"children":482},{"style":242},[483],{"type":29,"value":484},"(node)\n",{"type":23,"tag":218,"props":486,"children":488},{"class":220,"line":487},6,[489],{"type":23,"tag":218,"props":490,"children":491},{"style":463},[492],{"type":29,"value":493},"    // Traverse left\n",{"type":23,"tag":218,"props":495,"children":497},{"class":220,"line":496},7,[498,503],{"type":23,"tag":218,"props":499,"children":500},{"style":236},[501],{"type":29,"value":502},"    preOrderTraversal",{"type":23,"tag":218,"props":504,"children":505},{"style":242},[506],{"type":29,"value":507},"(node.left,operation);\n",{"type":23,"tag":218,"props":509,"children":511},{"class":220,"line":510},8,[512],{"type":23,"tag":218,"props":513,"children":514},{"style":463},[515],{"type":29,"value":516},"    // Traverse right\n",{"type":23,"tag":218,"props":518,"children":520},{"class":220,"line":519},9,[521,525],{"type":23,"tag":218,"props":522,"children":523},{"style":236},[524],{"type":29,"value":502},{"type":23,"tag":218,"props":526,"children":527},{"style":242},[528],{"type":29,"value":529},"(node.right,operation);\n",{"type":23,"tag":218,"props":531,"children":533},{"class":220,"line":532},10,[534],{"type":23,"tag":218,"props":535,"children":536},{"style":242},[537],{"type":29,"value":538},"}\n",{"type":23,"tag":36,"props":540,"children":541},{},[542],{"type":23,"tag":70,"props":543,"children":544},{},[545],{"type":29,"value":546},"InOrder traversal",{"type":23,"tag":209,"props":548,"children":550},{"className":211,"code":549,"language":16,"meta":7,"style":7},"fun inOrderTraversal(node: BinaryTreeNode?, \n                     operation: (BinaryTreeNode) -> Unit){\n    if (node == null) return;\n    // Traverse left\n    inOrderTraversal(node.left,operation);\n    // Visit node\n    operation.invoke(node)\n    // Traverse right\n    inOrderTraversal(node.right,operation);\n}\n",[551],{"type":23,"tag":15,"props":552,"children":553},{"__ignoreMap":7},[554,578,602,633,640,652,659,674,681,692],{"type":23,"tag":218,"props":555,"children":556},{"class":220,"line":221},[557,561,566,570,574],{"type":23,"tag":218,"props":558,"children":559},{"style":225},[560],{"type":29,"value":374},{"type":23,"tag":218,"props":562,"children":563},{"style":236},[564],{"type":29,"value":565}," inOrderTraversal",{"type":23,"tag":218,"props":567,"children":568},{"style":242},[569],{"type":29,"value":384},{"type":23,"tag":218,"props":571,"children":572},{"style":236},[573],{"type":29,"value":292},{"type":23,"tag":218,"props":575,"children":576},{"style":242},[577],{"type":29,"value":393},{"type":23,"tag":218,"props":579,"children":580},{"class":220,"line":248},[581,586,590,594,598],{"type":23,"tag":218,"props":582,"children":583},{"style":242},[584],{"type":29,"value":585},"                     operation: (",{"type":23,"tag":218,"props":587,"children":588},{"style":236},[589],{"type":29,"value":292},{"type":23,"tag":218,"props":591,"children":592},{"style":242},[593],{"type":29,"value":410},{"type":23,"tag":218,"props":595,"children":596},{"style":236},[597],{"type":29,"value":415},{"type":23,"tag":218,"props":599,"children":600},{"style":242},[601],{"type":29,"value":420},{"type":23,"tag":218,"props":603,"children":604},{"class":220,"line":277},[605,609,613,617,621,625,629],{"type":23,"tag":218,"props":606,"children":607},{"style":225},[608],{"type":29,"value":428},{"type":23,"tag":218,"props":610,"children":611},{"style":242},[612],{"type":29,"value":433},{"type":23,"tag":218,"props":614,"children":615},{"style":225},[616],{"type":29,"value":438},{"type":23,"tag":218,"props":618,"children":619},{"style":305},[620],{"type":29,"value":308},{"type":23,"tag":218,"props":622,"children":623},{"style":242},[624],{"type":29,"value":447},{"type":23,"tag":218,"props":626,"children":627},{"style":225},[628],{"type":29,"value":452},{"type":23,"tag":218,"props":630,"children":631},{"style":242},[632],{"type":29,"value":457},{"type":23,"tag":218,"props":634,"children":635},{"class":220,"line":315},[636],{"type":23,"tag":218,"props":637,"children":638},{"style":463},[639],{"type":29,"value":493},{"type":23,"tag":218,"props":641,"children":642},{"class":220,"line":345},[643,648],{"type":23,"tag":218,"props":644,"children":645},{"style":236},[646],{"type":29,"value":647},"    inOrderTraversal",{"type":23,"tag":218,"props":649,"children":650},{"style":242},[651],{"type":29,"value":507},{"type":23,"tag":218,"props":653,"children":654},{"class":220,"line":487},[655],{"type":23,"tag":218,"props":656,"children":657},{"style":463},[658],{"type":29,"value":466},{"type":23,"tag":218,"props":660,"children":661},{"class":220,"line":496},[662,666,670],{"type":23,"tag":218,"props":663,"children":664},{"style":242},[665],{"type":29,"value":474},{"type":23,"tag":218,"props":667,"children":668},{"style":236},[669],{"type":29,"value":479},{"type":23,"tag":218,"props":671,"children":672},{"style":242},[673],{"type":29,"value":484},{"type":23,"tag":218,"props":675,"children":676},{"class":220,"line":510},[677],{"type":23,"tag":218,"props":678,"children":679},{"style":463},[680],{"type":29,"value":516},{"type":23,"tag":218,"props":682,"children":683},{"class":220,"line":519},[684,688],{"type":23,"tag":218,"props":685,"children":686},{"style":236},[687],{"type":29,"value":647},{"type":23,"tag":218,"props":689,"children":690},{"style":242},[691],{"type":29,"value":529},{"type":23,"tag":218,"props":693,"children":694},{"class":220,"line":532},[695],{"type":23,"tag":218,"props":696,"children":697},{"style":242},[698],{"type":29,"value":538},{"type":23,"tag":36,"props":700,"children":701},{},[702],{"type":23,"tag":70,"props":703,"children":704},{},[705],{"type":29,"value":706},"PostOrder traversal",{"type":23,"tag":209,"props":708,"children":710},{"className":211,"code":709,"language":16,"meta":7,"style":7},"fun postOrderTraversal(node: BinaryTreeNode?, \n                       operation: (BinaryTreeNode) -> Unit){\n    if (node == null) return;\n    // Traverse left\n    postOrderTraversal(node.left,operation)\n    // Traverse right\n    postOrderTraversal(node.right,operation)\n    // Visit node\n    operation.invoke(node)\n}\n",[711],{"type":23,"tag":15,"props":712,"children":713},{"__ignoreMap":7},[714,738,762,793,800,813,820,832,839,854],{"type":23,"tag":218,"props":715,"children":716},{"class":220,"line":221},[717,721,726,730,734],{"type":23,"tag":218,"props":718,"children":719},{"style":225},[720],{"type":29,"value":374},{"type":23,"tag":218,"props":722,"children":723},{"style":236},[724],{"type":29,"value":725}," postOrderTraversal",{"type":23,"tag":218,"props":727,"children":728},{"style":242},[729],{"type":29,"value":384},{"type":23,"tag":218,"props":731,"children":732},{"style":236},[733],{"type":29,"value":292},{"type":23,"tag":218,"props":735,"children":736},{"style":242},[737],{"type":29,"value":393},{"type":23,"tag":218,"props":739,"children":740},{"class":220,"line":248},[741,746,750,754,758],{"type":23,"tag":218,"props":742,"children":743},{"style":242},[744],{"type":29,"value":745},"                       operation: (",{"type":23,"tag":218,"props":747,"children":748},{"style":236},[749],{"type":29,"value":292},{"type":23,"tag":218,"props":751,"children":752},{"style":242},[753],{"type":29,"value":410},{"type":23,"tag":218,"props":755,"children":756},{"style":236},[757],{"type":29,"value":415},{"type":23,"tag":218,"props":759,"children":760},{"style":242},[761],{"type":29,"value":420},{"type":23,"tag":218,"props":763,"children":764},{"class":220,"line":277},[765,769,773,777,781,785,789],{"type":23,"tag":218,"props":766,"children":767},{"style":225},[768],{"type":29,"value":428},{"type":23,"tag":218,"props":770,"children":771},{"style":242},[772],{"type":29,"value":433},{"type":23,"tag":218,"props":774,"children":775},{"style":225},[776],{"type":29,"value":438},{"type":23,"tag":218,"props":778,"children":779},{"style":305},[780],{"type":29,"value":308},{"type":23,"tag":218,"props":782,"children":783},{"style":242},[784],{"type":29,"value":447},{"type":23,"tag":218,"props":786,"children":787},{"style":225},[788],{"type":29,"value":452},{"type":23,"tag":218,"props":790,"children":791},{"style":242},[792],{"type":29,"value":457},{"type":23,"tag":218,"props":794,"children":795},{"class":220,"line":315},[796],{"type":23,"tag":218,"props":797,"children":798},{"style":463},[799],{"type":29,"value":493},{"type":23,"tag":218,"props":801,"children":802},{"class":220,"line":345},[803,808],{"type":23,"tag":218,"props":804,"children":805},{"style":236},[806],{"type":29,"value":807},"    postOrderTraversal",{"type":23,"tag":218,"props":809,"children":810},{"style":242},[811],{"type":29,"value":812},"(node.left,operation)\n",{"type":23,"tag":218,"props":814,"children":815},{"class":220,"line":487},[816],{"type":23,"tag":218,"props":817,"children":818},{"style":463},[819],{"type":29,"value":516},{"type":23,"tag":218,"props":821,"children":822},{"class":220,"line":496},[823,827],{"type":23,"tag":218,"props":824,"children":825},{"style":236},[826],{"type":29,"value":807},{"type":23,"tag":218,"props":828,"children":829},{"style":242},[830],{"type":29,"value":831},"(node.right,operation)\n",{"type":23,"tag":218,"props":833,"children":834},{"class":220,"line":510},[835],{"type":23,"tag":218,"props":836,"children":837},{"style":463},[838],{"type":29,"value":466},{"type":23,"tag":218,"props":840,"children":841},{"class":220,"line":519},[842,846,850],{"type":23,"tag":218,"props":843,"children":844},{"style":242},[845],{"type":29,"value":474},{"type":23,"tag":218,"props":847,"children":848},{"style":236},[849],{"type":29,"value":479},{"type":23,"tag":218,"props":851,"children":852},{"style":242},[853],{"type":29,"value":484},{"type":23,"tag":218,"props":855,"children":856},{"class":220,"line":532},[857],{"type":23,"tag":218,"props":858,"children":859},{"style":242},[860],{"type":29,"value":538},{"type":23,"tag":24,"props":862,"children":864},{"id":863},"bfs-breadth-first-search",[865],{"type":29,"value":866},"BFS - Breadth-First Search",{"type":23,"tag":36,"props":868,"children":869},{},[870],{"type":23,"tag":137,"props":871,"children":873},{"alt":7,"src":872},"https://jaimedearcos-resources.s3-eu-west-1.amazonaws.com/blog/bfs.gif",[],{"type":23,"tag":36,"props":875,"children":876},{},[877],{"type":29,"value":878},"For BFS algorithm we are going to use a queue:",{"type":23,"tag":62,"props":880,"children":881},{},[882,887],{"type":23,"tag":66,"props":883,"children":884},{},[885],{"type":29,"value":886},"Push root node to the queue",{"type":23,"tag":66,"props":888,"children":889},{},[890,892],{"type":29,"value":891},"While queue is not empty:",{"type":23,"tag":62,"props":893,"children":894},{},[895,900,905],{"type":23,"tag":66,"props":896,"children":897},{},[898],{"type":29,"value":899},"Poll element from queue and process data",{"type":23,"tag":66,"props":901,"children":902},{},[903],{"type":29,"value":904},"If left child exist, add to the queue",{"type":23,"tag":66,"props":906,"children":907},{},[908],{"type":29,"value":909},"If right child exist, add to the queue",{"type":23,"tag":209,"props":911,"children":913},{"className":211,"code":912,"language":16,"meta":7,"style":7},"   fun bfs(node: BinaryTreeNode, operation: (BinaryTreeNode) -> Unit){\n\n        val queue = LinkedList\u003CBinaryTreeNode>() as Queue\u003CBinaryTreeNode>\n        queue.offer(node)\n        var current : BinaryTreeNode\n\n        while(!queue.isEmpty()){\n            current = queue.poll()\n            operation.invoke(current)\n\n            if (current.left != null){\n                queue.offer(current.left)\n            }\n            if (current.right != null){\n                queue.offer(current.right)\n            }\n        }\n    }\n",[914],{"type":23,"tag":15,"props":915,"children":916},{"__ignoreMap":7},[917,959,968,1026,1043,1061,1068,1101,1128,1145,1152,1179,1197,1206,1231,1248,1256,1265],{"type":23,"tag":218,"props":918,"children":919},{"class":220,"line":221},[920,925,930,934,938,943,947,951,955],{"type":23,"tag":218,"props":921,"children":922},{"style":225},[923],{"type":29,"value":924},"   fun",{"type":23,"tag":218,"props":926,"children":927},{"style":236},[928],{"type":29,"value":929}," bfs",{"type":23,"tag":218,"props":931,"children":932},{"style":242},[933],{"type":29,"value":384},{"type":23,"tag":218,"props":935,"children":936},{"style":236},[937],{"type":29,"value":292},{"type":23,"tag":218,"props":939,"children":940},{"style":242},[941],{"type":29,"value":942},", operation: (",{"type":23,"tag":218,"props":944,"children":945},{"style":236},[946],{"type":29,"value":292},{"type":23,"tag":218,"props":948,"children":949},{"style":242},[950],{"type":29,"value":410},{"type":23,"tag":218,"props":952,"children":953},{"style":236},[954],{"type":29,"value":415},{"type":23,"tag":218,"props":956,"children":957},{"style":242},[958],{"type":29,"value":420},{"type":23,"tag":218,"props":960,"children":961},{"class":220,"line":248},[962],{"type":23,"tag":218,"props":963,"children":965},{"emptyLinePlaceholder":964},true,[966],{"type":29,"value":967},"\n",{"type":23,"tag":218,"props":969,"children":970},{"class":220,"line":277},[971,975,980,984,989,994,998,1003,1008,1013,1017,1021],{"type":23,"tag":218,"props":972,"children":973},{"style":225},[974],{"type":29,"value":254},{"type":23,"tag":218,"props":976,"children":977},{"style":242},[978],{"type":29,"value":979}," queue ",{"type":23,"tag":218,"props":981,"children":982},{"style":225},[983],{"type":29,"value":302},{"type":23,"tag":218,"props":985,"children":986},{"style":236},[987],{"type":29,"value":988}," LinkedList",{"type":23,"tag":218,"props":990,"children":991},{"style":242},[992],{"type":29,"value":993},"\u003C",{"type":23,"tag":218,"props":995,"children":996},{"style":236},[997],{"type":29,"value":292},{"type":23,"tag":218,"props":999,"children":1000},{"style":242},[1001],{"type":29,"value":1002},">() ",{"type":23,"tag":218,"props":1004,"children":1005},{"style":225},[1006],{"type":29,"value":1007},"as",{"type":23,"tag":218,"props":1009,"children":1010},{"style":242},[1011],{"type":29,"value":1012}," Queue",{"type":23,"tag":218,"props":1014,"children":1015},{"style":225},[1016],{"type":29,"value":993},{"type":23,"tag":218,"props":1018,"children":1019},{"style":242},[1020],{"type":29,"value":292},{"type":23,"tag":218,"props":1022,"children":1023},{"style":225},[1024],{"type":29,"value":1025},">\n",{"type":23,"tag":218,"props":1027,"children":1028},{"class":220,"line":315},[1029,1034,1039],{"type":23,"tag":218,"props":1030,"children":1031},{"style":242},[1032],{"type":29,"value":1033},"        queue.",{"type":23,"tag":218,"props":1035,"children":1036},{"style":236},[1037],{"type":29,"value":1038},"offer",{"type":23,"tag":218,"props":1040,"children":1041},{"style":242},[1042],{"type":29,"value":484},{"type":23,"tag":218,"props":1044,"children":1045},{"class":220,"line":345},[1046,1051,1056],{"type":23,"tag":218,"props":1047,"children":1048},{"style":225},[1049],{"type":29,"value":1050},"        var",{"type":23,"tag":218,"props":1052,"children":1053},{"style":242},[1054],{"type":29,"value":1055}," current : ",{"type":23,"tag":218,"props":1057,"children":1058},{"style":236},[1059],{"type":29,"value":1060},"BinaryTreeNode\n",{"type":23,"tag":218,"props":1062,"children":1063},{"class":220,"line":487},[1064],{"type":23,"tag":218,"props":1065,"children":1066},{"emptyLinePlaceholder":964},[1067],{"type":29,"value":967},{"type":23,"tag":218,"props":1069,"children":1070},{"class":220,"line":496},[1071,1076,1081,1086,1091,1096],{"type":23,"tag":218,"props":1072,"children":1073},{"style":225},[1074],{"type":29,"value":1075},"        while",{"type":23,"tag":218,"props":1077,"children":1078},{"style":242},[1079],{"type":29,"value":1080},"(",{"type":23,"tag":218,"props":1082,"children":1083},{"style":225},[1084],{"type":29,"value":1085},"!",{"type":23,"tag":218,"props":1087,"children":1088},{"style":242},[1089],{"type":29,"value":1090},"queue.",{"type":23,"tag":218,"props":1092,"children":1093},{"style":236},[1094],{"type":29,"value":1095},"isEmpty",{"type":23,"tag":218,"props":1097,"children":1098},{"style":242},[1099],{"type":29,"value":1100},"()){\n",{"type":23,"tag":218,"props":1102,"children":1103},{"class":220,"line":510},[1104,1109,1113,1118,1123],{"type":23,"tag":218,"props":1105,"children":1106},{"style":242},[1107],{"type":29,"value":1108},"            current ",{"type":23,"tag":218,"props":1110,"children":1111},{"style":225},[1112],{"type":29,"value":302},{"type":23,"tag":218,"props":1114,"children":1115},{"style":242},[1116],{"type":29,"value":1117}," queue.",{"type":23,"tag":218,"props":1119,"children":1120},{"style":236},[1121],{"type":29,"value":1122},"poll",{"type":23,"tag":218,"props":1124,"children":1125},{"style":242},[1126],{"type":29,"value":1127},"()\n",{"type":23,"tag":218,"props":1129,"children":1130},{"class":220,"line":519},[1131,1136,1140],{"type":23,"tag":218,"props":1132,"children":1133},{"style":242},[1134],{"type":29,"value":1135},"            operation.",{"type":23,"tag":218,"props":1137,"children":1138},{"style":236},[1139],{"type":29,"value":479},{"type":23,"tag":218,"props":1141,"children":1142},{"style":242},[1143],{"type":29,"value":1144},"(current)\n",{"type":23,"tag":218,"props":1146,"children":1147},{"class":220,"line":532},[1148],{"type":23,"tag":218,"props":1149,"children":1150},{"emptyLinePlaceholder":964},[1151],{"type":29,"value":967},{"type":23,"tag":218,"props":1153,"children":1155},{"class":220,"line":1154},11,[1156,1161,1166,1171,1175],{"type":23,"tag":218,"props":1157,"children":1158},{"style":225},[1159],{"type":29,"value":1160},"            if",{"type":23,"tag":218,"props":1162,"children":1163},{"style":242},[1164],{"type":29,"value":1165}," (current.left ",{"type":23,"tag":218,"props":1167,"children":1168},{"style":225},[1169],{"type":29,"value":1170},"!=",{"type":23,"tag":218,"props":1172,"children":1173},{"style":305},[1174],{"type":29,"value":308},{"type":23,"tag":218,"props":1176,"children":1177},{"style":242},[1178],{"type":29,"value":420},{"type":23,"tag":218,"props":1180,"children":1182},{"class":220,"line":1181},12,[1183,1188,1192],{"type":23,"tag":218,"props":1184,"children":1185},{"style":242},[1186],{"type":29,"value":1187},"                queue.",{"type":23,"tag":218,"props":1189,"children":1190},{"style":236},[1191],{"type":29,"value":1038},{"type":23,"tag":218,"props":1193,"children":1194},{"style":242},[1195],{"type":29,"value":1196},"(current.left)\n",{"type":23,"tag":218,"props":1198,"children":1200},{"class":220,"line":1199},13,[1201],{"type":23,"tag":218,"props":1202,"children":1203},{"style":242},[1204],{"type":29,"value":1205},"            }\n",{"type":23,"tag":218,"props":1207,"children":1209},{"class":220,"line":1208},14,[1210,1214,1219,1223,1227],{"type":23,"tag":218,"props":1211,"children":1212},{"style":225},[1213],{"type":29,"value":1160},{"type":23,"tag":218,"props":1215,"children":1216},{"style":242},[1217],{"type":29,"value":1218}," (current.right ",{"type":23,"tag":218,"props":1220,"children":1221},{"style":225},[1222],{"type":29,"value":1170},{"type":23,"tag":218,"props":1224,"children":1225},{"style":305},[1226],{"type":29,"value":308},{"type":23,"tag":218,"props":1228,"children":1229},{"style":242},[1230],{"type":29,"value":420},{"type":23,"tag":218,"props":1232,"children":1234},{"class":220,"line":1233},15,[1235,1239,1243],{"type":23,"tag":218,"props":1236,"children":1237},{"style":242},[1238],{"type":29,"value":1187},{"type":23,"tag":218,"props":1240,"children":1241},{"style":236},[1242],{"type":29,"value":1038},{"type":23,"tag":218,"props":1244,"children":1245},{"style":242},[1246],{"type":29,"value":1247},"(current.right)\n",{"type":23,"tag":218,"props":1249,"children":1251},{"class":220,"line":1250},16,[1252],{"type":23,"tag":218,"props":1253,"children":1254},{"style":242},[1255],{"type":29,"value":1205},{"type":23,"tag":218,"props":1257,"children":1259},{"class":220,"line":1258},17,[1260],{"type":23,"tag":218,"props":1261,"children":1262},{"style":242},[1263],{"type":29,"value":1264},"        }\n",{"type":23,"tag":218,"props":1266,"children":1268},{"class":220,"line":1267},18,[1269],{"type":23,"tag":218,"props":1270,"children":1271},{"style":242},[1272],{"type":29,"value":1273},"    }\n",{"type":23,"tag":36,"props":1275,"children":1276},{},[1277,1279],{"type":29,"value":1278},"You can find the complete code of this example in this ",{"type":23,"tag":48,"props":1280,"children":1283},{"href":1281,"target":1282},"https://github.com/JaimeDeArcos/kotlin-algorithms/tree/main/trees/src/main/kotlin/es/jaimedearcos/algorithms/tree","_blank",[1284],{"type":29,"value":1285},"GitHub repository",{"type":23,"tag":1287,"props":1288,"children":1289},"style",{},[1290],{"type":29,"value":1291},"html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}",{"title":7,"searchDepth":248,"depth":248,"links":1293},[1294,1295,1298],{"id":26,"depth":248,"text":30},{"id":107,"depth":248,"text":110,"children":1296},[1297],{"id":184,"depth":277,"text":187},{"id":863,"depth":248,"text":866},"markdown","content:posts:algorithms-working-with-trees.md","content","posts/algorithms-working-with-trees.md","posts/algorithms-working-with-trees","md",[1306,3425],{"_path":1307,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":1308,"description":1309,"date":1310,"layout":11,"subtitle":1309,"image":12,"optimized_image":12,"category":13,"tags":1311,"author":18,"paginate":6,"body":1312,"_type":1299,"_id":3422,"_source":1301,"_file":3423,"_stem":3424,"_extension":1304},"/posts/algorithms-binary-search-tree","Algorithms - Binary Search Tree in kotlin","Brief review of binary search trees with kotlin examples","2020-12-06T11:00:00.000Z",[15,16,17,13],{"type":20,"children":1313,"toc":3416},[1314,1333,1350,1362,1369,1375,1387,1520,1532,1538,1543,1593,1919,1925,1944,2190,2196,2201,2307,3401,3412],{"type":23,"tag":32,"props":1315,"children":1316},{},[1317],{"type":23,"tag":36,"props":1318,"children":1319},{},[1320,1325,1327],{"type":23,"tag":40,"props":1321,"children":1322},{},[1323],{"type":29,"value":1324},"\"A binary search tree (BST) is a rooted binary tree whose internal nodes each store a key greater than all the keys in\nthe node's left subtree and less than those in its right subtree.\"",{"type":29,"value":1326}," -- ",{"type":23,"tag":48,"props":1328,"children":1331},{"href":1329,"rel":1330},"https://en.wikipedia.org/wiki/Binary_search_tree",[52],[1332],{"type":29,"value":55},{"type":23,"tag":36,"props":1334,"children":1335},{},[1336,1338,1343,1345],{"type":29,"value":1337},"One of the biggest ",{"type":23,"tag":70,"props":1339,"children":1340},{},[1341],{"type":29,"value":1342},"advantages of Binary Search Trees",{"type":29,"value":1344}," is the performance of lookup, addition and removal of data nodes, each\ncomparison skips the half of the remaining tree, so the operation usually is ",{"type":23,"tag":70,"props":1346,"children":1347},{},[1348],{"type":29,"value":1349},"O(log n)",{"type":23,"tag":36,"props":1351,"children":1352},{},[1353,1355,1360],{"type":29,"value":1354},"Here is ",{"type":23,"tag":70,"props":1356,"children":1357},{},[1358],{"type":29,"value":1359},"an example",{"type":29,"value":1361}," of Binary Search Tree:",{"type":23,"tag":36,"props":1363,"children":1364},{},[1365],{"type":23,"tag":137,"props":1366,"children":1368},{"alt":7,"src":1367},"/assets/img/uploads/binary-search-tree.jpg",[],{"type":23,"tag":24,"props":1370,"children":1372},{"id":1371},"lets-code-a-binary-search-tree",[1373],{"type":29,"value":1374},"Let's code a binary search tree",{"type":23,"tag":36,"props":1376,"children":1377},{},[1378,1380,1386],{"type":29,"value":1379},"The fist step it's going to be the data structure. It's pretty similar to the one we code in the previous post about\n",{"type":23,"tag":48,"props":1381,"children":1383},{"href":1382,"target":1282},"/algorithms-working-with-trees/",[1384],{"type":29,"value":1385},"binary tree traversals",{"type":29,"value":199},{"type":23,"tag":209,"props":1388,"children":1390},{"className":211,"code":1389,"language":16,"meta":7,"style":7},"data class BinarySearchTreeNode(\n        var data:Int,\n        var left: BinarySearchTreeNode? = null,\n        var right: BinarySearchTreeNode? = null\n){\n   // ...\n}\n",[1391],{"type":23,"tag":15,"props":1392,"children":1393},{"__ignoreMap":7},[1394,1414,1437,1470,1498,1505,1513],{"type":23,"tag":218,"props":1395,"children":1396},{"class":220,"line":221},[1397,1401,1405,1410],{"type":23,"tag":218,"props":1398,"children":1399},{"style":225},[1400],{"type":29,"value":228},{"type":23,"tag":218,"props":1402,"children":1403},{"style":225},[1404],{"type":29,"value":233},{"type":23,"tag":218,"props":1406,"children":1407},{"style":236},[1408],{"type":29,"value":1409}," BinarySearchTreeNode",{"type":23,"tag":218,"props":1411,"children":1412},{"style":242},[1413],{"type":29,"value":245},{"type":23,"tag":218,"props":1415,"children":1416},{"class":220,"line":248},[1417,1421,1425,1429,1433],{"type":23,"tag":218,"props":1418,"children":1419},{"style":225},[1420],{"type":29,"value":1050},{"type":23,"tag":218,"props":1422,"children":1423},{"style":225},[1424],{"type":29,"value":259},{"type":23,"tag":218,"props":1426,"children":1427},{"style":242},[1428],{"type":29,"value":264},{"type":23,"tag":218,"props":1430,"children":1431},{"style":236},[1432],{"type":29,"value":269},{"type":23,"tag":218,"props":1434,"children":1435},{"style":242},[1436],{"type":29,"value":274},{"type":23,"tag":218,"props":1438,"children":1439},{"class":220,"line":277},[1440,1444,1449,1454,1458,1462,1466],{"type":23,"tag":218,"props":1441,"children":1442},{"style":225},[1443],{"type":29,"value":1050},{"type":23,"tag":218,"props":1445,"children":1446},{"style":242},[1447],{"type":29,"value":1448}," left: ",{"type":23,"tag":218,"props":1450,"children":1451},{"style":236},[1452],{"type":29,"value":1453},"BinarySearchTreeNode",{"type":23,"tag":218,"props":1455,"children":1456},{"style":242},[1457],{"type":29,"value":297},{"type":23,"tag":218,"props":1459,"children":1460},{"style":225},[1461],{"type":29,"value":302},{"type":23,"tag":218,"props":1463,"children":1464},{"style":305},[1465],{"type":29,"value":308},{"type":23,"tag":218,"props":1467,"children":1468},{"style":242},[1469],{"type":29,"value":274},{"type":23,"tag":218,"props":1471,"children":1472},{"class":220,"line":315},[1473,1477,1482,1486,1490,1494],{"type":23,"tag":218,"props":1474,"children":1475},{"style":225},[1476],{"type":29,"value":1050},{"type":23,"tag":218,"props":1478,"children":1479},{"style":242},[1480],{"type":29,"value":1481}," right: ",{"type":23,"tag":218,"props":1483,"children":1484},{"style":236},[1485],{"type":29,"value":1453},{"type":23,"tag":218,"props":1487,"children":1488},{"style":242},[1489],{"type":29,"value":297},{"type":23,"tag":218,"props":1491,"children":1492},{"style":225},[1493],{"type":29,"value":302},{"type":23,"tag":218,"props":1495,"children":1496},{"style":305},[1497],{"type":29,"value":342},{"type":23,"tag":218,"props":1499,"children":1500},{"class":220,"line":345},[1501],{"type":23,"tag":218,"props":1502,"children":1503},{"style":242},[1504],{"type":29,"value":420},{"type":23,"tag":218,"props":1506,"children":1507},{"class":220,"line":487},[1508],{"type":23,"tag":218,"props":1509,"children":1510},{"style":463},[1511],{"type":29,"value":1512},"   // ...\n",{"type":23,"tag":218,"props":1514,"children":1515},{"class":220,"line":496},[1516],{"type":23,"tag":218,"props":1517,"children":1518},{"style":242},[1519],{"type":29,"value":538},{"type":23,"tag":36,"props":1521,"children":1522},{},[1523,1525,1530],{"type":29,"value":1524},"As you can see is basically the same as simple binary trees, the main difference is going to be how we ",{"type":23,"tag":70,"props":1526,"children":1527},{},[1528],{"type":29,"value":1529},"insert, search\nand delete",{"type":29,"value":1531}," values from the tree.",{"type":23,"tag":24,"props":1533,"children":1535},{"id":1534},"insert-values-in-the-tree",[1536],{"type":29,"value":1537},"Insert values in the tree",{"type":23,"tag":36,"props":1539,"children":1540},{},[1541],{"type":29,"value":1542},"The insertion function is a simple recursive one, there are 4 cases:",{"type":23,"tag":62,"props":1544,"children":1545},{},[1546,1571],{"type":23,"tag":66,"props":1547,"children":1548},{},[1549,1551,1556,1558],{"type":29,"value":1550},"New item is ",{"type":23,"tag":70,"props":1552,"children":1553},{},[1554],{"type":29,"value":1555},"lower or equals",{"type":29,"value":1557}," than the current node data:\n",{"type":23,"tag":62,"props":1559,"children":1560},{},[1561,1566],{"type":23,"tag":66,"props":1562,"children":1563},{},[1564],{"type":29,"value":1565},"If left exist --> insert the value in left child (recursion)",{"type":23,"tag":66,"props":1567,"children":1568},{},[1569],{"type":29,"value":1570},"Else, create a new left child with the value (finish condition)",{"type":23,"tag":66,"props":1572,"children":1573},{},[1574,1575,1580,1581],{"type":29,"value":1550},{"type":23,"tag":70,"props":1576,"children":1577},{},[1578],{"type":29,"value":1579},"greater",{"type":29,"value":1557},{"type":23,"tag":62,"props":1582,"children":1583},{},[1584,1588],{"type":23,"tag":66,"props":1585,"children":1586},{},[1587],{"type":29,"value":1565},{"type":23,"tag":66,"props":1589,"children":1590},{},[1591],{"type":29,"value":1592},"Else, create a new right child with the value (finish condition)",{"type":23,"tag":209,"props":1594,"children":1596},{"className":211,"code":1595,"language":16,"meta":7,"style":7},"fun insert(value:Int){\n    if(value \u003C= data && left != null){\n        left!!.insert(value)\n    }else if (value \u003C= data){\n        left = BinarySearchTreeNode(value)\n    }else if(value> data && right != null){\n        right!!.insert(value)\n    }else{\n        right = BinarySearchTreeNode(value)\n    }\n}\n",[1597],{"type":23,"tag":15,"props":1598,"children":1599},{"__ignoreMap":7},[1600,1633,1679,1713,1752,1780,1829,1861,1877,1905,1912],{"type":23,"tag":218,"props":1601,"children":1602},{"class":220,"line":221},[1603,1607,1612,1616,1621,1625,1629],{"type":23,"tag":218,"props":1604,"children":1605},{"style":225},[1606],{"type":29,"value":374},{"type":23,"tag":218,"props":1608,"children":1609},{"style":236},[1610],{"type":29,"value":1611}," insert",{"type":23,"tag":218,"props":1613,"children":1614},{"style":242},[1615],{"type":29,"value":1080},{"type":23,"tag":218,"props":1617,"children":1618},{"style":225},[1619],{"type":29,"value":1620},"value",{"type":23,"tag":218,"props":1622,"children":1623},{"style":242},[1624],{"type":29,"value":264},{"type":23,"tag":218,"props":1626,"children":1627},{"style":236},[1628],{"type":29,"value":269},{"type":23,"tag":218,"props":1630,"children":1631},{"style":242},[1632],{"type":29,"value":420},{"type":23,"tag":218,"props":1634,"children":1635},{"class":220,"line":248},[1636,1640,1644,1648,1653,1657,1662,1667,1671,1675],{"type":23,"tag":218,"props":1637,"children":1638},{"style":225},[1639],{"type":29,"value":428},{"type":23,"tag":218,"props":1641,"children":1642},{"style":242},[1643],{"type":29,"value":1080},{"type":23,"tag":218,"props":1645,"children":1646},{"style":225},[1647],{"type":29,"value":1620},{"type":23,"tag":218,"props":1649,"children":1650},{"style":225},[1651],{"type":29,"value":1652}," \u003C=",{"type":23,"tag":218,"props":1654,"children":1655},{"style":225},[1656],{"type":29,"value":259},{"type":23,"tag":218,"props":1658,"children":1659},{"style":225},[1660],{"type":29,"value":1661}," &&",{"type":23,"tag":218,"props":1663,"children":1664},{"style":242},[1665],{"type":29,"value":1666}," left ",{"type":23,"tag":218,"props":1668,"children":1669},{"style":225},[1670],{"type":29,"value":1170},{"type":23,"tag":218,"props":1672,"children":1673},{"style":305},[1674],{"type":29,"value":308},{"type":23,"tag":218,"props":1676,"children":1677},{"style":242},[1678],{"type":29,"value":420},{"type":23,"tag":218,"props":1680,"children":1681},{"class":220,"line":277},[1682,1687,1692,1696,1701,1705,1709],{"type":23,"tag":218,"props":1683,"children":1684},{"style":242},[1685],{"type":29,"value":1686},"        left",{"type":23,"tag":218,"props":1688,"children":1689},{"style":225},[1690],{"type":29,"value":1691},"!!",{"type":23,"tag":218,"props":1693,"children":1694},{"style":242},[1695],{"type":29,"value":199},{"type":23,"tag":218,"props":1697,"children":1698},{"style":236},[1699],{"type":29,"value":1700},"insert",{"type":23,"tag":218,"props":1702,"children":1703},{"style":242},[1704],{"type":29,"value":1080},{"type":23,"tag":218,"props":1706,"children":1707},{"style":225},[1708],{"type":29,"value":1620},{"type":23,"tag":218,"props":1710,"children":1711},{"style":242},[1712],{"type":29,"value":351},{"type":23,"tag":218,"props":1714,"children":1715},{"class":220,"line":315},[1716,1721,1726,1731,1736,1740,1744,1748],{"type":23,"tag":218,"props":1717,"children":1718},{"style":242},[1719],{"type":29,"value":1720},"    }",{"type":23,"tag":218,"props":1722,"children":1723},{"style":225},[1724],{"type":29,"value":1725},"else",{"type":23,"tag":218,"props":1727,"children":1728},{"style":225},[1729],{"type":29,"value":1730}," if",{"type":23,"tag":218,"props":1732,"children":1733},{"style":242},[1734],{"type":29,"value":1735}," (",{"type":23,"tag":218,"props":1737,"children":1738},{"style":225},[1739],{"type":29,"value":1620},{"type":23,"tag":218,"props":1741,"children":1742},{"style":225},[1743],{"type":29,"value":1652},{"type":23,"tag":218,"props":1745,"children":1746},{"style":225},[1747],{"type":29,"value":259},{"type":23,"tag":218,"props":1749,"children":1750},{"style":242},[1751],{"type":29,"value":420},{"type":23,"tag":218,"props":1753,"children":1754},{"class":220,"line":345},[1755,1760,1764,1768,1772,1776],{"type":23,"tag":218,"props":1756,"children":1757},{"style":242},[1758],{"type":29,"value":1759},"        left ",{"type":23,"tag":218,"props":1761,"children":1762},{"style":225},[1763],{"type":29,"value":302},{"type":23,"tag":218,"props":1765,"children":1766},{"style":236},[1767],{"type":29,"value":1409},{"type":23,"tag":218,"props":1769,"children":1770},{"style":242},[1771],{"type":29,"value":1080},{"type":23,"tag":218,"props":1773,"children":1774},{"style":225},[1775],{"type":29,"value":1620},{"type":23,"tag":218,"props":1777,"children":1778},{"style":242},[1779],{"type":29,"value":351},{"type":23,"tag":218,"props":1781,"children":1782},{"class":220,"line":487},[1783,1787,1791,1795,1799,1804,1808,1812,1817,1821,1825],{"type":23,"tag":218,"props":1784,"children":1785},{"style":242},[1786],{"type":29,"value":1720},{"type":23,"tag":218,"props":1788,"children":1789},{"style":225},[1790],{"type":29,"value":1725},{"type":23,"tag":218,"props":1792,"children":1793},{"style":225},[1794],{"type":29,"value":1730},{"type":23,"tag":218,"props":1796,"children":1797},{"style":242},[1798],{"type":29,"value":1080},{"type":23,"tag":218,"props":1800,"children":1801},{"style":225},[1802],{"type":29,"value":1803},"value>",{"type":23,"tag":218,"props":1805,"children":1806},{"style":225},[1807],{"type":29,"value":259},{"type":23,"tag":218,"props":1809,"children":1810},{"style":225},[1811],{"type":29,"value":1661},{"type":23,"tag":218,"props":1813,"children":1814},{"style":242},[1815],{"type":29,"value":1816}," right ",{"type":23,"tag":218,"props":1818,"children":1819},{"style":225},[1820],{"type":29,"value":1170},{"type":23,"tag":218,"props":1822,"children":1823},{"style":305},[1824],{"type":29,"value":308},{"type":23,"tag":218,"props":1826,"children":1827},{"style":242},[1828],{"type":29,"value":420},{"type":23,"tag":218,"props":1830,"children":1831},{"class":220,"line":496},[1832,1837,1841,1845,1849,1853,1857],{"type":23,"tag":218,"props":1833,"children":1834},{"style":242},[1835],{"type":29,"value":1836},"        right",{"type":23,"tag":218,"props":1838,"children":1839},{"style":225},[1840],{"type":29,"value":1691},{"type":23,"tag":218,"props":1842,"children":1843},{"style":242},[1844],{"type":29,"value":199},{"type":23,"tag":218,"props":1846,"children":1847},{"style":236},[1848],{"type":29,"value":1700},{"type":23,"tag":218,"props":1850,"children":1851},{"style":242},[1852],{"type":29,"value":1080},{"type":23,"tag":218,"props":1854,"children":1855},{"style":225},[1856],{"type":29,"value":1620},{"type":23,"tag":218,"props":1858,"children":1859},{"style":242},[1860],{"type":29,"value":351},{"type":23,"tag":218,"props":1862,"children":1863},{"class":220,"line":510},[1864,1868,1872],{"type":23,"tag":218,"props":1865,"children":1866},{"style":242},[1867],{"type":29,"value":1720},{"type":23,"tag":218,"props":1869,"children":1870},{"style":225},[1871],{"type":29,"value":1725},{"type":23,"tag":218,"props":1873,"children":1874},{"style":242},[1875],{"type":29,"value":1876},"{\n",{"type":23,"tag":218,"props":1878,"children":1879},{"class":220,"line":519},[1880,1885,1889,1893,1897,1901],{"type":23,"tag":218,"props":1881,"children":1882},{"style":242},[1883],{"type":29,"value":1884},"        right ",{"type":23,"tag":218,"props":1886,"children":1887},{"style":225},[1888],{"type":29,"value":302},{"type":23,"tag":218,"props":1890,"children":1891},{"style":236},[1892],{"type":29,"value":1409},{"type":23,"tag":218,"props":1894,"children":1895},{"style":242},[1896],{"type":29,"value":1080},{"type":23,"tag":218,"props":1898,"children":1899},{"style":225},[1900],{"type":29,"value":1620},{"type":23,"tag":218,"props":1902,"children":1903},{"style":242},[1904],{"type":29,"value":351},{"type":23,"tag":218,"props":1906,"children":1907},{"class":220,"line":532},[1908],{"type":23,"tag":218,"props":1909,"children":1910},{"style":242},[1911],{"type":29,"value":1273},{"type":23,"tag":218,"props":1913,"children":1914},{"class":220,"line":1154},[1915],{"type":23,"tag":218,"props":1916,"children":1917},{"style":242},[1918],{"type":29,"value":538},{"type":23,"tag":24,"props":1920,"children":1922},{"id":1921},"searchcontains-value-in-the-tree",[1923],{"type":29,"value":1924},"Search/contains value in the tree",{"type":23,"tag":36,"props":1926,"children":1927},{},[1928,1930,1934,1936,1942],{"type":29,"value":1929},"In the following ",{"type":23,"tag":70,"props":1931,"children":1932},{},[1933],{"type":29,"value":16},{"type":29,"value":1935}," snippet we are going to implement the ",{"type":23,"tag":15,"props":1937,"children":1939},{"className":1938},[],[1940],{"type":29,"value":1941},"contains",{"type":29,"value":1943}," function, because our tree stores only Integer values.\nIf you want to store complex objects, is not difficult to modify the function to return the data of searched value instead\na boolean, but you need to store only comparable objects with proper implementation of equals function.",{"type":23,"tag":209,"props":1945,"children":1947},{"className":211,"code":1946,"language":16,"meta":7,"style":7},"fun contains(value:Int) : Boolean{\n    if (value \u003C data && left !=null) {\n        return left!!.contains(value)\n    }\n    if(value > data && right!=null) {\n        return right!!.contains(value)\n    }\n    return value==data\n}\n",[1948],{"type":23,"tag":15,"props":1949,"children":1950},{"__ignoreMap":7},[1951,1993,2039,2076,2083,2128,2163,2170,2183],{"type":23,"tag":218,"props":1952,"children":1953},{"class":220,"line":221},[1954,1958,1963,1967,1971,1975,1979,1984,1989],{"type":23,"tag":218,"props":1955,"children":1956},{"style":225},[1957],{"type":29,"value":374},{"type":23,"tag":218,"props":1959,"children":1960},{"style":236},[1961],{"type":29,"value":1962}," contains",{"type":23,"tag":218,"props":1964,"children":1965},{"style":242},[1966],{"type":29,"value":1080},{"type":23,"tag":218,"props":1968,"children":1969},{"style":225},[1970],{"type":29,"value":1620},{"type":23,"tag":218,"props":1972,"children":1973},{"style":242},[1974],{"type":29,"value":264},{"type":23,"tag":218,"props":1976,"children":1977},{"style":236},[1978],{"type":29,"value":269},{"type":23,"tag":218,"props":1980,"children":1981},{"style":242},[1982],{"type":29,"value":1983},") : ",{"type":23,"tag":218,"props":1985,"children":1986},{"style":236},[1987],{"type":29,"value":1988},"Boolean",{"type":23,"tag":218,"props":1990,"children":1991},{"style":242},[1992],{"type":29,"value":1876},{"type":23,"tag":218,"props":1994,"children":1995},{"class":220,"line":248},[1996,2000,2004,2008,2013,2017,2021,2025,2029,2034],{"type":23,"tag":218,"props":1997,"children":1998},{"style":225},[1999],{"type":29,"value":428},{"type":23,"tag":218,"props":2001,"children":2002},{"style":242},[2003],{"type":29,"value":1735},{"type":23,"tag":218,"props":2005,"children":2006},{"style":225},[2007],{"type":29,"value":1620},{"type":23,"tag":218,"props":2009,"children":2010},{"style":225},[2011],{"type":29,"value":2012}," \u003C",{"type":23,"tag":218,"props":2014,"children":2015},{"style":225},[2016],{"type":29,"value":259},{"type":23,"tag":218,"props":2018,"children":2019},{"style":225},[2020],{"type":29,"value":1661},{"type":23,"tag":218,"props":2022,"children":2023},{"style":242},[2024],{"type":29,"value":1666},{"type":23,"tag":218,"props":2026,"children":2027},{"style":225},[2028],{"type":29,"value":1170},{"type":23,"tag":218,"props":2030,"children":2031},{"style":305},[2032],{"type":29,"value":2033},"null",{"type":23,"tag":218,"props":2035,"children":2036},{"style":242},[2037],{"type":29,"value":2038},") {\n",{"type":23,"tag":218,"props":2040,"children":2041},{"class":220,"line":277},[2042,2047,2052,2056,2060,2064,2068,2072],{"type":23,"tag":218,"props":2043,"children":2044},{"style":225},[2045],{"type":29,"value":2046},"        return",{"type":23,"tag":218,"props":2048,"children":2049},{"style":242},[2050],{"type":29,"value":2051}," left",{"type":23,"tag":218,"props":2053,"children":2054},{"style":225},[2055],{"type":29,"value":1691},{"type":23,"tag":218,"props":2057,"children":2058},{"style":242},[2059],{"type":29,"value":199},{"type":23,"tag":218,"props":2061,"children":2062},{"style":236},[2063],{"type":29,"value":1941},{"type":23,"tag":218,"props":2065,"children":2066},{"style":242},[2067],{"type":29,"value":1080},{"type":23,"tag":218,"props":2069,"children":2070},{"style":225},[2071],{"type":29,"value":1620},{"type":23,"tag":218,"props":2073,"children":2074},{"style":242},[2075],{"type":29,"value":351},{"type":23,"tag":218,"props":2077,"children":2078},{"class":220,"line":315},[2079],{"type":23,"tag":218,"props":2080,"children":2081},{"style":242},[2082],{"type":29,"value":1273},{"type":23,"tag":218,"props":2084,"children":2085},{"class":220,"line":345},[2086,2090,2094,2098,2103,2107,2111,2116,2120,2124],{"type":23,"tag":218,"props":2087,"children":2088},{"style":225},[2089],{"type":29,"value":428},{"type":23,"tag":218,"props":2091,"children":2092},{"style":242},[2093],{"type":29,"value":1080},{"type":23,"tag":218,"props":2095,"children":2096},{"style":225},[2097],{"type":29,"value":1620},{"type":23,"tag":218,"props":2099,"children":2100},{"style":225},[2101],{"type":29,"value":2102}," >",{"type":23,"tag":218,"props":2104,"children":2105},{"style":225},[2106],{"type":29,"value":259},{"type":23,"tag":218,"props":2108,"children":2109},{"style":225},[2110],{"type":29,"value":1661},{"type":23,"tag":218,"props":2112,"children":2113},{"style":242},[2114],{"type":29,"value":2115}," right",{"type":23,"tag":218,"props":2117,"children":2118},{"style":225},[2119],{"type":29,"value":1170},{"type":23,"tag":218,"props":2121,"children":2122},{"style":305},[2123],{"type":29,"value":2033},{"type":23,"tag":218,"props":2125,"children":2126},{"style":242},[2127],{"type":29,"value":2038},{"type":23,"tag":218,"props":2129,"children":2130},{"class":220,"line":487},[2131,2135,2139,2143,2147,2151,2155,2159],{"type":23,"tag":218,"props":2132,"children":2133},{"style":225},[2134],{"type":29,"value":2046},{"type":23,"tag":218,"props":2136,"children":2137},{"style":242},[2138],{"type":29,"value":2115},{"type":23,"tag":218,"props":2140,"children":2141},{"style":225},[2142],{"type":29,"value":1691},{"type":23,"tag":218,"props":2144,"children":2145},{"style":242},[2146],{"type":29,"value":199},{"type":23,"tag":218,"props":2148,"children":2149},{"style":236},[2150],{"type":29,"value":1941},{"type":23,"tag":218,"props":2152,"children":2153},{"style":242},[2154],{"type":29,"value":1080},{"type":23,"tag":218,"props":2156,"children":2157},{"style":225},[2158],{"type":29,"value":1620},{"type":23,"tag":218,"props":2160,"children":2161},{"style":242},[2162],{"type":29,"value":351},{"type":23,"tag":218,"props":2164,"children":2165},{"class":220,"line":496},[2166],{"type":23,"tag":218,"props":2167,"children":2168},{"style":242},[2169],{"type":29,"value":1273},{"type":23,"tag":218,"props":2171,"children":2172},{"class":220,"line":510},[2173,2178],{"type":23,"tag":218,"props":2174,"children":2175},{"style":225},[2176],{"type":29,"value":2177},"    return",{"type":23,"tag":218,"props":2179,"children":2180},{"style":225},[2181],{"type":29,"value":2182}," value==data\n",{"type":23,"tag":218,"props":2184,"children":2185},{"class":220,"line":519},[2186],{"type":23,"tag":218,"props":2187,"children":2188},{"style":242},[2189],{"type":29,"value":538},{"type":23,"tag":24,"props":2191,"children":2193},{"id":2192},"removing-nodes-from-binary-search-tree",[2194],{"type":29,"value":2195},"Removing nodes from Binary Search Tree",{"type":23,"tag":36,"props":2197,"children":2198},{},[2199],{"type":29,"value":2200},"Probably the most complex operation because if we need to remove a node with children we should reorder the tree.\nWe need to consider the following 3 scenarios:",{"type":23,"tag":2202,"props":2203,"children":2204},"ol",{},[2205,2246,2282],{"type":23,"tag":66,"props":2206,"children":2207},{},[2208,2210,2216,2218],{"type":29,"value":2209},"The removed value is ",{"type":23,"tag":15,"props":2211,"children":2213},{"className":2212},[],[2214],{"type":29,"value":2215},"lower",{"type":29,"value":2217}," than the current node:\n",{"type":23,"tag":62,"props":2219,"children":2220},{},[2221,2237],{"type":23,"tag":66,"props":2222,"children":2223},{},[2224,2229,2231,2235],{"type":23,"tag":70,"props":2225,"children":2226},{},[2227],{"type":29,"value":2228},"if left child exist",{"type":29,"value":2230},", we use the ",{"type":23,"tag":70,"props":2232,"children":2233},{},[2234],{"type":29,"value":197},{"type":29,"value":2236}," to remove value from the left child",{"type":23,"tag":66,"props":2238,"children":2239},{},[2240,2244],{"type":23,"tag":70,"props":2241,"children":2242},{},[2243],{"type":29,"value":1725},{"type":29,"value":2245},", value does not exist in tree, return false.",{"type":23,"tag":66,"props":2247,"children":2248},{},[2249,2251,2256,2257],{"type":29,"value":2250},"Removed value is ",{"type":23,"tag":15,"props":2252,"children":2254},{"className":2253},[],[2255],{"type":29,"value":1579},{"type":29,"value":2217},{"type":23,"tag":62,"props":2258,"children":2259},{},[2260,2274],{"type":23,"tag":66,"props":2261,"children":2262},{},[2263,2268,2269,2273],{"type":23,"tag":70,"props":2264,"children":2265},{},[2266],{"type":29,"value":2267},"if right child exist",{"type":29,"value":2230},{"type":23,"tag":70,"props":2270,"children":2271},{},[2272],{"type":29,"value":197},{"type":29,"value":2236},{"type":23,"tag":66,"props":2275,"children":2276},{},[2277,2281],{"type":23,"tag":70,"props":2278,"children":2279},{},[2280],{"type":29,"value":1725},{"type":29,"value":2245},{"type":23,"tag":66,"props":2283,"children":2284},{},[2285,2286,2292,2294],{"type":29,"value":2250},{"type":23,"tag":15,"props":2287,"children":2289},{"className":2288},[],[2290],{"type":29,"value":2291},"equals",{"type":29,"value":2293}," to the current node data:\n",{"type":23,"tag":62,"props":2295,"children":2296},{},[2297],{"type":23,"tag":66,"props":2298,"children":2299},{},[2300,2302],{"type":29,"value":2301},"Depending on the existing children we should remove or reorder ",{"type":23,"tag":40,"props":2303,"children":2304},{},[2305],{"type":29,"value":2306},"(see code comments)",{"type":23,"tag":209,"props":2308,"children":2310},{"className":211,"code":2309,"language":16,"meta":7,"style":7},"fun remove(value:Int, parent:BinarySearchTreeNode) : Boolean{\n    // Value lower than current node data\n    if (value\u003Cdata && left!=null){\n        return left!!.remove(value,this)\n    }else if(value\u003Cdata){\n        return false\n    // Value greater than current node data\n    }else if(value>data && right != null){\n        return right!!.remove(value,this)\n    }else if (value>data){\n        return false\n    // Value equals the greater node data\n    }else {\n        // No Children, current node is parents LEFT node\n        if (left == null && right==null && this == parent.left) {\n            parent.left = null\n        // No Children, current node is parents RIGHT node\n        }else if (left == null && right==null && this==parent.right){\n            parent.right = null\n        // ONLY LEFT child, current node is parents LEFT node\n        }else if(left!=null && right==null && this == parent.left){\n            parent.left = this.left\n        // ONLY LEFT child, current node is parents RIGHT node\n        }else if(left!=null && right== null && this == parent.right){\n            parent.right = this.left\n        // ONLY RIGHT child, current node is parents LEFT node\n        }else if (right!=null && left==null && this == parent.left) {\n            parent.left = this.right\n        // ONLY RIGHT child, current node is parents RIGHT node\n        }else if(right!=null && left==null && this == parent.right) {\n            parent.right = this.right\n        // BOTH Children exists, replace current data with min value from right \n        }else{\n            data = right!!.min()\n            right!!.remove(data,this)\n        }\n        return true\n    }\n}\n\nfun min() : Int = if (left==null) data else left!!.min()\n",[2311],{"type":23,"tag":15,"props":2312,"children":2313},{"__ignoreMap":7},[2314,2363,2371,2407,2453,2480,2492,2500,2544,2587,2614,2625,2633,2649,2657,2713,2729,2737,2798,2815,2824,2886,2907,2916,2977,2997,3006,3067,3088,3097,3159,3179,3188,3204,3239,3280,3288,3301,3309,3317,3325],{"type":23,"tag":218,"props":2315,"children":2316},{"class":220,"line":221},[2317,2321,2326,2330,2334,2338,2342,2347,2351,2355,2359],{"type":23,"tag":218,"props":2318,"children":2319},{"style":225},[2320],{"type":29,"value":374},{"type":23,"tag":218,"props":2322,"children":2323},{"style":236},[2324],{"type":29,"value":2325}," remove",{"type":23,"tag":218,"props":2327,"children":2328},{"style":242},[2329],{"type":29,"value":1080},{"type":23,"tag":218,"props":2331,"children":2332},{"style":225},[2333],{"type":29,"value":1620},{"type":23,"tag":218,"props":2335,"children":2336},{"style":242},[2337],{"type":29,"value":264},{"type":23,"tag":218,"props":2339,"children":2340},{"style":236},[2341],{"type":29,"value":269},{"type":23,"tag":218,"props":2343,"children":2344},{"style":242},[2345],{"type":29,"value":2346},", parent:",{"type":23,"tag":218,"props":2348,"children":2349},{"style":236},[2350],{"type":29,"value":1453},{"type":23,"tag":218,"props":2352,"children":2353},{"style":242},[2354],{"type":29,"value":1983},{"type":23,"tag":218,"props":2356,"children":2357},{"style":236},[2358],{"type":29,"value":1988},{"type":23,"tag":218,"props":2360,"children":2361},{"style":242},[2362],{"type":29,"value":1876},{"type":23,"tag":218,"props":2364,"children":2365},{"class":220,"line":248},[2366],{"type":23,"tag":218,"props":2367,"children":2368},{"style":463},[2369],{"type":29,"value":2370},"    // Value lower than current node data\n",{"type":23,"tag":218,"props":2372,"children":2373},{"class":220,"line":277},[2374,2378,2382,2387,2391,2395,2399,2403],{"type":23,"tag":218,"props":2375,"children":2376},{"style":225},[2377],{"type":29,"value":428},{"type":23,"tag":218,"props":2379,"children":2380},{"style":242},[2381],{"type":29,"value":1735},{"type":23,"tag":218,"props":2383,"children":2384},{"style":225},[2385],{"type":29,"value":2386},"value\u003Cdata",{"type":23,"tag":218,"props":2388,"children":2389},{"style":225},[2390],{"type":29,"value":1661},{"type":23,"tag":218,"props":2392,"children":2393},{"style":242},[2394],{"type":29,"value":2051},{"type":23,"tag":218,"props":2396,"children":2397},{"style":225},[2398],{"type":29,"value":1170},{"type":23,"tag":218,"props":2400,"children":2401},{"style":305},[2402],{"type":29,"value":2033},{"type":23,"tag":218,"props":2404,"children":2405},{"style":242},[2406],{"type":29,"value":420},{"type":23,"tag":218,"props":2408,"children":2409},{"class":220,"line":315},[2410,2414,2418,2422,2426,2431,2435,2439,2444,2449],{"type":23,"tag":218,"props":2411,"children":2412},{"style":225},[2413],{"type":29,"value":2046},{"type":23,"tag":218,"props":2415,"children":2416},{"style":242},[2417],{"type":29,"value":2051},{"type":23,"tag":218,"props":2419,"children":2420},{"style":225},[2421],{"type":29,"value":1691},{"type":23,"tag":218,"props":2423,"children":2424},{"style":242},[2425],{"type":29,"value":199},{"type":23,"tag":218,"props":2427,"children":2428},{"style":236},[2429],{"type":29,"value":2430},"remove",{"type":23,"tag":218,"props":2432,"children":2433},{"style":242},[2434],{"type":29,"value":1080},{"type":23,"tag":218,"props":2436,"children":2437},{"style":225},[2438],{"type":29,"value":1620},{"type":23,"tag":218,"props":2440,"children":2441},{"style":242},[2442],{"type":29,"value":2443},",",{"type":23,"tag":218,"props":2445,"children":2446},{"style":305},[2447],{"type":29,"value":2448},"this",{"type":23,"tag":218,"props":2450,"children":2451},{"style":242},[2452],{"type":29,"value":351},{"type":23,"tag":218,"props":2454,"children":2455},{"class":220,"line":345},[2456,2460,2464,2468,2472,2476],{"type":23,"tag":218,"props":2457,"children":2458},{"style":242},[2459],{"type":29,"value":1720},{"type":23,"tag":218,"props":2461,"children":2462},{"style":225},[2463],{"type":29,"value":1725},{"type":23,"tag":218,"props":2465,"children":2466},{"style":225},[2467],{"type":29,"value":1730},{"type":23,"tag":218,"props":2469,"children":2470},{"style":242},[2471],{"type":29,"value":1080},{"type":23,"tag":218,"props":2473,"children":2474},{"style":225},[2475],{"type":29,"value":2386},{"type":23,"tag":218,"props":2477,"children":2478},{"style":242},[2479],{"type":29,"value":420},{"type":23,"tag":218,"props":2481,"children":2482},{"class":220,"line":487},[2483,2487],{"type":23,"tag":218,"props":2484,"children":2485},{"style":225},[2486],{"type":29,"value":2046},{"type":23,"tag":218,"props":2488,"children":2489},{"style":305},[2490],{"type":29,"value":2491}," false\n",{"type":23,"tag":218,"props":2493,"children":2494},{"class":220,"line":496},[2495],{"type":23,"tag":218,"props":2496,"children":2497},{"style":463},[2498],{"type":29,"value":2499},"    // Value greater than current node data\n",{"type":23,"tag":218,"props":2501,"children":2502},{"class":220,"line":510},[2503,2507,2511,2515,2519,2524,2528,2532,2536,2540],{"type":23,"tag":218,"props":2504,"children":2505},{"style":242},[2506],{"type":29,"value":1720},{"type":23,"tag":218,"props":2508,"children":2509},{"style":225},[2510],{"type":29,"value":1725},{"type":23,"tag":218,"props":2512,"children":2513},{"style":225},[2514],{"type":29,"value":1730},{"type":23,"tag":218,"props":2516,"children":2517},{"style":242},[2518],{"type":29,"value":1080},{"type":23,"tag":218,"props":2520,"children":2521},{"style":225},[2522],{"type":29,"value":2523},"value>data",{"type":23,"tag":218,"props":2525,"children":2526},{"style":225},[2527],{"type":29,"value":1661},{"type":23,"tag":218,"props":2529,"children":2530},{"style":242},[2531],{"type":29,"value":1816},{"type":23,"tag":218,"props":2533,"children":2534},{"style":225},[2535],{"type":29,"value":1170},{"type":23,"tag":218,"props":2537,"children":2538},{"style":305},[2539],{"type":29,"value":308},{"type":23,"tag":218,"props":2541,"children":2542},{"style":242},[2543],{"type":29,"value":420},{"type":23,"tag":218,"props":2545,"children":2546},{"class":220,"line":519},[2547,2551,2555,2559,2563,2567,2571,2575,2579,2583],{"type":23,"tag":218,"props":2548,"children":2549},{"style":225},[2550],{"type":29,"value":2046},{"type":23,"tag":218,"props":2552,"children":2553},{"style":242},[2554],{"type":29,"value":2115},{"type":23,"tag":218,"props":2556,"children":2557},{"style":225},[2558],{"type":29,"value":1691},{"type":23,"tag":218,"props":2560,"children":2561},{"style":242},[2562],{"type":29,"value":199},{"type":23,"tag":218,"props":2564,"children":2565},{"style":236},[2566],{"type":29,"value":2430},{"type":23,"tag":218,"props":2568,"children":2569},{"style":242},[2570],{"type":29,"value":1080},{"type":23,"tag":218,"props":2572,"children":2573},{"style":225},[2574],{"type":29,"value":1620},{"type":23,"tag":218,"props":2576,"children":2577},{"style":242},[2578],{"type":29,"value":2443},{"type":23,"tag":218,"props":2580,"children":2581},{"style":305},[2582],{"type":29,"value":2448},{"type":23,"tag":218,"props":2584,"children":2585},{"style":242},[2586],{"type":29,"value":351},{"type":23,"tag":218,"props":2588,"children":2589},{"class":220,"line":532},[2590,2594,2598,2602,2606,2610],{"type":23,"tag":218,"props":2591,"children":2592},{"style":242},[2593],{"type":29,"value":1720},{"type":23,"tag":218,"props":2595,"children":2596},{"style":225},[2597],{"type":29,"value":1725},{"type":23,"tag":218,"props":2599,"children":2600},{"style":225},[2601],{"type":29,"value":1730},{"type":23,"tag":218,"props":2603,"children":2604},{"style":242},[2605],{"type":29,"value":1735},{"type":23,"tag":218,"props":2607,"children":2608},{"style":225},[2609],{"type":29,"value":2523},{"type":23,"tag":218,"props":2611,"children":2612},{"style":242},[2613],{"type":29,"value":420},{"type":23,"tag":218,"props":2615,"children":2616},{"class":220,"line":1154},[2617,2621],{"type":23,"tag":218,"props":2618,"children":2619},{"style":225},[2620],{"type":29,"value":2046},{"type":23,"tag":218,"props":2622,"children":2623},{"style":305},[2624],{"type":29,"value":2491},{"type":23,"tag":218,"props":2626,"children":2627},{"class":220,"line":1181},[2628],{"type":23,"tag":218,"props":2629,"children":2630},{"style":463},[2631],{"type":29,"value":2632},"    // Value equals the greater node data\n",{"type":23,"tag":218,"props":2634,"children":2635},{"class":220,"line":1199},[2636,2640,2644],{"type":23,"tag":218,"props":2637,"children":2638},{"style":242},[2639],{"type":29,"value":1720},{"type":23,"tag":218,"props":2641,"children":2642},{"style":225},[2643],{"type":29,"value":1725},{"type":23,"tag":218,"props":2645,"children":2646},{"style":242},[2647],{"type":29,"value":2648}," {\n",{"type":23,"tag":218,"props":2650,"children":2651},{"class":220,"line":1208},[2652],{"type":23,"tag":218,"props":2653,"children":2654},{"style":463},[2655],{"type":29,"value":2656},"        // No Children, current node is parents LEFT node\n",{"type":23,"tag":218,"props":2658,"children":2659},{"class":220,"line":1233},[2660,2665,2670,2674,2678,2682,2686,2690,2694,2698,2703,2708],{"type":23,"tag":218,"props":2661,"children":2662},{"style":225},[2663],{"type":29,"value":2664},"        if",{"type":23,"tag":218,"props":2666,"children":2667},{"style":242},[2668],{"type":29,"value":2669}," (left ",{"type":23,"tag":218,"props":2671,"children":2672},{"style":225},[2673],{"type":29,"value":438},{"type":23,"tag":218,"props":2675,"children":2676},{"style":305},[2677],{"type":29,"value":308},{"type":23,"tag":218,"props":2679,"children":2680},{"style":225},[2681],{"type":29,"value":1661},{"type":23,"tag":218,"props":2683,"children":2684},{"style":242},[2685],{"type":29,"value":2115},{"type":23,"tag":218,"props":2687,"children":2688},{"style":225},[2689],{"type":29,"value":438},{"type":23,"tag":218,"props":2691,"children":2692},{"style":305},[2693],{"type":29,"value":2033},{"type":23,"tag":218,"props":2695,"children":2696},{"style":225},[2697],{"type":29,"value":1661},{"type":23,"tag":218,"props":2699,"children":2700},{"style":305},[2701],{"type":29,"value":2702}," this",{"type":23,"tag":218,"props":2704,"children":2705},{"style":225},[2706],{"type":29,"value":2707}," ==",{"type":23,"tag":218,"props":2709,"children":2710},{"style":242},[2711],{"type":29,"value":2712}," parent.left) {\n",{"type":23,"tag":218,"props":2714,"children":2715},{"class":220,"line":1250},[2716,2721,2725],{"type":23,"tag":218,"props":2717,"children":2718},{"style":242},[2719],{"type":29,"value":2720},"            parent.left ",{"type":23,"tag":218,"props":2722,"children":2723},{"style":225},[2724],{"type":29,"value":302},{"type":23,"tag":218,"props":2726,"children":2727},{"style":305},[2728],{"type":29,"value":342},{"type":23,"tag":218,"props":2730,"children":2731},{"class":220,"line":1258},[2732],{"type":23,"tag":218,"props":2733,"children":2734},{"style":463},[2735],{"type":29,"value":2736},"        // No Children, current node is parents RIGHT node\n",{"type":23,"tag":218,"props":2738,"children":2739},{"class":220,"line":1267},[2740,2745,2749,2753,2757,2761,2765,2769,2773,2777,2781,2785,2789,2793],{"type":23,"tag":218,"props":2741,"children":2742},{"style":242},[2743],{"type":29,"value":2744},"        }",{"type":23,"tag":218,"props":2746,"children":2747},{"style":225},[2748],{"type":29,"value":1725},{"type":23,"tag":218,"props":2750,"children":2751},{"style":225},[2752],{"type":29,"value":1730},{"type":23,"tag":218,"props":2754,"children":2755},{"style":242},[2756],{"type":29,"value":2669},{"type":23,"tag":218,"props":2758,"children":2759},{"style":225},[2760],{"type":29,"value":438},{"type":23,"tag":218,"props":2762,"children":2763},{"style":305},[2764],{"type":29,"value":308},{"type":23,"tag":218,"props":2766,"children":2767},{"style":225},[2768],{"type":29,"value":1661},{"type":23,"tag":218,"props":2770,"children":2771},{"style":242},[2772],{"type":29,"value":2115},{"type":23,"tag":218,"props":2774,"children":2775},{"style":225},[2776],{"type":29,"value":438},{"type":23,"tag":218,"props":2778,"children":2779},{"style":305},[2780],{"type":29,"value":2033},{"type":23,"tag":218,"props":2782,"children":2783},{"style":225},[2784],{"type":29,"value":1661},{"type":23,"tag":218,"props":2786,"children":2787},{"style":305},[2788],{"type":29,"value":2702},{"type":23,"tag":218,"props":2790,"children":2791},{"style":225},[2792],{"type":29,"value":438},{"type":23,"tag":218,"props":2794,"children":2795},{"style":242},[2796],{"type":29,"value":2797},"parent.right){\n",{"type":23,"tag":218,"props":2799,"children":2801},{"class":220,"line":2800},19,[2802,2807,2811],{"type":23,"tag":218,"props":2803,"children":2804},{"style":242},[2805],{"type":29,"value":2806},"            parent.right ",{"type":23,"tag":218,"props":2808,"children":2809},{"style":225},[2810],{"type":29,"value":302},{"type":23,"tag":218,"props":2812,"children":2813},{"style":305},[2814],{"type":29,"value":342},{"type":23,"tag":218,"props":2816,"children":2818},{"class":220,"line":2817},20,[2819],{"type":23,"tag":218,"props":2820,"children":2821},{"style":463},[2822],{"type":29,"value":2823},"        // ONLY LEFT child, current node is parents LEFT node\n",{"type":23,"tag":218,"props":2825,"children":2827},{"class":220,"line":2826},21,[2828,2832,2836,2840,2845,2849,2853,2857,2861,2865,2869,2873,2877,2881],{"type":23,"tag":218,"props":2829,"children":2830},{"style":242},[2831],{"type":29,"value":2744},{"type":23,"tag":218,"props":2833,"children":2834},{"style":225},[2835],{"type":29,"value":1725},{"type":23,"tag":218,"props":2837,"children":2838},{"style":225},[2839],{"type":29,"value":1730},{"type":23,"tag":218,"props":2841,"children":2842},{"style":242},[2843],{"type":29,"value":2844},"(left",{"type":23,"tag":218,"props":2846,"children":2847},{"style":225},[2848],{"type":29,"value":1170},{"type":23,"tag":218,"props":2850,"children":2851},{"style":305},[2852],{"type":29,"value":2033},{"type":23,"tag":218,"props":2854,"children":2855},{"style":225},[2856],{"type":29,"value":1661},{"type":23,"tag":218,"props":2858,"children":2859},{"style":242},[2860],{"type":29,"value":2115},{"type":23,"tag":218,"props":2862,"children":2863},{"style":225},[2864],{"type":29,"value":438},{"type":23,"tag":218,"props":2866,"children":2867},{"style":305},[2868],{"type":29,"value":2033},{"type":23,"tag":218,"props":2870,"children":2871},{"style":225},[2872],{"type":29,"value":1661},{"type":23,"tag":218,"props":2874,"children":2875},{"style":305},[2876],{"type":29,"value":2702},{"type":23,"tag":218,"props":2878,"children":2879},{"style":225},[2880],{"type":29,"value":2707},{"type":23,"tag":218,"props":2882,"children":2883},{"style":242},[2884],{"type":29,"value":2885}," parent.left){\n",{"type":23,"tag":218,"props":2887,"children":2889},{"class":220,"line":2888},22,[2890,2894,2898,2902],{"type":23,"tag":218,"props":2891,"children":2892},{"style":242},[2893],{"type":29,"value":2720},{"type":23,"tag":218,"props":2895,"children":2896},{"style":225},[2897],{"type":29,"value":302},{"type":23,"tag":218,"props":2899,"children":2900},{"style":305},[2901],{"type":29,"value":2702},{"type":23,"tag":218,"props":2903,"children":2904},{"style":242},[2905],{"type":29,"value":2906},".left\n",{"type":23,"tag":218,"props":2908,"children":2910},{"class":220,"line":2909},23,[2911],{"type":23,"tag":218,"props":2912,"children":2913},{"style":463},[2914],{"type":29,"value":2915},"        // ONLY LEFT child, current node is parents RIGHT node\n",{"type":23,"tag":218,"props":2917,"children":2919},{"class":220,"line":2918},24,[2920,2924,2928,2932,2936,2940,2944,2948,2952,2956,2960,2964,2968,2972],{"type":23,"tag":218,"props":2921,"children":2922},{"style":242},[2923],{"type":29,"value":2744},{"type":23,"tag":218,"props":2925,"children":2926},{"style":225},[2927],{"type":29,"value":1725},{"type":23,"tag":218,"props":2929,"children":2930},{"style":225},[2931],{"type":29,"value":1730},{"type":23,"tag":218,"props":2933,"children":2934},{"style":242},[2935],{"type":29,"value":2844},{"type":23,"tag":218,"props":2937,"children":2938},{"style":225},[2939],{"type":29,"value":1170},{"type":23,"tag":218,"props":2941,"children":2942},{"style":305},[2943],{"type":29,"value":2033},{"type":23,"tag":218,"props":2945,"children":2946},{"style":225},[2947],{"type":29,"value":1661},{"type":23,"tag":218,"props":2949,"children":2950},{"style":242},[2951],{"type":29,"value":2115},{"type":23,"tag":218,"props":2953,"children":2954},{"style":225},[2955],{"type":29,"value":438},{"type":23,"tag":218,"props":2957,"children":2958},{"style":305},[2959],{"type":29,"value":308},{"type":23,"tag":218,"props":2961,"children":2962},{"style":225},[2963],{"type":29,"value":1661},{"type":23,"tag":218,"props":2965,"children":2966},{"style":305},[2967],{"type":29,"value":2702},{"type":23,"tag":218,"props":2969,"children":2970},{"style":225},[2971],{"type":29,"value":2707},{"type":23,"tag":218,"props":2973,"children":2974},{"style":242},[2975],{"type":29,"value":2976}," parent.right){\n",{"type":23,"tag":218,"props":2978,"children":2980},{"class":220,"line":2979},25,[2981,2985,2989,2993],{"type":23,"tag":218,"props":2982,"children":2983},{"style":242},[2984],{"type":29,"value":2806},{"type":23,"tag":218,"props":2986,"children":2987},{"style":225},[2988],{"type":29,"value":302},{"type":23,"tag":218,"props":2990,"children":2991},{"style":305},[2992],{"type":29,"value":2702},{"type":23,"tag":218,"props":2994,"children":2995},{"style":242},[2996],{"type":29,"value":2906},{"type":23,"tag":218,"props":2998,"children":3000},{"class":220,"line":2999},26,[3001],{"type":23,"tag":218,"props":3002,"children":3003},{"style":463},[3004],{"type":29,"value":3005},"        // ONLY RIGHT child, current node is parents LEFT node\n",{"type":23,"tag":218,"props":3007,"children":3009},{"class":220,"line":3008},27,[3010,3014,3018,3022,3027,3031,3035,3039,3043,3047,3051,3055,3059,3063],{"type":23,"tag":218,"props":3011,"children":3012},{"style":242},[3013],{"type":29,"value":2744},{"type":23,"tag":218,"props":3015,"children":3016},{"style":225},[3017],{"type":29,"value":1725},{"type":23,"tag":218,"props":3019,"children":3020},{"style":225},[3021],{"type":29,"value":1730},{"type":23,"tag":218,"props":3023,"children":3024},{"style":242},[3025],{"type":29,"value":3026}," (right",{"type":23,"tag":218,"props":3028,"children":3029},{"style":225},[3030],{"type":29,"value":1170},{"type":23,"tag":218,"props":3032,"children":3033},{"style":305},[3034],{"type":29,"value":2033},{"type":23,"tag":218,"props":3036,"children":3037},{"style":225},[3038],{"type":29,"value":1661},{"type":23,"tag":218,"props":3040,"children":3041},{"style":242},[3042],{"type":29,"value":2051},{"type":23,"tag":218,"props":3044,"children":3045},{"style":225},[3046],{"type":29,"value":438},{"type":23,"tag":218,"props":3048,"children":3049},{"style":305},[3050],{"type":29,"value":2033},{"type":23,"tag":218,"props":3052,"children":3053},{"style":225},[3054],{"type":29,"value":1661},{"type":23,"tag":218,"props":3056,"children":3057},{"style":305},[3058],{"type":29,"value":2702},{"type":23,"tag":218,"props":3060,"children":3061},{"style":225},[3062],{"type":29,"value":2707},{"type":23,"tag":218,"props":3064,"children":3065},{"style":242},[3066],{"type":29,"value":2712},{"type":23,"tag":218,"props":3068,"children":3070},{"class":220,"line":3069},28,[3071,3075,3079,3083],{"type":23,"tag":218,"props":3072,"children":3073},{"style":242},[3074],{"type":29,"value":2720},{"type":23,"tag":218,"props":3076,"children":3077},{"style":225},[3078],{"type":29,"value":302},{"type":23,"tag":218,"props":3080,"children":3081},{"style":305},[3082],{"type":29,"value":2702},{"type":23,"tag":218,"props":3084,"children":3085},{"style":242},[3086],{"type":29,"value":3087},".right\n",{"type":23,"tag":218,"props":3089,"children":3091},{"class":220,"line":3090},29,[3092],{"type":23,"tag":218,"props":3093,"children":3094},{"style":463},[3095],{"type":29,"value":3096},"        // ONLY RIGHT child, current node is parents RIGHT node\n",{"type":23,"tag":218,"props":3098,"children":3100},{"class":220,"line":3099},30,[3101,3105,3109,3113,3118,3122,3126,3130,3134,3138,3142,3146,3150,3154],{"type":23,"tag":218,"props":3102,"children":3103},{"style":242},[3104],{"type":29,"value":2744},{"type":23,"tag":218,"props":3106,"children":3107},{"style":225},[3108],{"type":29,"value":1725},{"type":23,"tag":218,"props":3110,"children":3111},{"style":225},[3112],{"type":29,"value":1730},{"type":23,"tag":218,"props":3114,"children":3115},{"style":242},[3116],{"type":29,"value":3117},"(right",{"type":23,"tag":218,"props":3119,"children":3120},{"style":225},[3121],{"type":29,"value":1170},{"type":23,"tag":218,"props":3123,"children":3124},{"style":305},[3125],{"type":29,"value":2033},{"type":23,"tag":218,"props":3127,"children":3128},{"style":225},[3129],{"type":29,"value":1661},{"type":23,"tag":218,"props":3131,"children":3132},{"style":242},[3133],{"type":29,"value":2051},{"type":23,"tag":218,"props":3135,"children":3136},{"style":225},[3137],{"type":29,"value":438},{"type":23,"tag":218,"props":3139,"children":3140},{"style":305},[3141],{"type":29,"value":2033},{"type":23,"tag":218,"props":3143,"children":3144},{"style":225},[3145],{"type":29,"value":1661},{"type":23,"tag":218,"props":3147,"children":3148},{"style":305},[3149],{"type":29,"value":2702},{"type":23,"tag":218,"props":3151,"children":3152},{"style":225},[3153],{"type":29,"value":2707},{"type":23,"tag":218,"props":3155,"children":3156},{"style":242},[3157],{"type":29,"value":3158}," parent.right) {\n",{"type":23,"tag":218,"props":3160,"children":3162},{"class":220,"line":3161},31,[3163,3167,3171,3175],{"type":23,"tag":218,"props":3164,"children":3165},{"style":242},[3166],{"type":29,"value":2806},{"type":23,"tag":218,"props":3168,"children":3169},{"style":225},[3170],{"type":29,"value":302},{"type":23,"tag":218,"props":3172,"children":3173},{"style":305},[3174],{"type":29,"value":2702},{"type":23,"tag":218,"props":3176,"children":3177},{"style":242},[3178],{"type":29,"value":3087},{"type":23,"tag":218,"props":3180,"children":3182},{"class":220,"line":3181},32,[3183],{"type":23,"tag":218,"props":3184,"children":3185},{"style":463},[3186],{"type":29,"value":3187},"        // BOTH Children exists, replace current data with min value from right \n",{"type":23,"tag":218,"props":3189,"children":3191},{"class":220,"line":3190},33,[3192,3196,3200],{"type":23,"tag":218,"props":3193,"children":3194},{"style":242},[3195],{"type":29,"value":2744},{"type":23,"tag":218,"props":3197,"children":3198},{"style":225},[3199],{"type":29,"value":1725},{"type":23,"tag":218,"props":3201,"children":3202},{"style":242},[3203],{"type":29,"value":1876},{"type":23,"tag":218,"props":3205,"children":3207},{"class":220,"line":3206},34,[3208,3213,3218,3222,3226,3230,3235],{"type":23,"tag":218,"props":3209,"children":3210},{"style":225},[3211],{"type":29,"value":3212},"            data",{"type":23,"tag":218,"props":3214,"children":3215},{"style":225},[3216],{"type":29,"value":3217}," =",{"type":23,"tag":218,"props":3219,"children":3220},{"style":242},[3221],{"type":29,"value":2115},{"type":23,"tag":218,"props":3223,"children":3224},{"style":225},[3225],{"type":29,"value":1691},{"type":23,"tag":218,"props":3227,"children":3228},{"style":242},[3229],{"type":29,"value":199},{"type":23,"tag":218,"props":3231,"children":3232},{"style":236},[3233],{"type":29,"value":3234},"min",{"type":23,"tag":218,"props":3236,"children":3237},{"style":242},[3238],{"type":29,"value":1127},{"type":23,"tag":218,"props":3240,"children":3242},{"class":220,"line":3241},35,[3243,3248,3252,3256,3260,3264,3268,3272,3276],{"type":23,"tag":218,"props":3244,"children":3245},{"style":242},[3246],{"type":29,"value":3247},"            right",{"type":23,"tag":218,"props":3249,"children":3250},{"style":225},[3251],{"type":29,"value":1691},{"type":23,"tag":218,"props":3253,"children":3254},{"style":242},[3255],{"type":29,"value":199},{"type":23,"tag":218,"props":3257,"children":3258},{"style":236},[3259],{"type":29,"value":2430},{"type":23,"tag":218,"props":3261,"children":3262},{"style":242},[3263],{"type":29,"value":1080},{"type":23,"tag":218,"props":3265,"children":3266},{"style":225},[3267],{"type":29,"value":228},{"type":23,"tag":218,"props":3269,"children":3270},{"style":242},[3271],{"type":29,"value":2443},{"type":23,"tag":218,"props":3273,"children":3274},{"style":305},[3275],{"type":29,"value":2448},{"type":23,"tag":218,"props":3277,"children":3278},{"style":242},[3279],{"type":29,"value":351},{"type":23,"tag":218,"props":3281,"children":3283},{"class":220,"line":3282},36,[3284],{"type":23,"tag":218,"props":3285,"children":3286},{"style":242},[3287],{"type":29,"value":1264},{"type":23,"tag":218,"props":3289,"children":3291},{"class":220,"line":3290},37,[3292,3296],{"type":23,"tag":218,"props":3293,"children":3294},{"style":225},[3295],{"type":29,"value":2046},{"type":23,"tag":218,"props":3297,"children":3298},{"style":305},[3299],{"type":29,"value":3300}," true\n",{"type":23,"tag":218,"props":3302,"children":3304},{"class":220,"line":3303},38,[3305],{"type":23,"tag":218,"props":3306,"children":3307},{"style":242},[3308],{"type":29,"value":1273},{"type":23,"tag":218,"props":3310,"children":3312},{"class":220,"line":3311},39,[3313],{"type":23,"tag":218,"props":3314,"children":3315},{"style":242},[3316],{"type":29,"value":538},{"type":23,"tag":218,"props":3318,"children":3320},{"class":220,"line":3319},40,[3321],{"type":23,"tag":218,"props":3322,"children":3323},{"emptyLinePlaceholder":964},[3324],{"type":29,"value":967},{"type":23,"tag":218,"props":3326,"children":3328},{"class":220,"line":3327},41,[3329,3333,3338,3343,3347,3351,3355,3360,3364,3368,3372,3376,3381,3385,3389,3393,3397],{"type":23,"tag":218,"props":3330,"children":3331},{"style":225},[3332],{"type":29,"value":374},{"type":23,"tag":218,"props":3334,"children":3335},{"style":236},[3336],{"type":29,"value":3337}," min",{"type":23,"tag":218,"props":3339,"children":3340},{"style":242},[3341],{"type":29,"value":3342},"() : ",{"type":23,"tag":218,"props":3344,"children":3345},{"style":236},[3346],{"type":29,"value":269},{"type":23,"tag":218,"props":3348,"children":3349},{"style":225},[3350],{"type":29,"value":3217},{"type":23,"tag":218,"props":3352,"children":3353},{"style":225},[3354],{"type":29,"value":1730},{"type":23,"tag":218,"props":3356,"children":3357},{"style":242},[3358],{"type":29,"value":3359}," (left",{"type":23,"tag":218,"props":3361,"children":3362},{"style":225},[3363],{"type":29,"value":438},{"type":23,"tag":218,"props":3365,"children":3366},{"style":305},[3367],{"type":29,"value":2033},{"type":23,"tag":218,"props":3369,"children":3370},{"style":242},[3371],{"type":29,"value":447},{"type":23,"tag":218,"props":3373,"children":3374},{"style":225},[3375],{"type":29,"value":228},{"type":23,"tag":218,"props":3377,"children":3378},{"style":225},[3379],{"type":29,"value":3380}," else",{"type":23,"tag":218,"props":3382,"children":3383},{"style":242},[3384],{"type":29,"value":2051},{"type":23,"tag":218,"props":3386,"children":3387},{"style":225},[3388],{"type":29,"value":1691},{"type":23,"tag":218,"props":3390,"children":3391},{"style":242},[3392],{"type":29,"value":199},{"type":23,"tag":218,"props":3394,"children":3395},{"style":236},[3396],{"type":29,"value":3234},{"type":23,"tag":218,"props":3398,"children":3399},{"style":242},[3400],{"type":29,"value":1127},{"type":23,"tag":36,"props":3402,"children":3403},{},[3404,3406],{"type":29,"value":3405},"You can find the complete code of this example in this\n",{"type":23,"tag":48,"props":3407,"children":3409},{"href":3408,"target":1282},"https://github.com/JaimeDeArcos/kotlin-algorithms/blob/main/trees/src/main/kotlin/es/jaimedearcos/algorithms/tree/data/BinarySearchTreeNode.kt",[3410],{"type":29,"value":3411},"\nGitHub repository\n",{"type":23,"tag":1287,"props":3413,"children":3414},{},[3415],{"type":29,"value":1291},{"title":7,"searchDepth":248,"depth":248,"links":3417},[3418,3419,3420,3421],{"id":1371,"depth":248,"text":1374},{"id":1534,"depth":248,"text":1537},{"id":1921,"depth":248,"text":1924},{"id":2192,"depth":248,"text":2195},"content:posts:algorithms-binary-search-tree.md","posts/algorithms-binary-search-tree.md","posts/algorithms-binary-search-tree",{"_path":3426,"_dir":5,"_draft":6,"_partial":6,"_locale":7,"title":3427,"description":3428,"date":1310,"layout":11,"subtitle":3428,"image":3429,"optimized_image":3429,"category":13,"tags":3430,"author":18,"paginate":6,"body":3434,"_type":1299,"_id":4553,"_source":1301,"_file":4554,"_stem":4555,"_extension":1304},"/posts/how-to-create-a-twitter-bot","How to create a Twitter bot","Step by step creation of a twitter bot","/assets/img/uploads/twitter.jpg",[3431,3432,3433],"bots","twitter","python",{"type":20,"children":3435,"toc":4542},[3436,3442,3452,3464,3471,3478,3484,3496,3560,3572,3767,3772,3791,3797,3802,3811,3943,3949,3973,3979,4112,4118,4123,4141,4496,4506,4510,4516,4535,4538],{"type":23,"tag":24,"props":3437,"children":3439},{"id":3438},"_1-request-your-developer-account-create-your-first-app",[3440],{"type":29,"value":3441},"1. Request your developer account & create your first app",{"type":23,"tag":36,"props":3443,"children":3444},{},[3445,3447],{"type":29,"value":3446},"The first step of building twitter bots is to request access to developer features, this can be done easily\nin ",{"type":23,"tag":48,"props":3448,"children":3450},{"href":3449,"target":1282},"developer.twitter.com",[3451],{"type":29,"value":3449},{"type":23,"tag":36,"props":3453,"children":3454},{},[3455,3457,3462],{"type":29,"value":3456},"Once you have fill the form to request access, you can log in the ",{"type":23,"tag":70,"props":3458,"children":3459},{},[3460],{"type":29,"value":3461},"twitter console",{"type":29,"value":3463}," to retrieve the access keys:",{"type":23,"tag":36,"props":3465,"children":3466},{},[3467],{"type":23,"tag":137,"props":3468,"children":3470},{"alt":7,"src":3469},"/assets/img/uploads/twitter-dashboard.png",[],{"type":23,"tag":36,"props":3472,"children":3473},{},[3474],{"type":23,"tag":137,"props":3475,"children":3477},{"alt":7,"src":3476},"/assets/img/uploads/twitter-tokens.png",[],{"type":23,"tag":24,"props":3479,"children":3481},{"id":3480},"_2-set-up-dev-environment",[3482],{"type":29,"value":3483},"2. Set-up dev environment",{"type":23,"tag":36,"props":3485,"children":3486},{},[3487,3489,3494],{"type":29,"value":3488},"We are going to use ",{"type":23,"tag":70,"props":3490,"children":3491},{},[3492],{"type":29,"value":3493},"Tweepy",{"type":29,"value":3495},", a python library to facilitate the communication with twitter API. To create the python\nenvironment and install Tweepy we need to execute the following commands:",{"type":23,"tag":209,"props":3497,"children":3501},{"className":3498,"code":3499,"language":3500,"meta":7,"style":7},"language-bash shiki shiki-themes github-dark github-light","python3 -m venv venv\nsource ./venv/bin/activate\npip install tweepy\n","bash",[3502],{"type":23,"tag":15,"props":3503,"children":3504},{"__ignoreMap":7},[3505,3529,3542],{"type":23,"tag":218,"props":3506,"children":3507},{"class":220,"line":221},[3508,3513,3518,3524],{"type":23,"tag":218,"props":3509,"children":3510},{"style":236},[3511],{"type":29,"value":3512},"python3",{"type":23,"tag":218,"props":3514,"children":3515},{"style":305},[3516],{"type":29,"value":3517}," -m",{"type":23,"tag":218,"props":3519,"children":3521},{"style":3520},"--shiki-default:#9ECBFF;--shiki-light:#032F62",[3522],{"type":29,"value":3523}," venv",{"type":23,"tag":218,"props":3525,"children":3526},{"style":3520},[3527],{"type":29,"value":3528}," venv\n",{"type":23,"tag":218,"props":3530,"children":3531},{"class":220,"line":248},[3532,3537],{"type":23,"tag":218,"props":3533,"children":3534},{"style":305},[3535],{"type":29,"value":3536},"source",{"type":23,"tag":218,"props":3538,"children":3539},{"style":3520},[3540],{"type":29,"value":3541}," ./venv/bin/activate\n",{"type":23,"tag":218,"props":3543,"children":3544},{"class":220,"line":277},[3545,3550,3555],{"type":23,"tag":218,"props":3546,"children":3547},{"style":236},[3548],{"type":29,"value":3549},"pip",{"type":23,"tag":218,"props":3551,"children":3552},{"style":3520},[3553],{"type":29,"value":3554}," install",{"type":23,"tag":218,"props":3556,"children":3557},{"style":3520},[3558],{"type":29,"value":3559}," tweepy\n",{"type":23,"tag":36,"props":3561,"children":3562},{},[3563,3565,3570],{"type":29,"value":3564},"After that we need to create our first python script to verify keys and environment (",{"type":23,"tag":70,"props":3566,"children":3567},{},[3568],{"type":29,"value":3569},"test-credentials.py",{"type":29,"value":3571},")",{"type":23,"tag":209,"props":3573,"children":3576},{"className":3574,"code":3575,"language":3433,"meta":7,"style":7},"language-python shiki shiki-themes github-dark github-light","import tweepy\n\n# Authentication\nauth = tweepy.OAuthHandler(\"CONSUMER_API_KEY\", \"CONSUMER_API_SECRET\")\nauth.set_access_token(\"ACCESS_TOKEN\", \"ACCESS_TOKEN_SECRET\")\napi = tweepy.API(auth)\n\ntry:\n    api.verify_credentials()\n    print(\"Authentication OK\")\nexcept:\n    print(\"Error during authentication\")\n",[3577],{"type":23,"tag":15,"props":3578,"children":3579},{"__ignoreMap":7},[3580,3592,3599,3607,3643,3669,3686,3693,3706,3714,3735,3747],{"type":23,"tag":218,"props":3581,"children":3582},{"class":220,"line":221},[3583,3588],{"type":23,"tag":218,"props":3584,"children":3585},{"style":225},[3586],{"type":29,"value":3587},"import",{"type":23,"tag":218,"props":3589,"children":3590},{"style":242},[3591],{"type":29,"value":3559},{"type":23,"tag":218,"props":3593,"children":3594},{"class":220,"line":248},[3595],{"type":23,"tag":218,"props":3596,"children":3597},{"emptyLinePlaceholder":964},[3598],{"type":29,"value":967},{"type":23,"tag":218,"props":3600,"children":3601},{"class":220,"line":277},[3602],{"type":23,"tag":218,"props":3603,"children":3604},{"style":463},[3605],{"type":29,"value":3606},"# Authentication\n",{"type":23,"tag":218,"props":3608,"children":3609},{"class":220,"line":315},[3610,3615,3619,3624,3629,3634,3639],{"type":23,"tag":218,"props":3611,"children":3612},{"style":242},[3613],{"type":29,"value":3614},"auth ",{"type":23,"tag":218,"props":3616,"children":3617},{"style":225},[3618],{"type":29,"value":302},{"type":23,"tag":218,"props":3620,"children":3621},{"style":242},[3622],{"type":29,"value":3623}," tweepy.OAuthHandler(",{"type":23,"tag":218,"props":3625,"children":3626},{"style":3520},[3627],{"type":29,"value":3628},"\"CONSUMER_API_KEY\"",{"type":23,"tag":218,"props":3630,"children":3631},{"style":242},[3632],{"type":29,"value":3633},", ",{"type":23,"tag":218,"props":3635,"children":3636},{"style":3520},[3637],{"type":29,"value":3638},"\"CONSUMER_API_SECRET\"",{"type":23,"tag":218,"props":3640,"children":3641},{"style":242},[3642],{"type":29,"value":351},{"type":23,"tag":218,"props":3644,"children":3645},{"class":220,"line":345},[3646,3651,3656,3660,3665],{"type":23,"tag":218,"props":3647,"children":3648},{"style":242},[3649],{"type":29,"value":3650},"auth.set_access_token(",{"type":23,"tag":218,"props":3652,"children":3653},{"style":3520},[3654],{"type":29,"value":3655},"\"ACCESS_TOKEN\"",{"type":23,"tag":218,"props":3657,"children":3658},{"style":242},[3659],{"type":29,"value":3633},{"type":23,"tag":218,"props":3661,"children":3662},{"style":3520},[3663],{"type":29,"value":3664},"\"ACCESS_TOKEN_SECRET\"",{"type":23,"tag":218,"props":3666,"children":3667},{"style":242},[3668],{"type":29,"value":351},{"type":23,"tag":218,"props":3670,"children":3671},{"class":220,"line":487},[3672,3677,3681],{"type":23,"tag":218,"props":3673,"children":3674},{"style":242},[3675],{"type":29,"value":3676},"api ",{"type":23,"tag":218,"props":3678,"children":3679},{"style":225},[3680],{"type":29,"value":302},{"type":23,"tag":218,"props":3682,"children":3683},{"style":242},[3684],{"type":29,"value":3685}," tweepy.API(auth)\n",{"type":23,"tag":218,"props":3687,"children":3688},{"class":220,"line":496},[3689],{"type":23,"tag":218,"props":3690,"children":3691},{"emptyLinePlaceholder":964},[3692],{"type":29,"value":967},{"type":23,"tag":218,"props":3694,"children":3695},{"class":220,"line":510},[3696,3701],{"type":23,"tag":218,"props":3697,"children":3698},{"style":225},[3699],{"type":29,"value":3700},"try",{"type":23,"tag":218,"props":3702,"children":3703},{"style":242},[3704],{"type":29,"value":3705},":\n",{"type":23,"tag":218,"props":3707,"children":3708},{"class":220,"line":519},[3709],{"type":23,"tag":218,"props":3710,"children":3711},{"style":242},[3712],{"type":29,"value":3713},"    api.verify_credentials()\n",{"type":23,"tag":218,"props":3715,"children":3716},{"class":220,"line":532},[3717,3722,3726,3731],{"type":23,"tag":218,"props":3718,"children":3719},{"style":305},[3720],{"type":29,"value":3721},"    print",{"type":23,"tag":218,"props":3723,"children":3724},{"style":242},[3725],{"type":29,"value":1080},{"type":23,"tag":218,"props":3727,"children":3728},{"style":3520},[3729],{"type":29,"value":3730},"\"Authentication OK\"",{"type":23,"tag":218,"props":3732,"children":3733},{"style":242},[3734],{"type":29,"value":351},{"type":23,"tag":218,"props":3736,"children":3737},{"class":220,"line":1154},[3738,3743],{"type":23,"tag":218,"props":3739,"children":3740},{"style":225},[3741],{"type":29,"value":3742},"except",{"type":23,"tag":218,"props":3744,"children":3745},{"style":242},[3746],{"type":29,"value":3705},{"type":23,"tag":218,"props":3748,"children":3749},{"class":220,"line":1181},[3750,3754,3758,3763],{"type":23,"tag":218,"props":3751,"children":3752},{"style":305},[3753],{"type":29,"value":3721},{"type":23,"tag":218,"props":3755,"children":3756},{"style":242},[3757],{"type":29,"value":1080},{"type":23,"tag":218,"props":3759,"children":3760},{"style":3520},[3761],{"type":29,"value":3762},"\"Error during authentication\"",{"type":23,"tag":218,"props":3764,"children":3765},{"style":242},[3766],{"type":29,"value":351},{"type":23,"tag":36,"props":3768,"children":3769},{},[3770],{"type":29,"value":3771},"Run the script by executing:",{"type":23,"tag":209,"props":3773,"children":3775},{"className":3498,"code":3774,"language":3500,"meta":7,"style":7},"python test-credentials.py  \n",[3776],{"type":23,"tag":15,"props":3777,"children":3778},{"__ignoreMap":7},[3779],{"type":23,"tag":218,"props":3780,"children":3781},{"class":220,"line":221},[3782,3786],{"type":23,"tag":218,"props":3783,"children":3784},{"style":236},[3785],{"type":29,"value":3433},{"type":23,"tag":218,"props":3787,"children":3788},{"style":3520},[3789],{"type":29,"value":3790}," test-credentials.py\n",{"type":23,"tag":24,"props":3792,"children":3794},{"id":3793},"_3-playing-with-the-api",[3795],{"type":29,"value":3796},"3. Playing with the API",{"type":23,"tag":36,"props":3798,"children":3799},{},[3800],{"type":29,"value":3801},"Tweepy provides a really simple way to retrieve/create twitter information.",{"type":23,"tag":182,"props":3803,"children":3805},{"id":3804},"retrieve-your-timeline",[3806],{"type":23,"tag":70,"props":3807,"children":3808},{},[3809],{"type":29,"value":3810},"Retrieve your timeline",{"type":23,"tag":209,"props":3812,"children":3814},{"className":3574,"code":3813,"language":3433,"meta":7,"style":7},"# ...\n\n# Retrieve timeline\ntimeline = api.home_timeline()\nfor tweet in timeline:\n    print(f\"{tweet.user.name} said {tweet.text}\")\n",[3815],{"type":23,"tag":15,"props":3816,"children":3817},{"__ignoreMap":7},[3818,3826,3833,3841,3858,3881],{"type":23,"tag":218,"props":3819,"children":3820},{"class":220,"line":221},[3821],{"type":23,"tag":218,"props":3822,"children":3823},{"style":463},[3824],{"type":29,"value":3825},"# ...\n",{"type":23,"tag":218,"props":3827,"children":3828},{"class":220,"line":248},[3829],{"type":23,"tag":218,"props":3830,"children":3831},{"emptyLinePlaceholder":964},[3832],{"type":29,"value":967},{"type":23,"tag":218,"props":3834,"children":3835},{"class":220,"line":277},[3836],{"type":23,"tag":218,"props":3837,"children":3838},{"style":463},[3839],{"type":29,"value":3840},"# Retrieve timeline\n",{"type":23,"tag":218,"props":3842,"children":3843},{"class":220,"line":315},[3844,3849,3853],{"type":23,"tag":218,"props":3845,"children":3846},{"style":242},[3847],{"type":29,"value":3848},"timeline ",{"type":23,"tag":218,"props":3850,"children":3851},{"style":225},[3852],{"type":29,"value":302},{"type":23,"tag":218,"props":3854,"children":3855},{"style":242},[3856],{"type":29,"value":3857}," api.home_timeline()\n",{"type":23,"tag":218,"props":3859,"children":3860},{"class":220,"line":345},[3861,3866,3871,3876],{"type":23,"tag":218,"props":3862,"children":3863},{"style":225},[3864],{"type":29,"value":3865},"for",{"type":23,"tag":218,"props":3867,"children":3868},{"style":242},[3869],{"type":29,"value":3870}," tweet ",{"type":23,"tag":218,"props":3872,"children":3873},{"style":225},[3874],{"type":29,"value":3875},"in",{"type":23,"tag":218,"props":3877,"children":3878},{"style":242},[3879],{"type":29,"value":3880}," timeline:\n",{"type":23,"tag":218,"props":3882,"children":3883},{"class":220,"line":487},[3884,3888,3892,3897,3902,3907,3912,3917,3922,3926,3931,3935,3939],{"type":23,"tag":218,"props":3885,"children":3886},{"style":305},[3887],{"type":29,"value":3721},{"type":23,"tag":218,"props":3889,"children":3890},{"style":242},[3891],{"type":29,"value":1080},{"type":23,"tag":218,"props":3893,"children":3894},{"style":225},[3895],{"type":29,"value":3896},"f",{"type":23,"tag":218,"props":3898,"children":3899},{"style":3520},[3900],{"type":29,"value":3901},"\"",{"type":23,"tag":218,"props":3903,"children":3904},{"style":305},[3905],{"type":29,"value":3906},"{",{"type":23,"tag":218,"props":3908,"children":3909},{"style":242},[3910],{"type":29,"value":3911},"tweet.user.name",{"type":23,"tag":218,"props":3913,"children":3914},{"style":305},[3915],{"type":29,"value":3916},"}",{"type":23,"tag":218,"props":3918,"children":3919},{"style":3520},[3920],{"type":29,"value":3921}," said ",{"type":23,"tag":218,"props":3923,"children":3924},{"style":305},[3925],{"type":29,"value":3906},{"type":23,"tag":218,"props":3927,"children":3928},{"style":242},[3929],{"type":29,"value":3930},"tweet.text",{"type":23,"tag":218,"props":3932,"children":3933},{"style":305},[3934],{"type":29,"value":3916},{"type":23,"tag":218,"props":3936,"children":3937},{"style":3520},[3938],{"type":29,"value":3901},{"type":23,"tag":218,"props":3940,"children":3941},{"style":242},[3942],{"type":29,"value":351},{"type":23,"tag":182,"props":3944,"children":3946},{"id":3945},"publish-a-new-tweet",[3947],{"type":29,"value":3948},"Publish a new tweet",{"type":23,"tag":209,"props":3950,"children":3952},{"className":3574,"code":3951,"language":3433,"meta":7,"style":7},"api.update_status(\"Hello World from Tweepy\")\n",[3953],{"type":23,"tag":15,"props":3954,"children":3955},{"__ignoreMap":7},[3956],{"type":23,"tag":218,"props":3957,"children":3958},{"class":220,"line":221},[3959,3964,3969],{"type":23,"tag":218,"props":3960,"children":3961},{"style":242},[3962],{"type":29,"value":3963},"api.update_status(",{"type":23,"tag":218,"props":3965,"children":3966},{"style":3520},[3967],{"type":29,"value":3968},"\"Hello World from Tweepy\"",{"type":23,"tag":218,"props":3970,"children":3971},{"style":242},[3972],{"type":29,"value":351},{"type":23,"tag":182,"props":3974,"children":3976},{"id":3975},"retrieve-info-of-any-user",[3977],{"type":29,"value":3978},"Retrieve info of any user",{"type":23,"tag":209,"props":3980,"children":3982},{"className":3574,"code":3981,"language":3433,"meta":7,"style":7},"user = api.get_user(\"JaimeDeArcos\")\n\nprint(\"User details:\")\nprint(user)\n\nprint(\"Last 20 Followers:\")\nfor follower in user.followers():\n    print(follower.name)\n",[3983],{"type":23,"tag":15,"props":3984,"children":3985},{"__ignoreMap":7},[3986,4012,4019,4040,4052,4059,4079,4100],{"type":23,"tag":218,"props":3987,"children":3988},{"class":220,"line":221},[3989,3994,3998,4003,4008],{"type":23,"tag":218,"props":3990,"children":3991},{"style":242},[3992],{"type":29,"value":3993},"user ",{"type":23,"tag":218,"props":3995,"children":3996},{"style":225},[3997],{"type":29,"value":302},{"type":23,"tag":218,"props":3999,"children":4000},{"style":242},[4001],{"type":29,"value":4002}," api.get_user(",{"type":23,"tag":218,"props":4004,"children":4005},{"style":3520},[4006],{"type":29,"value":4007},"\"JaimeDeArcos\"",{"type":23,"tag":218,"props":4009,"children":4010},{"style":242},[4011],{"type":29,"value":351},{"type":23,"tag":218,"props":4013,"children":4014},{"class":220,"line":248},[4015],{"type":23,"tag":218,"props":4016,"children":4017},{"emptyLinePlaceholder":964},[4018],{"type":29,"value":967},{"type":23,"tag":218,"props":4020,"children":4021},{"class":220,"line":277},[4022,4027,4031,4036],{"type":23,"tag":218,"props":4023,"children":4024},{"style":305},[4025],{"type":29,"value":4026},"print",{"type":23,"tag":218,"props":4028,"children":4029},{"style":242},[4030],{"type":29,"value":1080},{"type":23,"tag":218,"props":4032,"children":4033},{"style":3520},[4034],{"type":29,"value":4035},"\"User details:\"",{"type":23,"tag":218,"props":4037,"children":4038},{"style":242},[4039],{"type":29,"value":351},{"type":23,"tag":218,"props":4041,"children":4042},{"class":220,"line":315},[4043,4047],{"type":23,"tag":218,"props":4044,"children":4045},{"style":305},[4046],{"type":29,"value":4026},{"type":23,"tag":218,"props":4048,"children":4049},{"style":242},[4050],{"type":29,"value":4051},"(user)\n",{"type":23,"tag":218,"props":4053,"children":4054},{"class":220,"line":345},[4055],{"type":23,"tag":218,"props":4056,"children":4057},{"emptyLinePlaceholder":964},[4058],{"type":29,"value":967},{"type":23,"tag":218,"props":4060,"children":4061},{"class":220,"line":487},[4062,4066,4070,4075],{"type":23,"tag":218,"props":4063,"children":4064},{"style":305},[4065],{"type":29,"value":4026},{"type":23,"tag":218,"props":4067,"children":4068},{"style":242},[4069],{"type":29,"value":1080},{"type":23,"tag":218,"props":4071,"children":4072},{"style":3520},[4073],{"type":29,"value":4074},"\"Last 20 Followers:\"",{"type":23,"tag":218,"props":4076,"children":4077},{"style":242},[4078],{"type":29,"value":351},{"type":23,"tag":218,"props":4080,"children":4081},{"class":220,"line":496},[4082,4086,4091,4095],{"type":23,"tag":218,"props":4083,"children":4084},{"style":225},[4085],{"type":29,"value":3865},{"type":23,"tag":218,"props":4087,"children":4088},{"style":242},[4089],{"type":29,"value":4090}," follower ",{"type":23,"tag":218,"props":4092,"children":4093},{"style":225},[4094],{"type":29,"value":3875},{"type":23,"tag":218,"props":4096,"children":4097},{"style":242},[4098],{"type":29,"value":4099}," user.followers():\n",{"type":23,"tag":218,"props":4101,"children":4102},{"class":220,"line":510},[4103,4107],{"type":23,"tag":218,"props":4104,"children":4105},{"style":305},[4106],{"type":29,"value":3721},{"type":23,"tag":218,"props":4108,"children":4109},{"style":242},[4110],{"type":29,"value":4111},"(follower.name)\n",{"type":23,"tag":24,"props":4113,"children":4115},{"id":4114},"creating-your-first-bot-follow-anyone-who-follows-you",[4116],{"type":29,"value":4117},"Creating your first bot - Follow anyone who follows you",{"type":23,"tag":36,"props":4119,"children":4120},{},[4121],{"type":29,"value":4122},"In this example we are going to create a bot which:",{"type":23,"tag":62,"props":4124,"children":4125},{},[4126,4131,4136],{"type":23,"tag":66,"props":4127,"children":4128},{},[4129],{"type":29,"value":4130},"Retrieve the list of your followers",{"type":23,"tag":66,"props":4132,"children":4133},{},[4134],{"type":29,"value":4135},"Check if you're already following them",{"type":23,"tag":66,"props":4137,"children":4138},{},[4139],{"type":29,"value":4140},"If not, start following",{"type":23,"tag":209,"props":4142,"children":4144},{"className":3574,"code":4143,"language":3433,"meta":7,"style":7},"import tweepy, time\n\n# Authentication\nauth = tweepy.OAuthHandler(\"CONSUMER_API_KEY\", \"CONSUMER_API_SECRET\")\nauth.set_access_token(\"ACCESS_TOKEN\", \"ACCESS_TOKEN_SECRET\")\napi = tweepy.API(auth, wait_on_rate_limit=True, wait_on_rate_limit_notify=True)\n\n# Verify authentication\ntry:\n    api.verify_credentials()\n    print(\"Authentication OK\")\nexcept:\n    print(\"Error during authentication\")\n\nwhile True:\n    # Retrieve followers\n    for follower in tweepy.Cursor(api.followers).items():\n        if not follower.following:\n            print(\"New Follower -> {}\".format(follower.name))\n            follower.follow()\n    # Sleep for 2 minutes \n    time.sleep(120)\n\n",[4145],{"type":23,"tag":15,"props":4146,"children":4147},{"__ignoreMap":7},[4148,4160,4167,4174,4205,4228,4280,4287,4295,4306,4313,4332,4343,4362,4369,4386,4394,4415,4432,4463,4471,4479],{"type":23,"tag":218,"props":4149,"children":4150},{"class":220,"line":221},[4151,4155],{"type":23,"tag":218,"props":4152,"children":4153},{"style":225},[4154],{"type":29,"value":3587},{"type":23,"tag":218,"props":4156,"children":4157},{"style":242},[4158],{"type":29,"value":4159}," tweepy, time\n",{"type":23,"tag":218,"props":4161,"children":4162},{"class":220,"line":248},[4163],{"type":23,"tag":218,"props":4164,"children":4165},{"emptyLinePlaceholder":964},[4166],{"type":29,"value":967},{"type":23,"tag":218,"props":4168,"children":4169},{"class":220,"line":277},[4170],{"type":23,"tag":218,"props":4171,"children":4172},{"style":463},[4173],{"type":29,"value":3606},{"type":23,"tag":218,"props":4175,"children":4176},{"class":220,"line":315},[4177,4181,4185,4189,4193,4197,4201],{"type":23,"tag":218,"props":4178,"children":4179},{"style":242},[4180],{"type":29,"value":3614},{"type":23,"tag":218,"props":4182,"children":4183},{"style":225},[4184],{"type":29,"value":302},{"type":23,"tag":218,"props":4186,"children":4187},{"style":242},[4188],{"type":29,"value":3623},{"type":23,"tag":218,"props":4190,"children":4191},{"style":3520},[4192],{"type":29,"value":3628},{"type":23,"tag":218,"props":4194,"children":4195},{"style":242},[4196],{"type":29,"value":3633},{"type":23,"tag":218,"props":4198,"children":4199},{"style":3520},[4200],{"type":29,"value":3638},{"type":23,"tag":218,"props":4202,"children":4203},{"style":242},[4204],{"type":29,"value":351},{"type":23,"tag":218,"props":4206,"children":4207},{"class":220,"line":345},[4208,4212,4216,4220,4224],{"type":23,"tag":218,"props":4209,"children":4210},{"style":242},[4211],{"type":29,"value":3650},{"type":23,"tag":218,"props":4213,"children":4214},{"style":3520},[4215],{"type":29,"value":3655},{"type":23,"tag":218,"props":4217,"children":4218},{"style":242},[4219],{"type":29,"value":3633},{"type":23,"tag":218,"props":4221,"children":4222},{"style":3520},[4223],{"type":29,"value":3664},{"type":23,"tag":218,"props":4225,"children":4226},{"style":242},[4227],{"type":29,"value":351},{"type":23,"tag":218,"props":4229,"children":4230},{"class":220,"line":487},[4231,4235,4239,4244,4250,4254,4259,4263,4268,4272,4276],{"type":23,"tag":218,"props":4232,"children":4233},{"style":242},[4234],{"type":29,"value":3676},{"type":23,"tag":218,"props":4236,"children":4237},{"style":225},[4238],{"type":29,"value":302},{"type":23,"tag":218,"props":4240,"children":4241},{"style":242},[4242],{"type":29,"value":4243}," tweepy.API(auth, ",{"type":23,"tag":218,"props":4245,"children":4247},{"style":4246},"--shiki-default:#FFAB70;--shiki-light:#E36209",[4248],{"type":29,"value":4249},"wait_on_rate_limit",{"type":23,"tag":218,"props":4251,"children":4252},{"style":225},[4253],{"type":29,"value":302},{"type":23,"tag":218,"props":4255,"children":4256},{"style":305},[4257],{"type":29,"value":4258},"True",{"type":23,"tag":218,"props":4260,"children":4261},{"style":242},[4262],{"type":29,"value":3633},{"type":23,"tag":218,"props":4264,"children":4265},{"style":4246},[4266],{"type":29,"value":4267},"wait_on_rate_limit_notify",{"type":23,"tag":218,"props":4269,"children":4270},{"style":225},[4271],{"type":29,"value":302},{"type":23,"tag":218,"props":4273,"children":4274},{"style":305},[4275],{"type":29,"value":4258},{"type":23,"tag":218,"props":4277,"children":4278},{"style":242},[4279],{"type":29,"value":351},{"type":23,"tag":218,"props":4281,"children":4282},{"class":220,"line":496},[4283],{"type":23,"tag":218,"props":4284,"children":4285},{"emptyLinePlaceholder":964},[4286],{"type":29,"value":967},{"type":23,"tag":218,"props":4288,"children":4289},{"class":220,"line":510},[4290],{"type":23,"tag":218,"props":4291,"children":4292},{"style":463},[4293],{"type":29,"value":4294},"# Verify authentication\n",{"type":23,"tag":218,"props":4296,"children":4297},{"class":220,"line":519},[4298,4302],{"type":23,"tag":218,"props":4299,"children":4300},{"style":225},[4301],{"type":29,"value":3700},{"type":23,"tag":218,"props":4303,"children":4304},{"style":242},[4305],{"type":29,"value":3705},{"type":23,"tag":218,"props":4307,"children":4308},{"class":220,"line":532},[4309],{"type":23,"tag":218,"props":4310,"children":4311},{"style":242},[4312],{"type":29,"value":3713},{"type":23,"tag":218,"props":4314,"children":4315},{"class":220,"line":1154},[4316,4320,4324,4328],{"type":23,"tag":218,"props":4317,"children":4318},{"style":305},[4319],{"type":29,"value":3721},{"type":23,"tag":218,"props":4321,"children":4322},{"style":242},[4323],{"type":29,"value":1080},{"type":23,"tag":218,"props":4325,"children":4326},{"style":3520},[4327],{"type":29,"value":3730},{"type":23,"tag":218,"props":4329,"children":4330},{"style":242},[4331],{"type":29,"value":351},{"type":23,"tag":218,"props":4333,"children":4334},{"class":220,"line":1181},[4335,4339],{"type":23,"tag":218,"props":4336,"children":4337},{"style":225},[4338],{"type":29,"value":3742},{"type":23,"tag":218,"props":4340,"children":4341},{"style":242},[4342],{"type":29,"value":3705},{"type":23,"tag":218,"props":4344,"children":4345},{"class":220,"line":1199},[4346,4350,4354,4358],{"type":23,"tag":218,"props":4347,"children":4348},{"style":305},[4349],{"type":29,"value":3721},{"type":23,"tag":218,"props":4351,"children":4352},{"style":242},[4353],{"type":29,"value":1080},{"type":23,"tag":218,"props":4355,"children":4356},{"style":3520},[4357],{"type":29,"value":3762},{"type":23,"tag":218,"props":4359,"children":4360},{"style":242},[4361],{"type":29,"value":351},{"type":23,"tag":218,"props":4363,"children":4364},{"class":220,"line":1208},[4365],{"type":23,"tag":218,"props":4366,"children":4367},{"emptyLinePlaceholder":964},[4368],{"type":29,"value":967},{"type":23,"tag":218,"props":4370,"children":4371},{"class":220,"line":1233},[4372,4377,4382],{"type":23,"tag":218,"props":4373,"children":4374},{"style":225},[4375],{"type":29,"value":4376},"while",{"type":23,"tag":218,"props":4378,"children":4379},{"style":305},[4380],{"type":29,"value":4381}," True",{"type":23,"tag":218,"props":4383,"children":4384},{"style":242},[4385],{"type":29,"value":3705},{"type":23,"tag":218,"props":4387,"children":4388},{"class":220,"line":1250},[4389],{"type":23,"tag":218,"props":4390,"children":4391},{"style":463},[4392],{"type":29,"value":4393},"    # Retrieve followers\n",{"type":23,"tag":218,"props":4395,"children":4396},{"class":220,"line":1258},[4397,4402,4406,4410],{"type":23,"tag":218,"props":4398,"children":4399},{"style":225},[4400],{"type":29,"value":4401},"    for",{"type":23,"tag":218,"props":4403,"children":4404},{"style":242},[4405],{"type":29,"value":4090},{"type":23,"tag":218,"props":4407,"children":4408},{"style":225},[4409],{"type":29,"value":3875},{"type":23,"tag":218,"props":4411,"children":4412},{"style":242},[4413],{"type":29,"value":4414}," tweepy.Cursor(api.followers).items():\n",{"type":23,"tag":218,"props":4416,"children":4417},{"class":220,"line":1267},[4418,4422,4427],{"type":23,"tag":218,"props":4419,"children":4420},{"style":225},[4421],{"type":29,"value":2664},{"type":23,"tag":218,"props":4423,"children":4424},{"style":225},[4425],{"type":29,"value":4426}," not",{"type":23,"tag":218,"props":4428,"children":4429},{"style":242},[4430],{"type":29,"value":4431}," follower.following:\n",{"type":23,"tag":218,"props":4433,"children":4434},{"class":220,"line":2800},[4435,4440,4444,4449,4454,4458],{"type":23,"tag":218,"props":4436,"children":4437},{"style":305},[4438],{"type":29,"value":4439},"            print",{"type":23,"tag":218,"props":4441,"children":4442},{"style":242},[4443],{"type":29,"value":1080},{"type":23,"tag":218,"props":4445,"children":4446},{"style":3520},[4447],{"type":29,"value":4448},"\"New Follower -> ",{"type":23,"tag":218,"props":4450,"children":4451},{"style":305},[4452],{"type":29,"value":4453},"{}",{"type":23,"tag":218,"props":4455,"children":4456},{"style":3520},[4457],{"type":29,"value":3901},{"type":23,"tag":218,"props":4459,"children":4460},{"style":242},[4461],{"type":29,"value":4462},".format(follower.name))\n",{"type":23,"tag":218,"props":4464,"children":4465},{"class":220,"line":2817},[4466],{"type":23,"tag":218,"props":4467,"children":4468},{"style":242},[4469],{"type":29,"value":4470},"            follower.follow()\n",{"type":23,"tag":218,"props":4472,"children":4473},{"class":220,"line":2826},[4474],{"type":23,"tag":218,"props":4475,"children":4476},{"style":463},[4477],{"type":29,"value":4478},"    # Sleep for 2 minutes \n",{"type":23,"tag":218,"props":4480,"children":4481},{"class":220,"line":2888},[4482,4487,4492],{"type":23,"tag":218,"props":4483,"children":4484},{"style":242},[4485],{"type":29,"value":4486},"    time.sleep(",{"type":23,"tag":218,"props":4488,"children":4489},{"style":305},[4490],{"type":29,"value":4491},"120",{"type":23,"tag":218,"props":4493,"children":4494},{"style":242},[4495],{"type":29,"value":351},{"type":23,"tag":36,"props":4497,"children":4498},{},[4499,4501],{"type":29,"value":4500},"Simple, right? now you can improve the script with whatever use case you need. This script could be running locally or\nmaybe you would deploy it on any provider of your choice ",{"type":23,"tag":40,"props":4502,"children":4503},{},[4504],{"type":29,"value":4505},"(AWS, Azure, google cloud...)",{"type":23,"tag":4507,"props":4508,"children":4509},"hr",{},[],{"type":23,"tag":24,"props":4511,"children":4513},{"id":4512},"references-links",[4514],{"type":29,"value":4515},"References & links",{"type":23,"tag":62,"props":4517,"children":4518},{},[4519,4526],{"type":23,"tag":66,"props":4520,"children":4521},{},[4522],{"type":23,"tag":48,"props":4523,"children":4524},{"href":3449},[4525],{"type":29,"value":3449},{"type":23,"tag":66,"props":4527,"children":4528},{},[4529],{"type":23,"tag":48,"props":4530,"children":4533},{"href":4531,"rel":4532},"https://www.tweepy.org/",[52],[4534],{"type":29,"value":3493},{"type":23,"tag":4507,"props":4536,"children":4537},{},[],{"type":23,"tag":1287,"props":4539,"children":4540},{},[4541],{"type":29,"value":1291},{"title":7,"searchDepth":248,"depth":248,"links":4543},[4544,4545,4546,4551,4552],{"id":3438,"depth":248,"text":3441},{"id":3480,"depth":248,"text":3483},{"id":3793,"depth":248,"text":3796,"children":4547},[4548,4549,4550],{"id":3804,"depth":277,"text":3810},{"id":3945,"depth":277,"text":3948},{"id":3975,"depth":277,"text":3978},{"id":4114,"depth":248,"text":4117},{"id":4512,"depth":248,"text":4515},"content:posts:how-to-create-a-twitter-bot.md","posts/how-to-create-a-twitter-bot.md","posts/how-to-create-a-twitter-bot",[4557,4561,4565,4569,4573,4577,4581,4585,4589,4593,4597,4601,4605,4609,4613,4617,4621,4622,4623,4627,4628],{"_path":4558,"title":4559,"date":4560},"/posts/gpt-sql-integration","Implementa un analizador de consultas en lenguaje natural para bases de datos SQL con GPT","2025-01-15T11:00:00.000Z",{"_path":4562,"title":4563,"date":4564},"/posts/java-security-best-practices","Java Security Best Practices: Preventing Vulnerabilities and Threats","2024-08-15T11:00:00.000Z",{"_path":4566,"title":4567,"date":4568},"/posts/circuit-breaking-resilience","Circuit Breaking in Distributed Systems","2024-07-15T11:00:00.000Z",{"_path":4570,"title":4571,"date":4572},"/posts/maven-and-github-actions","Automating Java Builds with Maven and GitHub Actions CI/CD","2024-06-15T11:00:00.000Z",{"_path":4574,"title":4575,"date":4576},"/posts/pattern-matching-sealed-classes-java21","A Guide to Pattern Matching and Sealed Classes in Java 21","2024-05-15T11:00:00.000Z",{"_path":4578,"title":4579,"date":4580},"/posts/async-java-completable-vs-reactive","Asynchronous Programming in Java: CompletableFuture vs Reactive Streams","2024-04-15T11:00:00.000Z",{"_path":4582,"title":4583,"date":4584},"/posts/mastering-concurrency","Mastering Concurrency in Java: Threads, Executors, and Virtual Threads","2024-03-15T11:00:00.000Z",{"_path":4586,"title":4587,"date":4588},"/posts/microservices-mistakes","Common Mistakes When Developing Microservices","2024-02-15T11:00:00.000Z",{"_path":4590,"title":4591,"date":4592},"/posts/tdd-testing","Test-Driven Development (TDD) in Java with JUnit and Mockito","2024-01-15T11:00:00.000Z",{"_path":4594,"title":4595,"date":4596},"/posts/single-table-design","Single Table Design","2023-12-27T11:00:00.000Z",{"_path":4598,"title":4599,"date":4600},"/posts/java-17","Exploring Java 17's New Features","2023-10-08T11:00:00.000Z",{"_path":4602,"title":4603,"date":4604},"/posts/hexagonal-architecture","Introduction to Hexagonal Architecture","2023-10-02 18:00:00",{"_path":4606,"title":4607,"date":4608},"/posts/terraform","Unleashing the Power of Infrastructure as Code (IaC)","2023-09-24 18:00:00",{"_path":4610,"title":4611,"date":4612},"/posts/python-virtual-environments","Python Virtual Environments (venv)","2023-09-17 22:00:00",{"_path":4614,"title":4615,"date":4616},"/posts/api-first-principles","API First principles","2023-09-01 22:15:18",{"_path":4618,"title":4619,"date":4620},"/posts/how-to-configure-ssh-key-based-authentication","How To Configure SSH Key-Based Authentication","2021-02-21 03:46:12",{"_path":1307,"title":1308,"date":1310},{"_path":3426,"title":3427,"date":1310},{"_path":4624,"title":4625,"date":4626},"/posts/manage-aws-infrastructure-with-terraform","Bootstrap complete Java application infrastructure in AWS with Terraform","2020-12-02T11:00:00.000Z",{"_path":4,"title":8,"date":10},{"_path":4629,"title":4630,"date":4631},"/posts/playing-with-ocr-libraries","Playing with OCR libraries","2020-11-22T11:00:00.000Z",1790510066443]