Balanced binary search tree on array

19
Nioi Pier Giuliano Università degli Studi di Cagliari Corso di Laurea in Tecnologie Informatiche Algoritmi e Strutture Dati 2 Array implementation of a balanced binary search tree

description

 

Transcript of Balanced binary search tree on array

Page 1: Balanced binary search tree on array

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

Array implementation of a balanced binary search tree

Page 2: Balanced binary search tree on array

• Given a set of ordered elements, we ask ourselves if it is possible to build a binary search tree upon these elements in a sequential data structure like an array; the answer, of course, is yes. And in a bottom-up way.

• These representation does not make use of pointers, typical in a linked representation of a binary search tree, therefore you save O(2n) memory locations.

• Our BST will save the ordered elements only at leaf nodes, and internal nodes will be valorized with key-values that will guide every search operation.

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

Page 3: Balanced binary search tree on array

• Given the set

containing 5 elements, the balanced binary search tree of these elements usually is represented in a ‘linked’ way like this:

1 2 3 4 5

3

2 4

1

1 2

3 4 5

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

Page 4: Balanced binary search tree on array

• Tree is made of 3 levels(with root at level 0)

• It has 5 ‘leaves’ and 4 ‘nodes’• Formally, if n is the number of elements, we will have n leaves and n-1 internal nodes• It is a complete and balanced tree• The number of leaves is between 2i e 2i+1,

where i and i+1 are the levels where the leaves are stored

• In this case we have 22 < 5 < 23, 4 < 5 < 8

3

2 4

1

1 2

3 4 5

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

lvl 0

lvl 1

lvl 2

lvl 3

Page 5: Balanced binary search tree on array

• To represent that tree we use the same representation used for a similar data structure, usually called heap(max o min)

• A node at position i on an array, will have his left child at position 2*i and his right child at position 2*i +1, and every child node in the array will have his own father at position floor(i/2)

• Our binary tree on array will have (n-1)+n+1 memory locations, (internal nodes)+ leaves+1

• Final +1 is about the option to leave empty the first location of the array

3

2 4

1

1 2

3 4 5Nioi Pier Giuliano

Università degli Studi di CagliariCorso di Laurea in Tecnologie Informatiche

Algoritmi e Strutture Dati 2

Page 6: Balanced binary search tree on array

• If we already have a linked representation of the tree based on our sorted elements, it is possibile to transfer it in a array representation simply traversing the levels of the tree, keeping in mind previous formulas about child and fathers positions.

• Like this

the root has position 1 and his children and are stored at 2*1=2 and

2*1+1=3 .

3

2 4

1

1 2

3 4 5Nioi Pier Giuliano

Università degli Studi di CagliariCorso di Laurea in Tecnologie Informatiche

Algoritmi e Strutture Dati 2

3 2 4 1 3 4 5 1 2

3

2 4

Page 7: Balanced binary search tree on array

• But how do we build this tree in a bottom-up way if we don’t have the linked representation of the tree, but only our elements sorted(usualli in a ascending order) ?

• We definitely must produce the same result of a tree traversing by levels, seen before, so we need the same number of memory locations like the array resulting the ‘transformation’

• 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.

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

Page 8: Balanced binary search tree on array

• Note the elements(1 2 3 4 5) order:

• The leaves(red ones), ares stored in a ascending order but, let’s say, in a ‘broken’ sequence,

3 4 5 1 2 instead of 1 2 3 4 5• This is the direct consequence of the tree levels• We must find out what elements from our input

array we must move in another positionsNioi Pier Giuliano

Università degli Studi di CagliariCorso di Laurea in Tecnologie Informatiche

Algoritmi e Strutture Dati 2

1 2 3 4 5

3 2 4 1 3 4 5 1 2

Page 9: Balanced binary search tree on array

• We use the basic theory of binary trees and it’s link with the power of the number 2 an we will use the tool log2 (log base 2)

• We said that leaves are stored between the level 2i and 2i+1”, , they are practically spread at most in two different and sequential levels, i and i+1

• We have to find out what values of our element musto go at level i and the others eventually will go on the next level, i+1

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

Page 10: Balanced binary search tree on array

• In our example we have that 22 < 5 < 23 , 4 < 5 < 8

• In this image we note that only ONE node of level i ( in this case i=2) is a internal node of level i ( the other are only leaves in red) and this node brings two leaves in the level i+1

• We have to find thes nodes, that brings certain elements to be on level i+1: if we subtract 4 from 5, we obrain the number of node that we are looking for, in this case 1 node that brings 2 elements to be leaves in level i+1

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

3

2 4

1

1 2

3 4 5

Page 11: Balanced binary search tree on array

• This 2 ( is the result of 1*2, [numer_of_node_we_were_looking_for]*2 ), represent the number of element to move from head to the tail of our input vector

• Doing this we obtain the right order we were looking for, like in the level-traversal representation shown before.

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

1 2 3 4 5

3 4 5 1 2

3 2 4 1 3 4 5 1 2

Page 12: Balanced binary search tree on array

• Firts we must understand how to obtain that 4 of our example without any knowledge about the number of level there are in our future tree

• It’s time to use some mathematics : log2

• WE only know the number of elements that will be our leaves, in the example are 5 (formally n)

• Using log and floor() we found the value that we are looking for of the level i.

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

Page 13: Balanced binary search tree on array

• log2 5 = 2.3219...

• floor(log2 5) = 2• Now we have the number of level i • To know how many nodes are usually stored at

level i in a full tree, ewe use powers of 2nodeNumber= 2floor(log2 n) = 2floor(log2 5) = 22 = 4

• And there we have our 4! Therefore ,5 – 4 =1 and 1*2= 2; we must move 2 elements from

head to tail• Let’s now complete our BST

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

Page 14: Balanced binary search tree on array

• Let’s create an array of lenght (internal nodes)+(leaves)+1, in our case 10 ( 4 + 5 +1) , and we our vector with moved elements

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

1 2 3 4 5

3 4 5 1 2

--- 3 4 5 1 2

0 1 2 3 4 5 6 7 8 9

Page 15: Balanced binary search tree on array

• For every fre location, from bottom to top, we must choose the key-value to lead the search

• If current node hasn’t ‘nephews’ we take the value of his left child

• Otherwise we look for the last ‘nephew’ on the right from the left child

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

--- 3 4 5 1 2

0 1 2 3 4 5 6 7 8 9

Page 16: Balanced binary search tree on array

• Index 4, has only children take the left value(2*i)

• Also for index 3(at position 6 = 2*i, i=3).

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

--- 1 3 4 5 1 2

0 1 2 3 4 5 6 7 8 9

--- 4 1 3 4 5 1 2

0 1 2 3 4 5 6 7 8 9

Page 17: Balanced binary search tree on array

• Index 2, has ‘nephews’, we take the last right on of the left child of node

• Also for position 1, root

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

--- 2 4 1 3 4 5 1 2

0 1 2 3 4 5 6 7 8 9

--- 3 2 4 1 3 4 5 1 2

0 1 2 3 4 5 6 7 8 9

Page 18: Balanced binary search tree on array

• We have finally built the balanced binary search tree, saving time and memory with the use of 2 formulas for searching

• You may want to check if you are already or you are going to find a value out of bounds, to prevente runtime errors

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2

--- 3 2 4 1 3 4 5 1 2

0 1 2 3 4 5 6 7 8 9

3 2 4 1 3 4 5 1 2 3

2 4

1

1 2

3 4 5

Page 19: Balanced binary search tree on array

Done.

Nioi Pier Giuliano Università degli Studi di Cagliari

Corso di Laurea in Tecnologie InformaticheAlgoritmi e Strutture Dati 2