Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
chiavi distinte! x
Left(x) Right(x)
< >
A.A. 2019/20 12 I BST 2
ADT di I classe BST
BST.h
typedef struct binarysearchtree *BST;
BST BSTinit() ;
void BSTfree(BST bst);
int BSTcount(BST bst);
int BSTempty(BST bst);
Item BSTsearch(BST bst, Key k);
void BSTinsert_leafI(BST bst, Item x);
void BSTinsert_leafR(BST bst, Item x);
void BSTinsert_root(BST bst, Item x);
Item BSTmin(BST bst);
Item BSTmax(BST bst);
void BSTvisit(BST bst, int strategy);
BST.c
#include <stdlib.h>
#include "Item.h"
#include "BST.h"
6 18
3 7 17 20
2 4
Sono visualizzate solo le
chiavi intere, non gli item
6 18
3 7 17 20
2 4
3 7 17 20
2 4
9 63
11 84
9 63
11 84
15
p
9 h 63
11 84
15
9 63
p
11 84
h
15
9 63
11 84
p
15 h
62
A.A. 2019/20 12 I BST 20
Sort
Attraversamento in-ordine: ordinamento
crescente delle chiavi.
15 2 3 4 6 7 9 13 15 17 18 20
if (strategy == PREORDER)
ITEMstore(h->item);
treePrintR(h->l, z, strategy);
if (strategy == INORDER)
ITEMstore(h->item);
treePrintR(h->r, z, strategy);
if (strategy == POSTORDER)
ITEMstore(h->item);
}
10 5
x->l h->r = x->l
link rotL(link h) {
link x = h->r;
h->r = x->l;
x->l = h;
return x;
}
BST BSTinit();
void BSTfree(BST bst);
int BSTcount(BST bst);
int BSTempty(BST bst);
Item BSTmin(BST bst);
Item BSTmax(BST bst);
void BSTinsert_leafI(BST bst, Item x);
void BSTinsert_leafR(BST bst, Item x);
void BSTinsert_root(BST bst, Item x);
Item BSTsearch(BST bst, Key k);
void BSTdelete(BST bst, Key k); Order-Statistic BST
Item BSTselect(BST bst, int r);
void BSTvisit(BST bst, int strategy);
Item BSTsucc(BST bst, Key k);
Item BSTpred(BST bst, Key k);
void BSTbalance(BST bst);
A.A. 2019/20 12 I BST 36
p
BSTnode item name value
item N
l r
BST.c chiave
#include <stdlib.h>
#include "Item.h"
#include "BST.h" puntatore al padre
dimensione sottoalbero
typedef struct BSTnode* link;
struct BSTnode {Item item;link p;link l;link r;int N;};
struct binarysearchtree { link root; link z; };
h
A.A. 2019/20 successore 12 I BST 39
Esempio
k = 15
15
6 18
3 7 17 20
successore
2 4 13
9
A.A. 2019/20 12 I BST 40
15
successore 6 18
3 7 17 20
2 4 13
k=4
9
A.A. 2019/20 12 I BST 41
Item BSTsucc(BST bst, Key k) {
return searchSucc(bst->root, k, bst->z);
}
Item searchSucc(link h, Key k, link z) {
link p;
if (h == z)
return ITEMsetNull();
if (KEYcmp(k, KEYget(h->item))==0) {
if (h->r != z)
return minR(h->r, z);
else {
p = h->p;
while (p != z && h == p->r) {
h = p;
p = p->p;
}
return p->item;
}
}
if (KEYcmp(k, KEYget(h->item))==-1)
return searchSucc(h->l, k, z);
return searchSucc(h->r, k, z);
}
6 18
3 7 17 20
2 4 13 predecessore
9
A.A. 2019/20 12 I BST 44
15
6 18
predecessore
3 7 17 20
2 4 13
9 k=9
A.A. 2019/20 12 I BST 45
Item BSTpred(BST bst, Key k) {
return searchPred(bst->root, k, bst->z);
}
Item searchPred(link h, Key k, link z) {
link p;
if (h == z)
return ITEMsetNull();
if (KEYcmp(k, KEYget(h->item))==0) {
if (h->l != z) return maxR(h->l, z);
else {
p = h->p;
while (p != z && h == p->l) {
h = p;
p = p->p;
}
return p->item;
}
}
if (KEYcmp(k, KEYget(h->item))==-1)
return searchPred(h->l, k, z);
return searchPred(h->r, k, z);
}
33 73 171 201
21 41 132
91
A.A. 2019/20 12 I BST 50
r=4
1511 t=3
3<4 scendo a DX
cerco r=4-3-1=0
67 183
33 73 171 201
21 41 132
91
A.A. 2019/20 12 I BST 51
r=0
1511 t=0
return 7
67 183
33 73 171 201
21 41 132
91
A.A. 2019/20 12 I BST 52
Item selectR(link h, int r, link z) {
int t;
if (h == z)
return ITEMsetNull();
t = h->l->N;
if (t > r)
return selectR(h->l, r, z);
if (t < r)
return selectR(h->r, r-t-1, z);
return h->item;
}
h
x = h->l x->r = h
x->r h->l = x->r
h x->l = h
x = h->r
x->l h->r = x->l
09
188
45 232
21 173 191
72
121
11
4 17
23
2 7 13
6 8 12 15
23
2 7 13
6 8 12 15
succ(11)
A.A. 2019/20 12 I BST 69
partition rispetto a 12
4 12
17
2 7
6 8 13 23
15
A.A. 2019/20 12 I BST 70
ricostruzione dell’albero con radice 12
12
4 17
2 7 13 23
6 8
15
0 delete 11 0
18 18
4 23 4 23
2 17 2 17
7 7
6 8 6 8
13 13
11
A.A. 2019/20 12 I BST 72
delete 7
0 0
18 18
4 23 4 23
2 17 2 17
7
6 8 6 8
13 13
0 0
18 18
4 23 23
2 17 2 17
8 8
6 13 6 13
}
free(y); aggiornamento
return h; puntatore
} al padre
void BSTdelete(BST bst, Key k) {
bst->root= deleteR(bst->root, k, bst->z);
}
A.A. 2019/20 12 I BST 78
Bilanciamento
i’ i’ i’ i’
caso b caso c
i i’ i’ i
[17, 19]
[16, 21]
[15, 23]
[8, 9]
[6, 10]
[0, 3] [5, 8]
0 5 10 15 20 25 30
[16, 21]
30
< in ricerca
A.A. 2019/20 12 I BST 94
Item.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "Item.h"
Item ITEMscan() {
Item val;
printf("low = "); scanf("%d", &val.low);
printf("high = "); scanf("%d", &val.high);
return val;
}
Item ITEMsetNull() {
Item val = {-1, -1};
return val;
}
nuove funzioni
funzioni modificate
IBST.c
#include <stdlib.h>
#include <stdio.h>
#include "Item.h" max: massimo high
#include "IBST.h“ del sottoalbero
typedef struct IBSTnode *link;
2 5
1
3 1,3
1,4
0 2,3
4 4,5
A.A. 2019/20 12 I BST 114
Applicazione al CAD elettronico: verificare se
le piste si intersecano in un circuito
elettronico.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 117
inserisci intervallo
ordinate del rettangolo 1
no intersezioni.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 118
inserisci intervallo
ordinate del rettangolo 2
no intersezioni.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 119
inserisci intervallo
ordinate del rettangolo 3
intersezioni con 1 e 2.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 120
rimuovi intervallo ordinate
del rettangolo 3.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 121
rimuovi intervallo ordinate
del rettangolo 0.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 122
rimuovi intervallo ordinate
del rettangolo 2.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 123
inserisci intervallo
ordinate del rettangolo 5
no intersezioni..
2 5
1
3
0
4
A.A. 2019/20 12 I BST 124
inserisci intervallo
ordinate del rettangolo 4
intersezioni con 1 e 5.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 125
rimuovi intervallo ordinate
del rettangolo 4.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 126
rimuovi intervallo ordinate
del rettangolo 1.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 127
rimuovi intervallo ordinate
del rettangolo 5.
2 5
1
3
0
4
A.A. 2019/20 12 I BST 128
Analisi