Array implementationof a balanced binary search tree<br />
Given a set oforderedelements, weaskourselvesifitispossibletobuild a binarysearchtreeupontheseelements in a sequential dat...
Given the set<br />containing5 elements, the balancedbinarysearchtreeoftheseelementsusuallyisrepresented in a ‘linked’ way...
3<br />lvl 0<br />Treeismadeof 3 levels<br />(withroot at level 0)<br />Ithas5 ‘leaves’ and 4 ‘nodes’<br />Formally, ifnis...
Torepresentthattreeweuse the samerepresentationusedfor a similar data structure, usuallycalledheap(maxo min)<br />A node a...
Ifwealreadyhave a linkedrepresentationof the treebased on oursortedelements, itis possibile to transfer it in a arrayrepre...
Buthow do webuildthistree in a bottom-up way ifwe don’t have the linkedrepresentationof the tree, butonlyourelementssorted...
Note the elements(1 2 3 4 5) order:<br />The leaves(redones), aresstored in a ascendingorderbut, let’s say, in a ‘broken’ ...
Weuse the basictheoryofbinarytrees and it’s link with the powerof the number 2 anwewilluse the toollog2(log base 2)<br />W...
In ourexamplewehavethat 22&lt; 5 &lt; 23 , <br />4 &lt; 5 &lt; 8<br />In thisimagewe note thatonly ONE nodeoflevel i ( in ...
This 2  ( is the resultof 1*2, [numer_of_node_we_were_looking_for]*2 ), <br />represent the numberofelementtomovefrom head...
Firtswemustunderstandhowtoobtainthat4 ofourexamplewithoutanyknowledgeabout the numberoflevelthere are in our future tree<b...
log2 5 = 2.3219...<br />floor(log2 5) = 2<br />Nowwehave the numberofleveli <br />Toknowhowmanynodes are usuallystored at ...
Let’s create anarrayoflenght (internalnodes)+(leaves)+1, in our case 10 ( 4 + 5 +1) , and weourvectorwithmovedelements<br ...
Foreveryfre location, from bottom to top, wemustchoose the key-valuetolead the search<br />Ifcurrentnodehasn’t ‘nephews’ w...
Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi...
Index 2, has ‘nephews’, we take the last right on of the leftchildofnode<br />Alsofor position 1, root<br />Nioi Pier Giul...
Wehavefinallybuilt the balancedbinarysearchtree, savingtime and memorywith the useof 2 formulasforsearching<br />Youmaywan...
Done.<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br ...
Upcoming SlideShare
Loading in...5
×

Balanced binary search tree on array

3,317

Published on

Published in: Education
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
3,317
On Slideshare
0
From Embeds
0
Number of Embeds
2
Actions
Shares
0
Downloads
23
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Balanced binary search tree on array

  1. 1. Array implementationof a balanced binary search tree<br />
  2. 2. Given a set oforderedelements, weaskourselvesifitispossibletobuild a binarysearchtreeupontheseelements in a sequential data structurelikeanarray; the answer, ofcourse, is yes. And in a bottom-up way.<br />Theserepresentationdoesnotmakeuseofpointers, typical in a linkedrepresentationof a binarysearchtree, thereforeyousaveO(2n) memorylocations.<br />Our BST willsave the orderedelementsonly at leafnodes, and internalnodeswillbevalorizedwithkey-valuesthatwill guide everysearchoperation.<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  3. 3. Given the set<br />containing5 elements, the balancedbinarysearchtreeoftheseelementsusuallyisrepresented in a ‘linked’ way likethis:<br />3<br />2<br />4<br />1<br />3<br />4<br />5<br />1<br />2<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  4. 4. 3<br />lvl 0<br />Treeismadeof 3 levels<br />(withroot at level 0)<br />Ithas5 ‘leaves’ and 4 ‘nodes’<br />Formally, ifnis the numberof<br />elements, wewillhavenleaves<br /> and n-1 internalnodes<br />Itis a complete and balancedtree<br />The numberofleavesisbetween2i e 2i+1, wherei and i+1 are the levelswhere the leaves are stored<br />In this case wehave22&lt; 5 &lt; 23, 4 &lt; 5 &lt; 8<br />2<br />4<br />lvl 1<br />1<br />lvl 2<br />3<br />4<br />5<br />1<br />2<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />lvl 3<br />
  5. 5. Torepresentthattreeweuse the samerepresentationusedfor a similar data structure, usuallycalledheap(maxo min)<br />A node at position i on anarray, willhavehisleftchild at position 2*i and his right child at position 2*i +1, and everychildnode in the arraywillhavehisownfather at position floor(i/2)<br />Ourbinarytree on arraywillhave(n-1)+n+1 memorylocations, <br />(internalnodes)+ leaves+1<br />Final+1isabout the optiontoleaveempty<br /> the first location of the array<br />3<br />2<br />4<br />1<br />3<br />4<br />5<br />1<br />2<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  6. 6. Ifwealreadyhave a linkedrepresentationof the treebased on oursortedelements, itis possibile to transfer it in a arrayrepresentationsimplytraversing the levelsof the tree, keeping in mind previousformulasaboutchild and fatherspositions.<br />Likethis<br />the roothas position 1 and hischildren<br />and are stored at2*1=2 and <br />2*1+1=3 .<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />3<br />2<br />4<br />1<br />3<br />4<br />5<br />1<br />2<br />3<br />3<br />4<br />2<br />2<br />4<br />1<br />3<br />4<br />5<br />1<br />2<br />
  7. 7. Buthow do webuildthistree in a bottom-up way ifwe don’t have the linkedrepresentationof the tree, butonlyourelementssorted(usualli in a ascendingorder) ?<br />Wedefinitelymust produce the sameresultof a treetraversingbylevels, seenbefore, so weneed the same number of memory locations like the array resulting the ‘transformation’<br />It is quite simple but we must note that our input array, with our elements, is in the final array but in a ‘strange’ way.<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  8. 8. Note the elements(1 2 3 4 5) order:<br />The leaves(redones), aresstored in a ascendingorderbut, let’s say, in a ‘broken’ sequence,<br /> 3 4 5 1 2 insteadof 1 2 3 4 5<br />Thisis the directconsequenceof the treelevels<br />Wemustfind out whatelementsfromour input arraywemustmove in anotherpositions<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />3<br />2<br />4<br />1<br />3<br />4<br />5<br />1<br />2<br />
  9. 9. Weuse the basictheoryofbinarytrees and it’s link with the powerof the number 2 anwewilluse the toollog2(log base 2)<br />Wesaidthatleaves are storedbetween the level 2i and 2i+1”,, they are practically spread at most in twodifferentand sequentiallevels, i and i+1<br />Wehavetofind out whatvaluesofourelementmusto go at level i and the otherseventuallywill go on the next level, i+1<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  10. 10. In ourexamplewehavethat 22&lt; 5 &lt; 23 , <br />4 &lt; 5 &lt; 8<br />In thisimagewe note thatonly ONE nodeoflevel i ( in this case i=2) is a internalnodeoflevel i ( the other are onlyleaves in red) and thisnodebringstwoleaves in the level i+1<br />Wehavetofindthesnodes, thatbringscertainelementstobe on level i+1: ifwesubtract 4 from 5, weobrain the numberofnodethatwe are lookingfor, in this case 1 nodethatbrings 2 elementstobeleaves in level i+1<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />3<br />2<br />4<br />1<br />3<br />4<br />5<br />1<br />2<br />
  11. 11. This 2 ( is the resultof 1*2, [numer_of_node_we_were_looking_for]*2 ), <br />represent the numberofelementtomovefrom head to the tailofour input vector<br />Doingthisweobtain the right orderwewerelookingfor, like in the level-traversalrepresentationshownbefore.<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />3<br />2<br />4<br />1<br />3<br />4<br />5<br />1<br />2<br />
  12. 12. Firtswemustunderstandhowtoobtainthat4 ofourexamplewithoutanyknowledgeabout the numberoflevelthere are in our future tree<br />It’s timetouse some mathematics : log2 <br />WE onlyknow the numberofelementsthatwillbeourleaves, in the example are 5 (formallyn)<br />Using log andfloor() wefound the valuethatwe are lookingforof the level i.<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  13. 13. log2 5 = 2.3219...<br />floor(log2 5) = 2<br />Nowwehave the numberofleveli <br />Toknowhowmanynodes are usuallystored at level i in a full tree, eweusepowersof 2<br />nodeNumber=2floor(log2 n) = 2floor(log2 5) = 22 = 4<br />And therewehaveour 4! Therefore ,5 – 4 =1<br /> and 1*2= 2; wemustmove 2 elementsfrom head totail<br />Let’s now complete our BST<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  14. 14. Let’s create anarrayoflenght (internalnodes)+(leaves)+1, in our case 10 ( 4 + 5 +1) , and weourvectorwithmovedelements<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  15. 15. Foreveryfre location, from bottom to top, wemustchoose the key-valuetolead the search<br />Ifcurrentnodehasn’t ‘nephews’ we take the valueofhisleftchild<br />Otherwisewe look for the last ‘nephew’ on the right from the leftchild<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  16. 16. Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />Index 4, hasonlychildren take the leftvalue(2*i)<br />Alsoforindex 3(at position 6 = 2*i, i=3).<br />
  17. 17. Index 2, has ‘nephews’, we take the last right on of the leftchildofnode<br />Alsofor position 1, root<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  18. 18. Wehavefinallybuilt the balancedbinarysearchtree, savingtime and memorywith the useof 2 formulasforsearching<br />Youmaywanttocheckifyou are already or you are goingtofind a value out ofbounds, topreventeruntimeerrors<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />3<br />2<br />4<br />3<br />2<br />4<br />1<br />3<br />4<br />5<br />1<br />2<br />1<br />3<br />4<br />5<br />1<br />2<br />
  19. 19. Done.<br />Nioi Pier Giuliano <br />Università degli Studi di Cagliari<br />Corso di Laurea in Tecnologie Informatiche<br />Algoritmi e Strutture Dati 2<br />
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×