Skip to main content

Treap

The Treap is a mix between a Binary Search Tree and a Heap.

Like a Binary Search Tree, it keeps an ordered set of keys in the shape of a tree, allowing for binary search traversal.

Like a Heap, it associates each node with a priority, making sure that a parent’s priority is always higher than any of its children.

What does it do?

By randomizing the priority value of each key at insertion time, we ensure a high likelihood that the tree stays roughly balanced, avoiding degenerating to unbalanced O(N) height.

Here’s a sample tree created by inserting integers from 0 to 250 into the tree:

    
        
            
            
            
        

        graph {
  node [shape=circle]
  2[label=<2<BR/> <FONT COLOR="grey">0.9957869495270144</FONT>>];
  2 -- 1 [side=L]
  1[label=<1<BR/> <FONT COLOR="grey">0.8476937427586738</FONT>>];
  1 -- 0 [side=L]
  0[label=<0<BR/> <FONT COLOR="grey">0.7514570833207395</FONT>>];
  2 -- 145 [side=R]
  145[label=<145<BR/> <FONT COLOR="grey">0.9939888728473314</FONT>>];
  145 -- 12 [side=L]
  12[label=<12<BR/> <FONT COLOR="grey">0.9907319285760915</FONT>>];
  12 -- 5 [side=L]
  5[label=<5<BR/> <FONT COLOR="grey">0.988082370881665</FONT>>];
  5 -- 4 [side=L]
  4[label=<4<BR/> <FONT COLOR="grey">0.7422137887388395</FONT>>];
  4 -- 3 [side=L]
  3[label=<3<BR/> <FONT COLOR="grey">0.24440218394323976</FONT>>];
  5 -- 8 [side=R]
  8[label=<8<BR/> <FONT COLOR="grey">0.695547503791194</FONT>>];
  8 -- 6 [side=L]
  6[label=<6<BR/> <FONT COLOR="grey">0.5664414792316717</FONT>>];
  6 -- 7 [side=R]
  7[label=<7<BR/> <FONT COLOR="grey">0.11672897928969816</FONT>>];
  8 -- 11 [side=R]
  11[label=<11<BR/> <FONT COLOR="grey">0.45404068759973704</FONT>>];
  11 -- 9 [side=L]
  9[label=<9<BR/> <FONT COLOR="grey">0.3325186970255505</FONT>>];
  9 -- 10 [side=R]
  10[label=<10<BR/> <FONT COLOR="grey">0.10578109016044712</FONT>>];
  12 -- 47 [side=R]
  47[label=<47<BR/> <FONT COLOR="grey">0.9904839559513964</FONT>>];
  47 -- 33 [side=L]
  33[label=<33<BR/> <FONT COLOR="grey">0.9758188259688814</FONT>>];
  33 -- 28 [side=L]
  28[label=<28<BR/> <FONT COLOR="grey">0.915671870648212</FONT>>];
  28 -- 23 [side=L]
  23[label=<23<BR/> <FONT COLOR="grey">0.8487360320264957</FONT>>];
  23 -- 20 [side=L]
  20[label=<20<BR/> <FONT COLOR="grey">0.7063809958484802</FONT>>];
  20 -- 16 [side=L]
  16[label=<16<BR/> <FONT COLOR="grey">0.7034299359160114</FONT>>];
  16 -- 15 [side=L]
  15[label=<15<BR/> <FONT COLOR="grey">0.5922634040068163</FONT>>];
  15 -- 13 [side=L]
  13[label=<13<BR/> <FONT COLOR="grey">0.4543498778672278</FONT>>];
  13 -- 14 [side=R]
  14[label=<14<BR/> <FONT COLOR="grey">0.21605966187340298</FONT>>];
  16 -- 17 [side=R]
  17[label=<17<BR/> <FONT COLOR="grey">0.3709284358038115</FONT>>];
  17 -- 18 [side=R]
  18[label=<18<BR/> <FONT COLOR="grey">0.260599350868457</FONT>>];
  18 -- 19 [side=R]
  19[label=<19<BR/> <FONT COLOR="grey">0.1817886048339078</FONT>>];
  20 -- 21 [side=R]
  21[label=<21<BR/> <FONT COLOR="grey">0.14210053829804103</FONT>>];
  21 -- 22 [side=R]
  22[label=<22<BR/> <FONT COLOR="grey">0.08869917426783513</FONT>>];
  23 -- 25 [side=R]
  25[label=<25<BR/> <FONT COLOR="grey">0.4892716208875628</FONT>>];
  25 -- 24 [side=L]
  24[label=<24<BR/> <FONT COLOR="grey">0.25363061372658946</FONT>>];
  25 -- 26 [side=R]
  26[label=<26<BR/> <FONT COLOR="grey">0.13796660931463622</FONT>>];
  26 -- 27 [side=R]
  27[label=<27<BR/> <FONT COLOR="grey">0.12997273552181765</FONT>>];
  28 -- 31 [side=R]
  31[label=<31<BR/> <FONT COLOR="grey">0.8601319422972336</FONT>>];
  31 -- 29 [side=L]
  29[label=<29<BR/> <FONT COLOR="grey">0.8084698172467167</FONT>>];
  29 -- 30 [side=R]
  30[label=<30<BR/> <FONT COLOR="grey">0.27908621178937176</FONT>>];
  31 -- 32 [side=R]
  32[label=<32<BR/> <FONT COLOR="grey">0.0844421977476495</FONT>>];
  33 -- 35 [side=R]
  35[label=<35<BR/> <FONT COLOR="grey">0.967049096465691</FONT>>];
  35 -- 34 [side=L]
  34[label=<34<BR/> <FONT COLOR="grey">0.12377873295631248</FONT>>];
  35 -- 39 [side=R]
  39[label=<39<BR/> <FONT COLOR="grey">0.9328169604588316</FONT>>];
  39 -- 38 [side=L]
  38[label=<38<BR/> <FONT COLOR="grey">0.7787222122657034</FONT>>];
  38 -- 37 [side=L]
  37[label=<37<BR/> <FONT COLOR="grey">0.4043690842717128</FONT>>];
  37 -- 36 [side=L]
  36[label=<36<BR/> <FONT COLOR="grey">0.3046740248996985</FONT>>];
  39 -- 43 [side=R]
  43[label=<43<BR/> <FONT COLOR="grey">0.8350649185763811</FONT>>];
  43 -- 40 [side=L]
  40[label=<40<BR/> <FONT COLOR="grey">0.5406537578359348</FONT>>];
  40 -- 42 [side=R]
  42[label=<42<BR/> <FONT COLOR="grey">0.3193437618727414</FONT>>];
  42 -- 41 [side=L]
  41[label=<41<BR/> <FONT COLOR="grey">0.27788504363715394</FONT>>];
  43 -- 44 [side=R]
  44[label=<44<BR/> <FONT COLOR="grey">0.4830145820991081</FONT>>];
  44 -- 46 [side=R]
  46[label=<46<BR/> <FONT COLOR="grey">0.33052118143151343</FONT>>];
  46 -- 45 [side=L]
  45[label=<45<BR/> <FONT COLOR="grey">0.29551968300055065</FONT>>];
  47 -- 114 [side=R]
  114[label=<114<BR/> <FONT COLOR="grey">0.9876511078116038</FONT>>];
  114 -- 86 [side=L]
  86[label=<86<BR/> <FONT COLOR="grey">0.9735379804960496</FONT>>];
  86 -- 77 [side=L]
  77[label=<77<BR/> <FONT COLOR="grey">0.9269342780771458</FONT>>];
  77 -- 68 [side=L]
  68[label=<68<BR/> <FONT COLOR="grey">0.9230582358699485</FONT>>];
  68 -- 55 [side=L]
  55[label=<55<BR/> <FONT COLOR="grey">0.839490204679298</FONT>>];
  55 -- 51 [side=L]
  51[label=<51<BR/> <FONT COLOR="grey">0.3385715557270519</FONT>>];
  51 -- 50 [side=L]
  50[label=<50<BR/> <FONT COLOR="grey">0.3360608605201836</FONT>>];
  50 -- 49 [side=L]
  49[label=<49<BR/> <FONT COLOR="grey">0.2774445142713422</FONT>>];
  49 -- 48 [side=L]
  48[label=<48<BR/> <FONT COLOR="grey">0.17489390663513982</FONT>>];
  51 -- 52 [side=R]
  52[label=<52<BR/> <FONT COLOR="grey">0.1886079446757799</FONT>>];
  52 -- 54 [side=R]
  54[label=<54<BR/> <FONT COLOR="grey">0.12293678964136756</FONT>>];
  54 -- 53 [side=L]
  53[label=<53<BR/> <FONT COLOR="grey">0.08266549169366844</FONT>>];
  55 -- 64 [side=R]
  64[label=<64<BR/> <FONT COLOR="grey">0.5480541758673416</FONT>>];
  64 -- 61 [side=L]
  61[label=<61<BR/> <FONT COLOR="grey">0.4333510895474586</FONT>>];
  61 -- 58 [side=L]
  58[label=<58<BR/> <FONT COLOR="grey">0.22965679673111572</FONT>>];
  58 -- 57 [side=L]
  57[label=<57<BR/> <FONT COLOR="grey">0.1750392554452569</FONT>>];
  57 -- 56 [side=L]
  56[label=<56<BR/> <FONT COLOR="grey">0.1154682181055644</FONT>>];
  58 -- 60 [side=R]
  60[label=<60<BR/> <FONT COLOR="grey">0.19505416228675265</FONT>>];
  60 -- 59 [side=L]
  59[label=<59<BR/> <FONT COLOR="grey">0.15628574332026035</FONT>>];
  61 -- 63 [side=R]
  63[label=<63<BR/> <FONT COLOR="grey">0.23625159275800722</FONT>>];
  63 -- 62 [side=L]
  62[label=<62<BR/> <FONT COLOR="grey">0.10778840213677576</FONT>>];
  64 -- 66 [side=R]
  66[label=<66<BR/> <FONT COLOR="grey">0.4795221664420316</FONT>>];
  66 -- 65 [side=L]
  65[label=<65<BR/> <FONT COLOR="grey">0.29689544767487486</FONT>>];
  66 -- 67 [side=R]
  67[label=<67<BR/> <FONT COLOR="grey">0.1446107813011439</FONT>>];
  68 -- 75 [side=R]
  75[label=<75<BR/> <FONT COLOR="grey">0.9136368186744673</FONT>>];
  75 -- 73 [side=L]
  73[label=<73<BR/> <FONT COLOR="grey">0.8180762618516633</FONT>>];
  73 -- 72 [side=L]
  72[label=<72<BR/> <FONT COLOR="grey">0.790052528538962</FONT>>];
  72 -- 69 [side=L]
  69[label=<69<BR/> <FONT COLOR="grey">0.6662534331501744</FONT>>];
  69 -- 70 [side=R]
  70[label=<70<BR/> <FONT COLOR="grey">0.198170103856931</FONT>>];
  70 -- 71 [side=R]
  71[label=<71<BR/> <FONT COLOR="grey">0.09798170568469355</FONT>>];
  73 -- 74 [side=R]
  74[label=<74<BR/> <FONT COLOR="grey">0.3113326781632004</FONT>>];
  75 -- 76 [side=R]
  76[label=<76<BR/> <FONT COLOR="grey">0.878956270284837</FONT>>];
  77 -- 84 [side=R]
  84[label=<84<BR/> <FONT COLOR="grey">0.7298827990732385</FONT>>];
  84 -- 81 [side=L]
  81[label=<81<BR/> <FONT COLOR="grey">0.7294243899593819</FONT>>];
  81 -- 78 [side=L]
  78[label=<78<BR/> <FONT COLOR="grey">0.6751780813023192</FONT>>];
  78 -- 80 [side=R]
  80[label=<80<BR/> <FONT COLOR="grey">0.2765792284019555</FONT>>];
  80 -- 79 [side=L]
  79[label=<79<BR/> <FONT COLOR="grey">0.12189576635143229</FONT>>];
  81 -- 83 [side=R]
  83[label=<83<BR/> <FONT COLOR="grey">0.5986915452921237</FONT>>];
  83 -- 82 [side=L]
  82[label=<82<BR/> <FONT COLOR="grey">0.5846092825823913</FONT>>];
  84 -- 85 [side=R]
  85[label=<85<BR/> <FONT COLOR="grey">0.5030186847252209</FONT>>];
  86 -- 103 [side=R]
  103[label=<103<BR/> <FONT COLOR="grey">0.968253245483458</FONT>>];
  103 -- 88 [side=L]
  88[label=<88<BR/> <FONT COLOR="grey">0.9609670084738324</FONT>>];
  88 -- 87 [side=L]
  87[label=<87<BR/> <FONT COLOR="grey">0.5446892558733331</FONT>>];
  88 -- 97 [side=R]
  97[label=<97<BR/> <FONT COLOR="grey">0.9483161815322799</FONT>>];
  97 -- 92 [side=L]
  92[label=<92<BR/> <FONT COLOR="grey">0.9417148997777522</FONT>>];
  92 -- 89 [side=L]
  89[label=<89<BR/> <FONT COLOR="grey">0.2455511319571373</FONT>>];
  89 -- 90 [side=R]
  90[label=<90<BR/> <FONT COLOR="grey">0.15647633602612276</FONT>>];
  90 -- 91 [side=R]
  91[label=<91<BR/> <FONT COLOR="grey">0.13997459061178652</FONT>>];
  92 -- 93 [side=R]
  93[label=<93<BR/> <FONT COLOR="grey">0.7804508194274744</FONT>>];
  93 -- 95 [side=R]
  95[label=<95<BR/> <FONT COLOR="grey">0.7226672256819942</FONT>>];
  95 -- 94 [side=L]
  94[label=<94<BR/> <FONT COLOR="grey">0.43503881905350683</FONT>>];
  95 -- 96 [side=R]
  96[label=<96<BR/> <FONT COLOR="grey">0.40712673636645136</FONT>>];
  97 -- 101 [side=R]
  101[label=<101<BR/> <FONT COLOR="grey">0.9255359833317219</FONT>>];
  101 -- 99 [side=L]
  99[label=<99<BR/> <FONT COLOR="grey">0.7139348283525223</FONT>>];
  99 -- 98 [side=L]
  98[label=<98<BR/> <FONT COLOR="grey">0.21279150783985867</FONT>>];
  99 -- 100 [side=R]
  100[label=<100<BR/> <FONT COLOR="grey">0.48002453571456083</FONT>>];
  101 -- 102 [side=R]
  102[label=<102<BR/> <FONT COLOR="grey">0.6219674393805691</FONT>>];
  103 -- 106 [side=R]
  106[label=<106<BR/> <FONT COLOR="grey">0.8731379458552129</FONT>>];
  106 -- 104 [side=L]
  104[label=<104<BR/> <FONT COLOR="grey">0.5400277589431796</FONT>>];
  104 -- 105 [side=R]
  105[label=<105<BR/> <FONT COLOR="grey">0.0007891566377928871</FONT>>];
  106 -- 110 [side=R]
  110[label=<110<BR/> <FONT COLOR="grey">0.8382929650437165</FONT>>];
  110 -- 109 [side=L]
  109[label=<109<BR/> <FONT COLOR="grey">0.4615149406437542</FONT>>];
  109 -- 107 [side=L]
  107[label=<107<BR/> <FONT COLOR="grey">0.43337904959817486</FONT>>];
  107 -- 108 [side=R]
  108[label=<108<BR/> <FONT COLOR="grey">0.053658620013432023</FONT>>];
  110 -- 112 [side=R]
  112[label=<112<BR/> <FONT COLOR="grey">0.5679810533328041</FONT>>];
  112 -- 111 [side=L]
  111[label=<111<BR/> <FONT COLOR="grey">0.541018763444613</FONT>>];
  112 -- 113 [side=R]
  113[label=<113<BR/> <FONT COLOR="grey">0.42841646768139463</FONT>>];
  114 -- 123 [side=R]
  123[label=<123<BR/> <FONT COLOR="grey">0.9464643826259924</FONT>>];
  123 -- 119 [side=L]
  119[label=<119<BR/> <FONT COLOR="grey">0.8662877311047388</FONT>>];
  119 -- 116 [side=L]
  116[label=<116<BR/> <FONT COLOR="grey">0.7670170824076343</FONT>>];
  116 -- 115 [side=L]
  115[label=<115<BR/> <FONT COLOR="grey">0.17190031141596263</FONT>>];
  116 -- 118 [side=R]
  118[label=<118<BR/> <FONT COLOR="grey">0.3671674221014496</FONT>>];
  118 -- 117 [side=L]
  117[label=<117<BR/> <FONT COLOR="grey">0.2159590427591035</FONT>>];
  119 -- 121 [side=R]
  121[label=<121<BR/> <FONT COLOR="grey">0.74738638353363</FONT>>];
  121 -- 120 [side=L]
  120[label=<120<BR/> <FONT COLOR="grey">0.08665916013292596</FONT>>];
  121 -- 122 [side=R]
  122[label=<122<BR/> <FONT COLOR="grey">0.4119543486009871</FONT>>];
  123 -- 143 [side=R]
  143[label=<143<BR/> <FONT COLOR="grey">0.9247442715890171</FONT>>];
  143 -- 140 [side=L]
  140[label=<140<BR/> <FONT COLOR="grey">0.8936337545064225</FONT>>];
  140 -- 125 [side=L]
  125[label=<125<BR/> <FONT COLOR="grey">0.8517904638602968</FONT>>];
  125 -- 124 [side=L]
  124[label=<124<BR/> <FONT COLOR="grey">0.681815353261336</FONT>>];
  125 -- 126 [side=R]
  126[label=<126<BR/> <FONT COLOR="grey">0.7946927108945189</FONT>>];
  126 -- 133 [side=R]
  133[label=<133<BR/> <FONT COLOR="grey">0.7289879153749418</FONT>>];
  133 -- 128 [side=L]
  128[label=<128<BR/> <FONT COLOR="grey">0.7256302169867033</FONT>>];
  128 -- 127 [side=L]
  127[label=<127<BR/> <FONT COLOR="grey">0.6200343851508556</FONT>>];
  128 -- 131 [side=R]
  131[label=<131<BR/> <FONT COLOR="grey">0.7089639682237622</FONT>>];
  131 -- 129 [side=L]
  129[label=<129<BR/> <FONT COLOR="grey">0.5255088944172916</FONT>>];
  129 -- 130 [side=R]
  130[label=<130<BR/> <FONT COLOR="grey">0.41550021924797953</FONT>>];
  131 -- 132 [side=R]
  132[label=<132<BR/> <FONT COLOR="grey">0.1851371011016204</FONT>>];
  133 -- 134 [side=R]
  134[label=<134<BR/> <FONT COLOR="grey">0.6333388625482715</FONT>>];
  134 -- 135 [side=R]
  135[label=<135<BR/> <FONT COLOR="grey">0.4707182839428111</FONT>>];
  135 -- 138 [side=R]
  138[label=<138<BR/> <FONT COLOR="grey">0.4128350639522185</FONT>>];
  138 -- 137 [side=L]
  137[label=<137<BR/> <FONT COLOR="grey">0.25475301393491523</FONT>>];
  137 -- 136 [side=L]
  136[label=<136<BR/> <FONT COLOR="grey">0.04841095612377411</FONT>>];
  138 -- 139 [side=R]
  139[label=<139<BR/> <FONT COLOR="grey">0.3855980374729622</FONT>>];
  140 -- 141 [side=R]
  141[label=<141<BR/> <FONT COLOR="grey">0.8655582249610275</FONT>>];
  141 -- 142 [side=R]
  142[label=<142<BR/> <FONT COLOR="grey">0.2288591915235536</FONT>>];
  143 -- 144 [side=R]
  144[label=<144<BR/> <FONT COLOR="grey">0.36710686055015185</FONT>>];
  145 -- 235 [side=R]
  235[label=<235<BR/> <FONT COLOR="grey">0.9812914040931304</FONT>>];
  235 -- 169 [side=L]
  169[label=<169<BR/> <FONT COLOR="grey">0.9782585107854103</FONT>>];
  169 -- 148 [side=L]
  148[label=<148<BR/> <FONT COLOR="grey">0.9449023406787344</FONT>>];
  148 -- 147 [side=L]
  147[label=<147<BR/> <FONT COLOR="grey">0.7293740098286456</FONT>>];
  147 -- 146 [side=L]
  146[label=<146<BR/> <FONT COLOR="grey">0.509027051210447</FONT>>];
  148 -- 150 [side=R]
  150[label=<150<BR/> <FONT COLOR="grey">0.9020436951014624</FONT>>];
  150 -- 149 [side=L]
  149[label=<149<BR/> <FONT COLOR="grey">0.8190200516813236</FONT>>];
  150 -- 153 [side=R]
  153[label=<153<BR/> <FONT COLOR="grey">0.8855777631108436</FONT>>];
  153 -- 152 [side=L]
  152[label=<152<BR/> <FONT COLOR="grey">0.8603376027563862</FONT>>];
  152 -- 151 [side=L]
  151[label=<151<BR/> <FONT COLOR="grey">0.45459695989529647</FONT>>];
  153 -- 167 [side=R]
  167[label=<167<BR/> <FONT COLOR="grey">0.8492899523613442</FONT>>];
  167 -- 163 [side=L]
  163[label=<163<BR/> <FONT COLOR="grey">0.8177575678069656</FONT>>];
  163 -- 160 [side=L]
  160[label=<160<BR/> <FONT COLOR="grey">0.7759852430212377</FONT>>];
  160 -- 158 [side=L]
  158[label=<158<BR/> <FONT COLOR="grey">0.734924251943539</FONT>>];
  158 -- 156 [side=L]
  156[label=<156<BR/> <FONT COLOR="grey">0.710110660084087</FONT>>];
  156 -- 155 [side=L]
  155[label=<155<BR/> <FONT COLOR="grey">0.2959097323390557</FONT>>];
  155 -- 154 [side=L]
  154[label=<154<BR/> <FONT COLOR="grey">0.23060113716089548</FONT>>];
  156 -- 157 [side=R]
  157[label=<157<BR/> <FONT COLOR="grey">0.04398748527099361</FONT>>];
  158 -- 159 [side=R]
  159[label=<159<BR/> <FONT COLOR="grey">0.09047000528918914</FONT>>];
  160 -- 162 [side=R]
  162[label=<162<BR/> <FONT COLOR="grey">0.7216878041963218</FONT>>];
  162 -- 161 [side=L]
  161[label=<161<BR/> <FONT COLOR="grey">0.6871202472393689</FONT>>];
  163 -- 165 [side=R]
  165[label=<165<BR/> <FONT COLOR="grey">0.3886140063494328</FONT>>];
  165 -- 164 [side=L]
  164[label=<164<BR/> <FONT COLOR="grey">0.3870484684485387</FONT>>];
  165 -- 166 [side=R]
  166[label=<166<BR/> <FONT COLOR="grey">0.3476475192762013</FONT>>];
  167 -- 168 [side=R]
  168[label=<168<BR/> <FONT COLOR="grey">0.8169397159175148</FONT>>];
  169 -- 172 [side=R]
  172[label=<172<BR/> <FONT COLOR="grey">0.9747928133504129</FONT>>];
  172 -- 170 [side=L]
  170[label=<170<BR/> <FONT COLOR="grey">0.846452976104617</FONT>>];
  170 -- 171 [side=R]
  171[label=<171<BR/> <FONT COLOR="grey">0.3121690933042578</FONT>>];
  172 -- 230 [side=R]
  230[label=<230<BR/> <FONT COLOR="grey">0.958355558453681</FONT>>];
  230 -- 187 [side=L]
  187[label=<187<BR/> <FONT COLOR="grey">0.957557676245447</FONT>>];
  187 -- 180 [side=L]
  180[label=<180<BR/> <FONT COLOR="grey">0.92710438449752</FONT>>];
  180 -- 174 [side=L]
  174[label=<174<BR/> <FONT COLOR="grey">0.7049973940003331</FONT>>];
  174 -- 173 [side=L]
  173[label=<173<BR/> <FONT COLOR="grey">0.152023809588824</FONT>>];
  174 -- 179 [side=R]
  179[label=<179<BR/> <FONT COLOR="grey">0.6715227532708304</FONT>>];
  179 -- 176 [side=L]
  176[label=<176<BR/> <FONT COLOR="grey">0.6621862385446002</FONT>>];
  176 -- 175 [side=L]
  175[label=<175<BR/> <FONT COLOR="grey">0.1248086920689816</FONT>>];
  176 -- 178 [side=R]
  178[label=<178<BR/> <FONT COLOR="grey">0.5713418767827186</FONT>>];
  178 -- 177 [side=L]
  177[label=<177<BR/> <FONT COLOR="grey">0.5560171098718876</FONT>>];
  180 -- 183 [side=R]
  183[label=<183<BR/> <FONT COLOR="grey">0.9250644230760526</FONT>>];
  183 -- 182 [side=L]
  182[label=<182<BR/> <FONT COLOR="grey">0.6824354777265715</FONT>>];
  182 -- 181 [side=L]
  181[label=<181<BR/> <FONT COLOR="grey">0.012508171868127804</FONT>>];
  183 -- 184 [side=R]
  184[label=<184<BR/> <FONT COLOR="grey">0.4823271793225655</FONT>>];
  184 -- 185 [side=R]
  185[label=<185<BR/> <FONT COLOR="grey">0.3841022310929997</FONT>>];
  185 -- 186 [side=R]
  186[label=<186<BR/> <FONT COLOR="grey">0.18775042542247533</FONT>>];
  187 -- 203 [side=R]
  203[label=<203<BR/> <FONT COLOR="grey">0.8944186382789656</FONT>>];
  203 -- 194 [side=L]
  194[label=<194<BR/> <FONT COLOR="grey">0.878541152511799</FONT>>];
  194 -- 190 [side=L]
  190[label=<190<BR/> <FONT COLOR="grey">0.6762694909925744</FONT>>];
  190 -- 188 [side=L]
  188[label=<188<BR/> <FONT COLOR="grey">0.3829541869540283</FONT>>];
  188 -- 189 [side=R]
  189[label=<189<BR/> <FONT COLOR="grey">0.20943675668859407</FONT>>];
  190 -- 192 [side=R]
  192[label=<192<BR/> <FONT COLOR="grey">0.5817469625147841</FONT>>];
  192 -- 191 [side=L]
  191[label=<191<BR/> <FONT COLOR="grey">0.09503744303386963</FONT>>];
  192 -- 193 [side=R]
  193[label=<193<BR/> <FONT COLOR="grey">0.3644771285875493</FONT>>];
  194 -- 197 [side=R]
  197[label=<197<BR/> <FONT COLOR="grey">0.7948407869124952</FONT>>];
  197 -- 195 [side=L]
  195[label=<195<BR/> <FONT COLOR="grey">0.7942811285510138</FONT>>];
  195 -- 196 [side=R]
  196[label=<196<BR/> <FONT COLOR="grey">0.5627582929517205</FONT>>];
  197 -- 202 [side=R]
  202[label=<202<BR/> <FONT COLOR="grey">0.6971384514391066</FONT>>];
  202 -- 198 [side=L]
  198[label=<198<BR/> <FONT COLOR="grey">0.6927264474372884</FONT>>];
  198 -- 200 [side=R]
  200[label=<200<BR/> <FONT COLOR="grey">0.6455091049085492</FONT>>];
  200 -- 199 [side=L]
  199[label=<199<BR/> <FONT COLOR="grey">0.5952913724552451</FONT>>];
  200 -- 201 [side=R]
  201[label=<201<BR/> <FONT COLOR="grey">0.02134082793304981</FONT>>];
  203 -- 222 [side=R]
  222[label=<222<BR/> <FONT COLOR="grey">0.7905661844403327</FONT>>];
  222 -- 221 [side=L]
  221[label=<221<BR/> <FONT COLOR="grey">0.7344461814842598</FONT>>];
  221 -- 218 [side=L]
  218[label=<218<BR/> <FONT COLOR="grey">0.710158738001777</FONT>>];
  218 -- 214 [side=L]
  214[label=<214<BR/> <FONT COLOR="grey">0.6681641416928006</FONT>>];
  214 -- 210 [side=L]
  210[label=<210<BR/> <FONT COLOR="grey">0.6445674840025054</FONT>>];
  210 -- 204 [side=L]
  204[label=<204<BR/> <FONT COLOR="grey">0.4613335241960753</FONT>>];
  204 -- 207 [side=R]
  207[label=<207<BR/> <FONT COLOR="grey">0.4534069998012402</FONT>>];
  207 -- 205 [side=L]
  205[label=<205<BR/> <FONT COLOR="grey">0.3958801765835097</FONT>>];
  205 -- 206 [side=R]
  206[label=<206<BR/> <FONT COLOR="grey">0.11317604266906478</FONT>>];
  207 -- 208 [side=R]
  208[label=<208<BR/> <FONT COLOR="grey">0.15215468537761012</FONT>>];
  208 -- 209 [side=R]
  209[label=<209<BR/> <FONT COLOR="grey">0.1517155863611125</FONT>>];
  210 -- 213 [side=R]
  213[label=<213<BR/> <FONT COLOR="grey">0.5254528847170963</FONT>>];
  213 -- 212 [side=L]
  212[label=<212<BR/> <FONT COLOR="grey">0.3831362875816451</FONT>>];
  212 -- 211 [side=L]
  211[label=<211<BR/> <FONT COLOR="grey">0.05056020282973139</FONT>>];
  214 -- 215 [side=R]
  215[label=<215<BR/> <FONT COLOR="grey">0.5009367015686823</FONT>>];
  215 -- 216 [side=R]
  216[label=<216<BR/> <FONT COLOR="grey">0.10901874828554337</FONT>>];
  216 -- 217 [side=R]
  217[label=<217<BR/> <FONT COLOR="grey">0.05646524838685241</FONT>>];
  218 -- 220 [side=R]
  220[label=<220<BR/> <FONT COLOR="grey">0.674411240175005</FONT>>];
  220 -- 219 [side=L]
  219[label=<219<BR/> <FONT COLOR="grey">0.14299197003272757</FONT>>];
  222 -- 223 [side=R]
  223[label=<223<BR/> <FONT COLOR="grey">0.6736901076540927</FONT>>];
  223 -- 226 [side=R]
  226[label=<226<BR/> <FONT COLOR="grey">0.6137254504515176</FONT>>];
  226 -- 225 [side=L]
  225[label=<225<BR/> <FONT COLOR="grey">0.42057192814517086</FONT>>];
  225 -- 224 [side=L]
  224[label=<224<BR/> <FONT COLOR="grey">0.15459807255670532</FONT>>];
  226 -- 227 [side=R]
  227[label=<227<BR/> <FONT COLOR="grey">0.3240860179195383</FONT>>];
  227 -- 229 [side=R]
  229[label=<229<BR/> <FONT COLOR="grey">0.1917962317572811</FONT>>];
  229 -- 228 [side=L]
  228[label=<228<BR/> <FONT COLOR="grey">0.09544487292662296</FONT>>];
  230 -- 231 [side=R]
  231[label=<231<BR/> <FONT COLOR="grey">0.9537855788342208</FONT>>];
  231 -- 234 [side=R]
  234[label=<234<BR/> <FONT COLOR="grey">0.8201857812416328</FONT>>];
  234 -- 233 [side=L]
  233[label=<233<BR/> <FONT COLOR="grey">0.38950247492509926</FONT>>];
  233 -- 232 [side=L]
  232[label=<232<BR/> <FONT COLOR="grey">0.3117950672088875</FONT>>];
  235 -- 245 [side=R]
  245[label=<245<BR/> <FONT COLOR="grey">0.9606326065278836</FONT>>];
  245 -- 243 [side=L]
  243[label=<243<BR/> <FONT COLOR="grey">0.9010055124673463</FONT>>];
  243 -- 237 [side=L]
  237[label=<237<BR/> <FONT COLOR="grey">0.8906258722253291</FONT>>];
  237 -- 236 [side=L]
  236[label=<236<BR/> <FONT COLOR="grey">0.699210568923407</FONT>>];
  237 -- 239 [side=R]
  239[label=<239<BR/> <FONT COLOR="grey">0.8460173468831691</FONT>>];
  239 -- 238 [side=L]
  238[label=<238<BR/> <FONT COLOR="grey">0.11026801706799916</FONT>>];
  239 -- 242 [side=R]
  242[label=<242<BR/> <FONT COLOR="grey">0.8439586286718704</FONT>>];
  242 -- 240 [side=L]
  240[label=<240<BR/> <FONT COLOR="grey">0.6602788030770246</FONT>>];
  240 -- 241 [side=R]
  241[label=<241<BR/> <FONT COLOR="grey">0.5119345141518413</FONT>>];
  243 -- 244 [side=R]
  244[label=<244<BR/> <FONT COLOR="grey">0.7247536554274525</FONT>>];
  245 -- 246 [side=R]
  246[label=<246<BR/> <FONT COLOR="grey">0.5721754588545217</FONT>>];
  246 -- 247 [side=R]
  247[label=<247<BR/> <FONT COLOR="grey">0.5181752052426042</FONT>>];
  247 -- 249 [side=R]
  249[label=<249<BR/> <FONT COLOR="grey">0.07619775425067954</FONT>>];
  249 -- 248 [side=L]
  248[label=<248<BR/> <FONT COLOR="grey">0.03123446276525388</FONT>>];
}
graph {
  node [shape=circle]
  2[label=<2<BR/> <FONT COLOR="grey">0.9957869495270144</FONT>>];
  2 -- 1 [side=L]
  1[label=<1<BR/> <FONT COLOR="grey">0.8476937427586738</FONT>>];
  1 -- 0 [side=L]
  0[label=<0<BR/> <FONT COLOR="grey">0.7514570833207395</FONT>>];
  2 -- 145 [side=R]
  145[label=<145<BR/> <FONT COLOR="grey">0.9939888728473314</FONT>>];
  145 -- 12 [side=L]
  12[label=<12<BR/> <FONT COLOR="grey">0.9907319285760915</FONT>>];
  12 -- 5 [side=L]
  5[label=<5<BR/> <FONT COLOR="grey">0.988082370881665</FONT>>];
  5 -- 4 [side=L]
  4[label=<4<BR/> <FONT COLOR="grey">0.7422137887388395</FONT>>];
  4 -- 3 [side=L]
  3[label=<3<BR/> <FONT COLOR="grey">0.24440218394323976</FONT>>];
  5 -- 8 [side=R]
  8[label=<8<BR/> <FONT COLOR="grey">0.695547503791194</FONT>>];
  8 -- 6 [side=L]
  6[label=<6<BR/> <FONT COLOR="grey">0.5664414792316717</FONT>>];
  6 -- 7 [side=R]
  7[label=<7<BR/> <FONT COLOR="grey">0.11672897928969816</FONT>>];
  8 -- 11 [side=R]
  11[label=<11<BR/> <FONT COLOR="grey">0.45404068759973704</FONT>>];
  11 -- 9 [side=L]
  9[label=<9<BR/> <FONT COLOR="grey">0.3325186970255505</FONT>>];
  9 -- 10 [side=R]
  10[label=<10<BR/> <FONT COLOR="grey">0.10578109016044712</FONT>>];
  12 -- 47 [side=R]
  47[label=<47<BR/> <FONT COLOR="grey">0.9904839559513964</FONT>>];
  47 -- 33 [side=L]
  33[label=<33<BR/> <FONT COLOR="grey">0.9758188259688814</FONT>>];
  33 -- 28 [side=L]
  28[label=<28<BR/> <FONT COLOR="grey">0.915671870648212</FONT>>];
  28 -- 23 [side=L]
  23[label=<23<BR/> <FONT COLOR="grey">0.8487360320264957</FONT>>];
  23 -- 20 [side=L]
  20[label=<20<BR/> <FONT COLOR="grey">0.7063809958484802</FONT>>];
  20 -- 16 [side=L]
  16[label=<16<BR/> <FONT COLOR="grey">0.7034299359160114</FONT>>];
  16 -- 15 [side=L]
  15[label=<15<BR/> <FONT COLOR="grey">0.5922634040068163</FONT>>];
  15 -- 13 [side=L]
  13[label=<13<BR/> <FONT COLOR="grey">0.4543498778672278</FONT>>];
  13 -- 14 [side=R]
  14[label=<14<BR/> <FONT COLOR="grey">0.21605966187340298</FONT>>];
  16 -- 17 [side=R]
  17[label=<17<BR/> <FONT COLOR="grey">0.3709284358038115</FONT>>];
  17 -- 18 [side=R]
  18[label=<18<BR/> <FONT COLOR="grey">0.260599350868457</FONT>>];
  18 -- 19 [side=R]
  19[label=<19<BR/> <FONT COLOR="grey">0.1817886048339078</FONT>>];
  20 -- 21 [side=R]
  21[label=<21<BR/> <FONT COLOR="grey">0.14210053829804103</FONT>>];
  21 -- 22 [side=R]
  22[label=<22<BR/> <FONT COLOR="grey">0.08869917426783513</FONT>>];
  23 -- 25 [side=R]
  25[label=<25<BR/> <FONT COLOR="grey">0.4892716208875628</FONT>>];
  25 -- 24 [side=L]
  24[label=<24<BR/> <FONT COLOR="grey">0.25363061372658946</FONT>>];
  25 -- 26 [side=R]
  26[label=<26<BR/> <FONT COLOR="grey">0.13796660931463622</FONT>>];
  26 -- 27 [side=R]
  27[label=<27<BR/> <FONT COLOR="grey">0.12997273552181765</FONT>>];
  28 -- 31 [side=R]
  31[label=<31<BR/> <FONT COLOR="grey">0.8601319422972336</FONT>>];
  31 -- 29 [side=L]
  29[label=<29<BR/> <FONT COLOR="grey">0.8084698172467167</FONT>>];
  29 -- 30 [side=R]
  30[label=<30<BR/> <FONT COLOR="grey">0.27908621178937176</FONT>>];
  31 -- 32 [side=R]
  32[label=<32<BR/> <FONT COLOR="grey">0.0844421977476495</FONT>>];
  33 -- 35 [side=R]
  35[label=<35<BR/> <FONT COLOR="grey">0.967049096465691</FONT>>];
  35 -- 34 [side=L]
  34[label=<34<BR/> <FONT COLOR="grey">0.12377873295631248</FONT>>];
  35 -- 39 [side=R]
  39[label=<39<BR/> <FONT COLOR="grey">0.9328169604588316</FONT>>];
  39 -- 38 [side=L]
  38[label=<38<BR/> <FONT COLOR="grey">0.7787222122657034</FONT>>];
  38 -- 37 [side=L]
  37[label=<37<BR/> <FONT COLOR="grey">0.4043690842717128</FONT>>];
  37 -- 36 [side=L]
  36[label=<36<BR/> <FONT COLOR="grey">0.3046740248996985</FONT>>];
  39 -- 43 [side=R]
  43[label=<43<BR/> <FONT COLOR="grey">0.8350649185763811</FONT>>];
  43 -- 40 [side=L]
  40[label=<40<BR/> <FONT COLOR="grey">0.5406537578359348</FONT>>];
  40 -- 42 [side=R]
  42[label=<42<BR/> <FONT COLOR="grey">0.3193437618727414</FONT>>];
  42 -- 41 [side=L]
  41[label=<41<BR/> <FONT COLOR="grey">0.27788504363715394</FONT>>];
  43 -- 44 [side=R]
  44[label=<44<BR/> <FONT COLOR="grey">0.4830145820991081</FONT>>];
  44 -- 46 [side=R]
  46[label=<46<BR/> <FONT COLOR="grey">0.33052118143151343</FONT>>];
  46 -- 45 [side=L]
  45[label=<45<BR/> <FONT COLOR="grey">0.29551968300055065</FONT>>];
  47 -- 114 [side=R]
  114[label=<114<BR/> <FONT COLOR="grey">0.9876511078116038</FONT>>];
  114 -- 86 [side=L]
  86[label=<86<BR/> <FONT COLOR="grey">0.9735379804960496</FONT>>];
  86 -- 77 [side=L]
  77[label=<77<BR/> <FONT COLOR="grey">0.9269342780771458</FONT>>];
  77 -- 68 [side=L]
  68[label=<68<BR/> <FONT COLOR="grey">0.9230582358699485</FONT>>];
  68 -- 55 [side=L]
  55[label=<55<BR/> <FONT COLOR="grey">0.839490204679298</FONT>>];
  55 -- 51 [side=L]
  51[label=<51<BR/> <FONT COLOR="grey">0.3385715557270519</FONT>>];
  51 -- 50 [side=L]
  50[label=<50<BR/> <FONT COLOR="grey">0.3360608605201836</FONT>>];
  50 -- 49 [side=L]
  49[label=<49<BR/> <FONT COLOR="grey">0.2774445142713422</FONT>>];
  49 -- 48 [side=L]
  48[label=<48<BR/> <FONT COLOR="grey">0.17489390663513982</FONT>>];
  51 -- 52 [side=R]
  52[label=<52<BR/> <FONT COLOR="grey">0.1886079446757799</FONT>>];
  52 -- 54 [side=R]
  54[label=<54<BR/> <FONT COLOR="grey">0.12293678964136756</FONT>>];
  54 -- 53 [side=L]
  53[label=<53<BR/> <FONT COLOR="grey">0.08266549169366844</FONT>>];
  55 -- 64 [side=R]
  64[label=<64<BR/> <FONT COLOR="grey">0.5480541758673416</FONT>>];
  64 -- 61 [side=L]
  61[label=<61<BR/> <FONT COLOR="grey">0.4333510895474586</FONT>>];
  61 -- 58 [side=L]
  58[label=<58<BR/> <FONT COLOR="grey">0.22965679673111572</FONT>>];
  58 -- 57 [side=L]
  57[label=<57<BR/> <FONT COLOR="grey">0.1750392554452569</FONT>>];
  57 -- 56 [side=L]
  56[label=<56<BR/> <FONT COLOR="grey">0.1154682181055644</FONT>>];
  58 -- 60 [side=R]
  60[label=<60<BR/> <FONT COLOR="grey">0.19505416228675265</FONT>>];
  60 -- 59 [side=L]
  59[label=<59<BR/> <FONT COLOR="grey">0.15628574332026035</FONT>>];
  61 -- 63 [side=R]
  63[label=<63<BR/> <FONT COLOR="grey">0.23625159275800722</FONT>>];
  63 -- 62 [side=L]
  62[label=<62<BR/> <FONT COLOR="grey">0.10778840213677576</FONT>>];
  64 -- 66 [side=R]
  66[label=<66<BR/> <FONT COLOR="grey">0.4795221664420316</FONT>>];
  66 -- 65 [side=L]
  65[label=<65<BR/> <FONT COLOR="grey">0.29689544767487486</FONT>>];
  66 -- 67 [side=R]
  67[label=<67<BR/> <FONT COLOR="grey">0.1446107813011439</FONT>>];
  68 -- 75 [side=R]
  75[label=<75<BR/> <FONT COLOR="grey">0.9136368186744673</FONT>>];
  75 -- 73 [side=L]
  73[label=<73<BR/> <FONT COLOR="grey">0.8180762618516633</FONT>>];
  73 -- 72 [side=L]
  72[label=<72<BR/> <FONT COLOR="grey">0.790052528538962</FONT>>];
  72 -- 69 [side=L]
  69[label=<69<BR/> <FONT COLOR="grey">0.6662534331501744</FONT>>];
  69 -- 70 [side=R]
  70[label=<70<BR/> <FONT COLOR="grey">0.198170103856931</FONT>>];
  70 -- 71 [side=R]
  71[label=<71<BR/> <FONT COLOR="grey">0.09798170568469355</FONT>>];
  73 -- 74 [side=R]
  74[label=<74<BR/> <FONT COLOR="grey">0.3113326781632004</FONT>>];
  75 -- 76 [side=R]
  76[label=<76<BR/> <FONT COLOR="grey">0.878956270284837</FONT>>];
  77 -- 84 [side=R]
  84[label=<84<BR/> <FONT COLOR="grey">0.7298827990732385</FONT>>];
  84 -- 81 [side=L]
  81[label=<81<BR/> <FONT COLOR="grey">0.7294243899593819</FONT>>];
  81 -- 78 [side=L]
  78[label=<78<BR/> <FONT COLOR="grey">0.6751780813023192</FONT>>];
  78 -- 80 [side=R]
  80[label=<80<BR/> <FONT COLOR="grey">0.2765792284019555</FONT>>];
  80 -- 79 [side=L]
  79[label=<79<BR/> <FONT COLOR="grey">0.12189576635143229</FONT>>];
  81 -- 83 [side=R]
  83[label=<83<BR/> <FONT COLOR="grey">0.5986915452921237</FONT>>];
  83 -- 82 [side=L]
  82[label=<82<BR/> <FONT COLOR="grey">0.5846092825823913</FONT>>];
  84 -- 85 [side=R]
  85[label=<85<BR/> <FONT COLOR="grey">0.5030186847252209</FONT>>];
  86 -- 103 [side=R]
  103[label=<103<BR/> <FONT COLOR="grey">0.968253245483458</FONT>>];
  103 -- 88 [side=L]
  88[label=<88<BR/> <FONT COLOR="grey">0.9609670084738324</FONT>>];
  88 -- 87 [side=L]
  87[label=<87<BR/> <FONT COLOR="grey">0.5446892558733331</FONT>>];
  88 -- 97 [side=R]
  97[label=<97<BR/> <FONT COLOR="grey">0.9483161815322799</FONT>>];
  97 -- 92 [side=L]
  92[label=<92<BR/> <FONT COLOR="grey">0.9417148997777522</FONT>>];
  92 -- 89 [side=L]
  89[label=<89<BR/> <FONT COLOR="grey">0.2455511319571373</FONT>>];
  89 -- 90 [side=R]
  90[label=<90<BR/> <FONT COLOR="grey">0.15647633602612276</FONT>>];
  90 -- 91 [side=R]
  91[label=<91<BR/> <FONT COLOR="grey">0.13997459061178652</FONT>>];
  92 -- 93 [side=R]
  93[label=<93<BR/> <FONT COLOR="grey">0.7804508194274744</FONT>>];
  93 -- 95 [side=R]
  95[label=<95<BR/> <FONT COLOR="grey">0.7226672256819942</FONT>>];
  95 -- 94 [side=L]
  94[label=<94<BR/> <FONT COLOR="grey">0.43503881905350683</FONT>>];
  95 -- 96 [side=R]
  96[label=<96<BR/> <FONT COLOR="grey">0.40712673636645136</FONT>>];
  97 -- 101 [side=R]
  101[label=<101<BR/> <FONT COLOR="grey">0.9255359833317219</FONT>>];
  101 -- 99 [side=L]
  99[label=<99<BR/> <FONT COLOR="grey">0.7139348283525223</FONT>>];
  99 -- 98 [side=L]
  98[label=<98<BR/> <FONT COLOR="grey">0.21279150783985867</FONT>>];
  99 -- 100 [side=R]
  100[label=<100<BR/> <FONT COLOR="grey">0.48002453571456083</FONT>>];
  101 -- 102 [side=R]
  102[label=<102<BR/> <FONT COLOR="grey">0.6219674393805691</FONT>>];
  103 -- 106 [side=R]
  106[label=<106<BR/> <FONT COLOR="grey">0.8731379458552129</FONT>>];
  106 -- 104 [side=L]
  104[label=<104<BR/> <FONT COLOR="grey">0.5400277589431796</FONT>>];
  104 -- 105 [side=R]
  105[label=<105<BR/> <FONT COLOR="grey">0.0007891566377928871</FONT>>];
  106 -- 110 [side=R]
  110[label=<110<BR/> <FONT COLOR="grey">0.8382929650437165</FONT>>];
  110 -- 109 [side=L]
  109[label=<109<BR/> <FONT COLOR="grey">0.4615149406437542</FONT>>];
  109 -- 107 [side=L]
  107[label=<107<BR/> <FONT COLOR="grey">0.43337904959817486</FONT>>];
  107 -- 108 [side=R]
  108[label=<108<BR/> <FONT COLOR="grey">0.053658620013432023</FONT>>];
  110 -- 112 [side=R]
  112[label=<112<BR/> <FONT COLOR="grey">0.5679810533328041</FONT>>];
  112 -- 111 [side=L]
  111[label=<111<BR/> <FONT COLOR="grey">0.541018763444613</FONT>>];
  112 -- 113 [side=R]
  113[label=<113<BR/> <FONT COLOR="grey">0.42841646768139463</FONT>>];
  114 -- 123 [side=R]
  123[label=<123<BR/> <FONT COLOR="grey">0.9464643826259924</FONT>>];
  123 -- 119 [side=L]
  119[label=<119<BR/> <FONT COLOR="grey">0.8662877311047388</FONT>>];
  119 -- 116 [side=L]
  116[label=<116<BR/> <FONT COLOR="grey">0.7670170824076343</FONT>>];
  116 -- 115 [side=L]
  115[label=<115<BR/> <FONT COLOR="grey">0.17190031141596263</FONT>>];
  116 -- 118 [side=R]
  118[label=<118<BR/> <FONT COLOR="grey">0.3671674221014496</FONT>>];
  118 -- 117 [side=L]
  117[label=<117<BR/> <FONT COLOR="grey">0.2159590427591035</FONT>>];
  119 -- 121 [side=R]
  121[label=<121<BR/> <FONT COLOR="grey">0.74738638353363</FONT>>];
  121 -- 120 [side=L]
  120[label=<120<BR/> <FONT COLOR="grey">0.08665916013292596</FONT>>];
  121 -- 122 [side=R]
  122[label=<122<BR/> <FONT COLOR="grey">0.4119543486009871</FONT>>];
  123 -- 143 [side=R]
  143[label=<143<BR/> <FONT COLOR="grey">0.9247442715890171</FONT>>];
  143 -- 140 [side=L]
  140[label=<140<BR/> <FONT COLOR="grey">0.8936337545064225</FONT>>];
  140 -- 125 [side=L]
  125[label=<125<BR/> <FONT COLOR="grey">0.8517904638602968</FONT>>];
  125 -- 124 [side=L]
  124[label=<124<BR/> <FONT COLOR="grey">0.681815353261336</FONT>>];
  125 -- 126 [side=R]
  126[label=<126<BR/> <FONT COLOR="grey">0.7946927108945189</FONT>>];
  126 -- 133 [side=R]
  133[label=<133<BR/> <FONT COLOR="grey">0.7289879153749418</FONT>>];
  133 -- 128 [side=L]
  128[label=<128<BR/> <FONT COLOR="grey">0.7256302169867033</FONT>>];
  128 -- 127 [side=L]
  127[label=<127<BR/> <FONT COLOR="grey">0.6200343851508556</FONT>>];
  128 -- 131 [side=R]
  131[label=<131<BR/> <FONT COLOR="grey">0.7089639682237622</FONT>>];
  131 -- 129 [side=L]
  129[label=<129<BR/> <FONT COLOR="grey">0.5255088944172916</FONT>>];
  129 -- 130 [side=R]
  130[label=<130<BR/> <FONT COLOR="grey">0.41550021924797953</FONT>>];
  131 -- 132 [side=R]
  132[label=<132<BR/> <FONT COLOR="grey">0.1851371011016204</FONT>>];
  133 -- 134 [side=R]
  134[label=<134<BR/> <FONT COLOR="grey">0.6333388625482715</FONT>>];
  134 -- 135 [side=R]
  135[label=<135<BR/> <FONT COLOR="grey">0.4707182839428111</FONT>>];
  135 -- 138 [side=R]
  138[label=<138<BR/> <FONT COLOR="grey">0.4128350639522185</FONT>>];
  138 -- 137 [side=L]
  137[label=<137<BR/> <FONT COLOR="grey">0.25475301393491523</FONT>>];
  137 -- 136 [side=L]
  136[label=<136<BR/> <FONT COLOR="grey">0.04841095612377411</FONT>>];
  138 -- 139 [side=R]
  139[label=<139<BR/> <FONT COLOR="grey">0.3855980374729622</FONT>>];
  140 -- 141 [side=R]
  141[label=<141<BR/> <FONT COLOR="grey">0.8655582249610275</FONT>>];
  141 -- 142 [side=R]
  142[label=<142<BR/> <FONT COLOR="grey">0.2288591915235536</FONT>>];
  143 -- 144 [side=R]
  144[label=<144<BR/> <FONT COLOR="grey">0.36710686055015185</FONT>>];
  145 -- 235 [side=R]
  235[label=<235<BR/> <FONT COLOR="grey">0.9812914040931304</FONT>>];
  235 -- 169 [side=L]
  169[label=<169<BR/> <FONT COLOR="grey">0.9782585107854103</FONT>>];
  169 -- 148 [side=L]
  148[label=<148<BR/> <FONT COLOR="grey">0.9449023406787344</FONT>>];
  148 -- 147 [side=L]
  147[label=<147<BR/> <FONT COLOR="grey">0.7293740098286456</FONT>>];
  147 -- 146 [side=L]
  146[label=<146<BR/> <FONT COLOR="grey">0.509027051210447</FONT>>];
  148 -- 150 [side=R]
  150[label=<150<BR/> <FONT COLOR="grey">0.9020436951014624</FONT>>];
  150 -- 149 [side=L]
  149[label=<149<BR/> <FONT COLOR="grey">0.8190200516813236</FONT>>];
  150 -- 153 [side=R]
  153[label=<153<BR/> <FONT COLOR="grey">0.8855777631108436</FONT>>];
  153 -- 152 [side=L]
  152[label=<152<BR/> <FONT COLOR="grey">0.8603376027563862</FONT>>];
  152 -- 151 [side=L]
  151[label=<151<BR/> <FONT COLOR="grey">0.45459695989529647</FONT>>];
  153 -- 167 [side=R]
  167[label=<167<BR/> <FONT COLOR="grey">0.8492899523613442</FONT>>];
  167 -- 163 [side=L]
  163[label=<163<BR/> <FONT COLOR="grey">0.8177575678069656</FONT>>];
  163 -- 160 [side=L]
  160[label=<160<BR/> <FONT COLOR="grey">0.7759852430212377</FONT>>];
  160 -- 158 [side=L]
  158[label=<158<BR/> <FONT COLOR="grey">0.734924251943539</FONT>>];
  158 -- 156 [side=L]
  156[label=<156<BR/> <FONT COLOR="grey">0.710110660084087</FONT>>];
  156 -- 155 [side=L]
  155[label=<155<BR/> <FONT COLOR="grey">0.2959097323390557</FONT>>];
  155 -- 154 [side=L]
  154[label=<154<BR/> <FONT COLOR="grey">0.23060113716089548</FONT>>];
  156 -- 157 [side=R]
  157[label=<157<BR/> <FONT COLOR="grey">0.04398748527099361</FONT>>];
  158 -- 159 [side=R]
  159[label=<159<BR/> <FONT COLOR="grey">0.09047000528918914</FONT>>];
  160 -- 162 [side=R]
  162[label=<162<BR/> <FONT COLOR="grey">0.7216878041963218</FONT>>];
  162 -- 161 [side=L]
  161[label=<161<BR/> <FONT COLOR="grey">0.6871202472393689</FONT>>];
  163 -- 165 [side=R]
  165[label=<165<BR/> <FONT COLOR="grey">0.3886140063494328</FONT>>];
  165 -- 164 [side=L]
  164[label=<164<BR/> <FONT COLOR="grey">0.3870484684485387</FONT>>];
  165 -- 166 [side=R]
  166[label=<166<BR/> <FONT COLOR="grey">0.3476475192762013</FONT>>];
  167 -- 168 [side=R]
  168[label=<168<BR/> <FONT COLOR="grey">0.8169397159175148</FONT>>];
  169 -- 172 [side=R]
  172[label=<172<BR/> <FONT COLOR="grey">0.9747928133504129</FONT>>];
  172 -- 170 [side=L]
  170[label=<170<BR/> <FONT COLOR="grey">0.846452976104617</FONT>>];
  170 -- 171 [side=R]
  171[label=<171<BR/> <FONT COLOR="grey">0.3121690933042578</FONT>>];
  172 -- 230 [side=R]
  230[label=<230<BR/> <FONT COLOR="grey">0.958355558453681</FONT>>];
  230 -- 187 [side=L]
  187[label=<187<BR/> <FONT COLOR="grey">0.957557676245447</FONT>>];
  187 -- 180 [side=L]
  180[label=<180<BR/> <FONT COLOR="grey">0.92710438449752</FONT>>];
  180 -- 174 [side=L]
  174[label=<174<BR/> <FONT COLOR="grey">0.7049973940003331</FONT>>];
  174 -- 173 [side=L]
  173[label=<173<BR/> <FONT COLOR="grey">0.152023809588824</FONT>>];
  174 -- 179 [side=R]
  179[label=<179<BR/> <FONT COLOR="grey">0.6715227532708304</FONT>>];
  179 -- 176 [side=L]
  176[label=<176<BR/> <FONT COLOR="grey">0.6621862385446002</FONT>>];
  176 -- 175 [side=L]
  175[label=<175<BR/> <FONT COLOR="grey">0.1248086920689816</FONT>>];
  176 -- 178 [side=R]
  178[label=<178<BR/> <FONT COLOR="grey">0.5713418767827186</FONT>>];
  178 -- 177 [side=L]
  177[label=<177<BR/> <FONT COLOR="grey">0.5560171098718876</FONT>>];
  180 -- 183 [side=R]
  183[label=<183<BR/> <FONT COLOR="grey">0.9250644230760526</FONT>>];
  183 -- 182 [side=L]
  182[label=<182<BR/> <FONT COLOR="grey">0.6824354777265715</FONT>>];
  182 -- 181 [side=L]
  181[label=<181<BR/> <FONT COLOR="grey">0.012508171868127804</FONT>>];
  183 -- 184 [side=R]
  184[label=<184<BR/> <FONT COLOR="grey">0.4823271793225655</FONT>>];
  184 -- 185 [side=R]
  185[label=<185<BR/> <FONT COLOR="grey">0.3841022310929997</FONT>>];
  185 -- 186 [side=R]
  186[label=<186<BR/> <FONT COLOR="grey">0.18775042542247533</FONT>>];
  187 -- 203 [side=R]
  203[label=<203<BR/> <FONT COLOR="grey">0.8944186382789656</FONT>>];
  203 -- 194 [side=L]
  194[label=<194<BR/> <FONT COLOR="grey">0.878541152511799</FONT>>];
  194 -- 190 [side=L]
  190[label=<190<BR/> <FONT COLOR="grey">0.6762694909925744</FONT>>];
  190 -- 188 [side=L]
  188[label=<188<BR/> <FONT COLOR="grey">0.3829541869540283</FONT>>];
  188 -- 189 [side=R]
  189[label=<189<BR/> <FONT COLOR="grey">0.20943675668859407</FONT>>];
  190 -- 192 [side=R]
  192[label=<192<BR/> <FONT COLOR="grey">0.5817469625147841</FONT>>];
  192 -- 191 [side=L]
  191[label=<191<BR/> <FONT COLOR="grey">0.09503744303386963</FONT>>];
  192 -- 193 [side=R]
  193[label=<193<BR/> <FONT COLOR="grey">0.3644771285875493</FONT>>];
  194 -- 197 [side=R]
  197[label=<197<BR/> <FONT COLOR="grey">0.7948407869124952</FONT>>];
  197 -- 195 [side=L]
  195[label=<195<BR/> <FONT COLOR="grey">0.7942811285510138</FONT>>];
  195 -- 196 [side=R]
  196[label=<196<BR/> <FONT COLOR="grey">0.5627582929517205</FONT>>];
  197 -- 202 [side=R]
  202[label=<202<BR/> <FONT COLOR="grey">0.6971384514391066</FONT>>];
  202 -- 198 [side=L]
  198[label=<198<BR/> <FONT COLOR="grey">0.6927264474372884</FONT>>];
  198 -- 200 [side=R]
  200[label=<200<BR/> <FONT COLOR="grey">0.6455091049085492</FONT>>];
  200 -- 199 [side=L]
  199[label=<199<BR/> <FONT COLOR="grey">0.5952913724552451</FONT>>];
  200 -- 201 [side=R]
  201[label=<201<BR/> <FONT COLOR="grey">0.02134082793304981</FONT>>];
  203 -- 222 [side=R]
  222[label=<222<BR/> <FONT COLOR="grey">0.7905661844403327</FONT>>];
  222 -- 221 [side=L]
  221[label=<221<BR/> <FONT COLOR="grey">0.7344461814842598</FONT>>];
  221 -- 218 [side=L]
  218[label=<218<BR/> <FONT COLOR="grey">0.710158738001777</FONT>>];
  218 -- 214 [side=L]
  214[label=<214<BR/> <FONT COLOR="grey">0.6681641416928006</FONT>>];
  214 -- 210 [side=L]
  210[label=<210<BR/> <FONT COLOR="grey">0.6445674840025054</FONT>>];
  210 -- 204 [side=L]
  204[label=<204<BR/> <FONT COLOR="grey">0.4613335241960753</FONT>>];
  204 -- 207 [side=R]
  207[label=<207<BR/> <FONT COLOR="grey">0.4534069998012402</FONT>>];
  207 -- 205 [side=L]
  205[label=<205<BR/> <FONT COLOR="grey">0.3958801765835097</FONT>>];
  205 -- 206 [side=R]
  206[label=<206<BR/> <FONT COLOR="grey">0.11317604266906478</FONT>>];
  207 -- 208 [side=R]
  208[label=<208<BR/> <FONT COLOR="grey">0.15215468537761012</FONT>>];
  208 -- 209 [side=R]
  209[label=<209<BR/> <FONT COLOR="grey">0.1517155863611125</FONT>>];
  210 -- 213 [side=R]
  213[label=<213<BR/> <FONT COLOR="grey">0.5254528847170963</FONT>>];
  213 -- 212 [side=L]
  212[label=<212<BR/> <FONT COLOR="grey">0.3831362875816451</FONT>>];
  212 -- 211 [side=L]
  211[label=<211<BR/> <FONT COLOR="grey">0.05056020282973139</FONT>>];
  214 -- 215 [side=R]
  215[label=<215<BR/> <FONT COLOR="grey">0.5009367015686823</FONT>>];
  215 -- 216 [side=R]
  216[label=<216<BR/> <FONT COLOR="grey">0.10901874828554337</FONT>>];
  216 -- 217 [side=R]
  217[label=<217<BR/> <FONT COLOR="grey">0.05646524838685241</FONT>>];
  218 -- 220 [side=R]
  220[label=<220<BR/> <FONT COLOR="grey">0.674411240175005</FONT>>];
  220 -- 219 [side=L]
  219[label=<219<BR/> <FONT COLOR="grey">0.14299197003272757</FONT>>];
  222 -- 223 [side=R]
  223[label=<223<BR/> <FONT COLOR="grey">0.6736901076540927</FONT>>];
  223 -- 226 [side=R]
  226[label=<226<BR/> <FONT COLOR="grey">0.6137254504515176</FONT>>];
  226 -- 225 [side=L]
  225[label=<225<BR/> <FONT COLOR="grey">0.42057192814517086</FONT>>];
  225 -- 224 [side=L]
  224[label=<224<BR/> <FONT COLOR="grey">0.15459807255670532</FONT>>];
  226 -- 227 [side=R]
  227[label=<227<BR/> <FONT COLOR="grey">0.3240860179195383</FONT>>];
  227 -- 229 [side=R]
  229[label=<229<BR/> <FONT COLOR="grey">0.1917962317572811</FONT>>];
  229 -- 228 [side=L]
  228[label=<228<BR/> <FONT COLOR="grey">0.09544487292662296</FONT>>];
  230 -- 231 [side=R]
  231[label=<231<BR/> <FONT COLOR="grey">0.9537855788342208</FONT>>];
  231 -- 234 [side=R]
  234[label=<234<BR/> <FONT COLOR="grey">0.8201857812416328</FONT>>];
  234 -- 233 [side=L]
  233[label=<233<BR/> <FONT COLOR="grey">0.38950247492509926</FONT>>];
  233 -- 232 [side=L]
  232[label=<232<BR/> <FONT COLOR="grey">0.3117950672088875</FONT>>];
  235 -- 245 [side=R]
  245[label=<245<BR/> <FONT COLOR="grey">0.9606326065278836</FONT>>];
  245 -- 243 [side=L]
  243[label=<243<BR/> <FONT COLOR="grey">0.9010055124673463</FONT>>];
  243 -- 237 [side=L]
  237[label=<237<BR/> <FONT COLOR="grey">0.8906258722253291</FONT>>];
  237 -- 236 [side=L]
  236[label=<236<BR/> <FONT COLOR="grey">0.699210568923407</FONT>>];
  237 -- 239 [side=R]
  239[label=<239<BR/> <FONT COLOR="grey">0.8460173468831691</FONT>>];
  239 -- 238 [side=L]
  238[label=<238<BR/> <FONT COLOR="grey">0.11026801706799916</FONT>>];
  239 -- 242 [side=R]
  242[label=<242<BR/> <FONT COLOR="grey">0.8439586286718704</FONT>>];
  242 -- 240 [side=L]
  240[label=<240<BR/> <FONT COLOR="grey">0.6602788030770246</FONT>>];
  240 -- 241 [side=R]
  241[label=<241<BR/> <FONT COLOR="grey">0.5119345141518413</FONT>>];
  243 -- 244 [side=R]
  244[label=<244<BR/> <FONT COLOR="grey">0.7247536554274525</FONT>>];
  245 -- 246 [side=R]
  246[label=<246<BR/> <FONT COLOR="grey">0.5721754588545217</FONT>>];
  246 -- 247 [side=R]
  247[label=<247<BR/> <FONT COLOR="grey">0.5181752052426042</FONT>>];
  247 -- 249 [side=R]
  249[label=<249<BR/> <FONT COLOR="grey">0.07619775425067954</FONT>>];
  249 -- 248 [side=L]
  248[label=<248<BR/> <FONT COLOR="grey">0.03123446276525388</FONT>>];
}

    

Implementation

I’ll be keeping the theme for this series by using Python to implement the Treap. This leads to somewhat annoying code to handle the rotation process, which is easier to do in C using pointers.

Representation

Creating a new Treap is easy: the tree starts off empty, waiting for new nodes to insert.

Each Node must keep track of the key, the mapped value, and the node’s priority (which is assigned randomly). Finally it must also allow for storing two children (left and right).

class Node[K, V]:
    key: K
    value: V
    priority: float
    left: Node[K, V] | None
    righg: Node[K, V] | None

    def __init__(self, key: K, value: V):
        # Store key and value, like a normal BST node
        self.key = key
        self.value = value
        # Priority is derived randomly
        self.priority = random()
        self.left = None
        self.right = None

class Treap[K, V]:
    _root: Node[K, V] | None

    def __init__(self):
        # The tree starts out empty
        self._root = None

Searching the tree is the same as in any other Binary Search Tree.

def get(self, key: K) -> T | None:
    node = self._root
    # The usual BST traversal
    while node is not None:
        if node.key == key:
            return node.value
        elif node.key < key:
            node = node.right
        else:
            node = node.left
    return None

Insertion

To insert a new key into the tree, we identify which leaf position it should be inserted at. We then generate the node’s priority, insert it at this position, and rotate the node upwards until the heap property is respected.

type ChildField = Literal["left, right"]

def insert(self, key: K, value: V) -> bool:
    # Empty treap base-case
    if self._root is None:
        self._root = Node(key, value)
        # Signal that we're not overwriting the value
        return False
    # Keep track of the parent chain for rotation after insertion
    parents = []
    node = self._root
    while node is not None:
        # Insert a pre-existing key
        if node.key == key:
            node.value = value
            return True
        #  Go down the tree, keep track of the path through the tree
        field = "left" if key < node.key else "right"
        parents.append((node, field))
        node = getattr(node, field)
    #  Key wasn't found, we're inserting a new node
    child = Node(key, value)
    parent, field = parents[-1]
    setattr(parent, field, child)
    # Rotate the new node up until we respect the decreasing priority property
    self._rotate_up(child, parents)
    # Key wasn't found, signal that we inserted a new node
    return False

def _rotate_up(
    self,
    node: Node[K, V],
    parents: list[tuple[Node[K, V], ChildField]],
) -> None:
    while parents:
        parent, field = parents.pop()
        # If the parent has higher priority, we're done rotating
        if parent.priority >= node.priority:
            break
        # Check for grand-parent/root of tree edge-case
        if parents:
            # Update grand-parent to point to the new rotated node
            grand_parent, field = parents[-1]
            setattr(grand_parent, field, node)
        else:
            # Point the root to the new rotated node
            self._root = node
        other_field = "left" if field == "right" else "right"
        # Rotate the node up
        setattr(parent, field, getattr(node, other_field))
        setattr(node, other_field, parent)