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