-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathARV - Pesquisa em árvores.cpp
94 lines (77 loc) · 1.39 KB
/
ARV - Pesquisa em árvores.cpp
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
#include <iostream>
using namespace std;
//Struct Arvore Binaria
struct treenode
{
int info;
treenode *esq;
treenode *dir;
};
typedef treenode* treenodeptr;
//Funcao insere
void tInsere(treenodeptr &p, int x)
{
if (p == NULL)
{
p = new treenode;
p->info = x;
p->esq = NULL;
p->dir = NULL;
}
else if (x < p->info)
tInsere(p->esq, x);
else
tInsere(p->dir, x);
}
//Funcao Pesquisa
treenodeptr tPesq(treenodeptr p, int x)
{
if (p == NULL)
return NULL;
else if (x == p->info)
return p;
else if(x < p->info)
return tPesq(p->esq, x);
else
return tPesq(p->dir, x);
}
//Destruir Arvore
void tDestruir (treenodeptr &arvore)
{
if (arvore != NULL)
{
tDestruir(arvore->esq);
tDestruir(arvore->dir);
delete arvore;
}
arvore = NULL;
}
//Funcao main
int main()
{
//Variaveis
treenodeptr p = NULL;
treenodeptr tree = NULL;
int n;
int input;
int x;
//Valor a ser inserido na arvore
cin >> n;
//Inserindo valor em arvore
for(int i = 0; i < n; i++)
{
cin >> input;
tInsere(tree, input);
}
//Valor a ser pesquisado na arvore
cin >> x;
p = tPesq(tree, x);
//Impressao de Dados
if(p == NULL)
cout << "Elemento nao encontrado" << endl;
else
cout << "Encontrado" << endl;
//Destruindo Arvore
tDestruir(tree);
return 0;
}