1. 05/09/2016 14:17:36C:UserselainDocumentsAlgoritmosÁrvorearvore_2.pas
1: {
2: ELAINE CECÍLIA GATTO
3: 05-09-2016
4: ÁRVORES
5: }
6:
7: program arvore_2;
8: uses wincrt, crt;
9:
10: //DEFININDO O TIPO ÁRVORE BINÁRIA
11: //É COMO UMA LISTA ENCADEADA!
12: type
13: elemento = char; //elemento da árvore
14: arvB = ^no; //ponteiro
15: //NÓ da árvore é um REGISTRO
16: no = record
17: esquerda : arvB; //folha esquerda
18: objeto : elemento; //elemento da folha
19: direita : arvB; //folha direita
20: end;
21:
22: var
23: t,a : arvB;
24: opcao : integer;
25: x : char;
26:
27: //INICIALIZANDO ÁRVORE
28: procedure inicializa(var t:arvB);
29: begin
30: writeln(' Inicializando arvore ');
31: t := nil;
32: writeln(' Arvore inicializada');
33: end;
34:
35: //INSERINDO UM ELEMENTO NA ÁRVORE BINÁRIA
36: procedure inserir(var t:arvB; x:elemento);
37: begin
38: //A ÁRVORE ESTÁ VAZIA, portanto, o primeiro elemento inserido será a raíz
39: if(t=nil)then
40: begin
41: new(t);
42: t^.objeto := x;
43: t^.esquerda := nil;
44: t^.direita := nil;
45: writeln(' Elemento ( ' , t^.objeto, ' ) inserido na raiz! ');
46: end
47: else
48: //A ÁRVORE NÃO ESTÁ VAZIA. OS ELEMENTOS SEGUINTES DEVERÃO RESPEITAR A
49: //SEGUINTE REGRA: - MENOR QUE A RAIZ, VAI PARA A ESQUERDA
50: // - MAIOR QUE A RAIZ, VAI PARA A DIREITA
51: begin
52: if(x<t^.objeto)then
53: begin
54: inserir(t^.esquerda, x);
55: writeln(' Elemento ( ' , t^.objeto, ' ) inserido na folha esquerda!
');
56: end
57: else
58: begin
59: inserir(t^.direita, x);
60: writeln(' Elemento ( ' , t^.objeto , ' ) inserido na folha direita!
');
61: end;
62: end;
63: end;
64:
1/5
2. 05/09/2016 14:17:36C:UserselainDocumentsAlgoritmosÁrvorearvore_2.pas
65: //BUSCAR UM ELEMENTO NA ÁRVORE BINÁRIA
66: function buscar(t:arvB; x:elemento):arvB;
67: begin
68: //se a árvore estiver vazia - não há o que procurar
69: if(t=nil)then
70: begin
71: buscar:=nil;
72: writeln(' Elemento não encontrado ');
73: end
74: else
75: begin
76: //o elemento está na raiz
77: if(x=t^.objeto)then
78: begin
79: buscar:=t;
80: end
81: else
82: begin
83: //busca o elemento nas subárvores
84: if(x<t^.objeto)then
85: begin
86: buscar := buscar(t^.esquerda,x);
87: end
88: else
89: begin
90: buscar := buscar(t^.direita,x);
91: end;
92: end;
93: end;
94: end;
95:
96: //ENCONTRANDO O MAIOR ELEMENTO NA ARVORE
97: function maiorElemento(var T:arvB):arvB;
98: begin
99: if(t^.direita = nil)then
100: begin
101: //procura o no que armazena o seu maior elemento e o desliga da arvore
102: maiorElemento := t;
103: t:=t^.esquerda;
104: end
105: else
106: begin
107: //retorna o endereço do maior elemento
108: maiorElemento := maiorElemento(t^.direita);
109: end;
110: end;
111:
112: //REMOÇÃO
113: procedure remover(var t:arvB; x:elemento);
114: var
115: p : arvB;
116: begin
117: //A ARVORE ESTÁ VAZIA
118: if(t=nil)then
119: begin
120: writeln(' Elemento não encontrado ');
121: end;
122:
123: //A ARVORE NÃO ESTÁ VAZIA
124: if(x=t^.objeto)then
125: begin
126: p :=t ;
127: //a raiz não tem filhos a esquerda
128: if(t^.esquerda=nil)then
129: begin
130: t:=t^.direita;
2/5
3. 05/09/2016 14:17:36C:UserselainDocumentsAlgoritmosÁrvorearvore_2.pas
131: writeln(' a raiz nao tem filhos a esquerda ( ', t^.objeto , ' ) ' );
132: end
133: else
134: //a raiz não tem filhos a direita
135: if(t^.direita=nil)then
136: begin
137: t:=t^.esquerda;
138: writeln(' a raiz nao tem filhos a direita ( ', t^.objeto , ' ) ' );
139: end
140: else
141: begin
142: //a raiz não tem filhos
143: p := maiorElemento(t^.esquerda);
144: t^.objeto := p^.objeto;
145: writeln(' a raiz nao tem filhos ( ' , t^.objeto , ' ) ' );
146: end;
147: dispose(p); //deslocando P
148: end
149: else
150: if(x<t^.objeto)then
151: begin
152: //remove da subarvore esquerda
153: remover(t^.esquerda, x);
154: writeln(' Elemento ( ' , t^.objeto , ' ) removido da subarvore
esquerda ');
155: end
156: else
157: begin
158: //remove da subarvore a direita
159: remover(t^.direita, x);
160: writeln(' Elemento ( ' , t^.objeto , ' ) removido da subarvore
direita ');
161: end;
162: end;
163:
164: //em ordem
165: procedure emOrdem(t:arvB);
166: begin
167: if(t<>nil)then
168: begin
169: emOrdem(t^.esquerda);
170: writeln(' ', t^.objeto);
171: emOrdem(t^.direita);
172: end;
173: end;
174:
175: procedure preOrdem(t:arvB);
176: begin
177: if(t<>nil)then
178: begin
179: writeln(' ', t^.objeto);
180: preOrdem(t^.esquerda);
181: preOrdem(t^.direita);
182: end;
183: end;
184:
185: procedure posOrdem(t:arvB);
186: begin
187: if(t<>nil)then
188: begin
189: posOrdem(t^.esquerda);
190: posOrdem(t^.direita);
191: writeln(' ', t^.objeto);
192: end;
193: end;
194:
3/5
4. 05/09/2016 14:17:36C:UserselainDocumentsAlgoritmosÁrvorearvore_2.pas
195: //PROGRAMA PRINCIPAL
196: procedure menu;
197: begin
198: repeat
199: writeln ((' |
==================================================================== |');
200: writeln ((' | MENU - ARVORE
| ');
201: writeln ((' |
==================================================================== |');
202: writeln ((' | 1. Inicializar a Arvore
| ');
203: writeln ((' | 2. Inserir na Arvore
| ');
204: writeln ((' | 3. Buscar um elemento
| ');
205: writeln ((' | 4. Buscar o maior elemento
| ');
206: writeln ((' | 5. Remover um elemento
| ');
207: writeln ((' | 6. Atravessa a arvore em ordem
| ');
208: writeln ((' | 7. Atravessa a arvore em pre ordem
| ');
209: writeln ((' | 8. Atravessa a arvore em pos ordem
| ');
210: writeln ((' | 0. SAIR
| ');
211: writeln ((' |
==================================================================== |');
212: writeln ('');
213: write(' Digite a opcao desejada: ');
214: readln(opcao);
215: writeln('');
216:
217: Case opcao Of
218: 1 : Begin
219: inicializa(t);
220: writeln('');
221: end;
222: 2 : Begin
223: write(' Digite um caracter: ');
224: readln(x);
225: writeln('');
226: inserir(t,x);
227: writeln('');
228: end;
229: 3 : Begin
230: write(' Digite um caracter: ');
231: readln(x);
232: writeln('');
233: a := buscar(t,x);
234: writeln('Retornou da funcao: ', a^.objeto);
235: writeln('');
236: end;
237: 4 : Begin
238: a:=maiorElemento(t);
239: writeln('Retornou da funcao: ', a^.objeto);
240: writeln('');
241: end;
242: 5 : Begin
243: write(' Digite um caracter: ');
244: readln(x);
245: remover(t,x);
246: writeln('');
247: end;
4/5
5. 05/09/2016 14:17:36C:UserselainDocumentsAlgoritmosÁrvorearvore_2.pas
248: 6 : Begin
249: writeln(' Atravessando a Arvore em Ordem ');
250: emOrdem(t);
251: writeln('');
252: end;
253: 7 : Begin
254: writeln(' Atravessando a Arvore em Pre Ordem ');
255: preOrdem(t);
256: writeln('');
257: end;
258: 8 : Begin
259: writeln(' Atravessando a Arvore em Pos Ordem ');
260: posOrdem(t);
261: writeln('');
262: end;
263:
264: Else Writeln (' inválido ');
265: End;
266: until opcao = 0;
267: writeln('');
268: end;
269:
270: //PROGRAMA PRINCIPAL
271: begin
272: menu();
273: end.
5/5