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