This repository was archived by the owner on Oct 8, 2021. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathBSTrees.h
More file actions
151 lines (121 loc) · 3.9 KB
/
Copy pathBSTrees.h
File metadata and controls
151 lines (121 loc) · 3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int key;
char *s;
struct node *left, *right;
};
//................................................................................................................................
//CREAZIONE DI UN NODO
struct node *new_node (int data, char *v)
{
struct node *n = (struct node *) malloc (sizeof (struct node));
n->s = v; //s punta alla stessa locazione di v, ossia al primo carattere della stringa
n->key = data;
n->left = n->right = NULL;
return n;
}
//................................................................................................................................
//PROCEDURA PER LA STAMPA
void show(struct node *root)
{
if (root == NULL)
{
printf ("NULL ");
}
else
{
printf ("%d:", root->key); //stampa della chiave e della stringa del nodo, es: 5 lorenzo
for (int i = 0; root->s[i] != '\0'; i++)
{
printf ("%c", root->s[i]);
}
printf (" ");
//chiamata di stampa sul figlio sx e dx
show(root->left);
show(root->right);
}
}
//................................................................................................................................
//PROCEDURA PER L' INSERIMENTO
struct node *insert (struct node *node, int key, char *v)
{
if (node == NULL){
return new_node (key, v);
}
if (key < node->key){
node->left = insert (node->left, key, v);
}
else if (key > node->key){
node->right = insert (node->right, key, v);
}
return node;
}
//.................................................................................................................................
//PROCEDURA PER L'ELIMINAZIONE DELL' ALBERO E LIBERAZIONE SPAZIO DI MEMORIA OCCUPATA
void destroyTree(struct node *root){
if(root == NULL){
return;
}else{
destroyTree(root->right);
destroyTree(root->left);
free(root);
}
}
//................................................................................................................................
//PROCEDURA DI RICERCA
struct node* find(struct node *root, int k)
{
if(root==NULL || root->key==k){
return root;
}
else if(k>root->key){
return find(root->right, k);
}
else{
return find(root->left, k);
}
}
//......................................................................................................................................
/*
Per inserimento: insert 5 str
Per terminazione: exit 5 str
*/
/*
int main ()
{
char op[20]; //stringa contenente l'operazione che voglio eseguire: insert find delete
// -memorizzo le stringhe dei nodi in un array v.
// -per come C( dimensionato possiamo al massimo inserire 100 nodi(dato che puC2 contenere al max 100 stringhe)
// e ogni nodo puC2 contenere un stringa di al massimo 100 caratteri
char v[100][100];
int i = 0;
int k;
struct node *root = NULL;
//printf ("operazione da eseguire: \n");
scanf("%s", op); //ricavo l'operazione che voglio eseguire da stdin
while (strcmp (op, "exit") != 0)
{
//inserimento
if(strcmp(op, "insert")==0){
scanf("%i", &k);
scanf("%s", v[i]); //se faccio un insert ricavo la stringa da passare al nodo da stdin
root=insert(root, k, v[i]);
//ricerca
}else if(strcmp(op, "find")==0){
scanf("%i", &k);
printf("%s \n",find(root,k)->s); //se voglio eseguire una ricerca cerco il nodo con chiave k e stampo la sua stringa
}else{
show(root);
printf("\n");
}
//printf ("operazione da eseguire: \n");
scanf ("%s", op);
i++;
}
destroyTree(root); //deallocazione dell'albero
return 0;
}
*/