• Non ci sono risultati.

node *insert(node *root, int key

N/A
N/A
Protected

Academic year: 2021

Condividi "node *insert(node *root, int key"

Copied!
2
0
0

Testo completo

(1)

#include <stdlib.h>

#include <stdio.h>

#define max(x, y) ((x) > (y) ? (x) : (y)) typedef struct _node {

int key;

struct _node *left;

struct _node *right;

} node;

// Inserimento in un albero binario non bilanciato.

node *insert(node *root, int key) { if (root == NULL) {

node *new = malloc(sizeof(node));

new->key = key;

new->left = NULL;

new->right = NULL;

return new;

}

if (key <= root->key) {

root->left = insert(root->left, key);

} else {

root->right = insert(root->right, key);

}

return root;

}

// Cerca la chiave key all'interno dell'albero e ne restituisce la profondità int search(const node *root, int key) {

if (root == NULL) { return -1;

}

if (root->key == key) { return 0;

}

int res;

if (key <= root->key) {

res = search(root->left, key);

} else {

res = search(root->right, key);

}

// Se la chiave non è stata trovata nei sottoalberi, dobbiamo propagare -1 // altrimenti dobbiamo incrementare la profondità di uno.

return res == -1 ? -1 : 1 + res;

}

int main(int argc, char *argv[]) { int k, n, d;

node *root = NULL;

(2)

scanf("%d", &n);

for (size_t i = 0; i < n; i++) { scanf("%d", &k);

root = insert(root, k);

}

while (1) {

scanf("%d", &k);

if (k < 0) { break;

}

d = search(root, k);

if (d == -1) { printf("NO\n");

} else {

printf("%d\n", d);

} }

return 0;

}

Riferimenti

Documenti correlati

The typical anatomy will show the marginal vessels from the right and left transverse colon forming the middle colic vein and joining the right gastroepiploic vein to become

The right template includes the interaortocaval lymph nodes, preoartic tissue between the left renal vein and the inferior mesenteric artery, precaval tissue, and all the

In stage II disease, retroperitoneal lymph node dissection (RPLND) can be performed as a first line of treatment or after chemotherapy; the latter approach confers a staging

g Blood smear of a case of plasma cell leukemia with CD138 positive cells.. 6.4.4 Natural Killer (NK)-Cell Neoplasias These diseases, which occur as extranodal tumors or leukemias

Kitagawa Y, Fujii H, Mukai M, et al: Intraoperative lymphatic mapping and sentinel lymph node sampling in esophageal and gastric cancer. Kitagawa Y, Fujii H, Mukai M, et al: The

&#34;porous&#34; to small particle radiocolloid than other lymph nodes and in our breast cancer patients it is routine to see a string of second tier internal mammary nodes

Open and laparoscopic pelvic lymph node dissection can be compared on several points such as operation time, intraoperative blood loss, economic advantage, complications and

When dealing with the SAN, it is important to understand its ultrastructure. As an action potential from the sinoatrial node, nodal myocytes are seen to potentiate electrical