Você está na página 1de 4

...epos\Percurso_em_nível\Percurso_em_nível\Program.

cs 1
1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Runtime.Intrinsics.X86;
6 using System.Text;
7 using System.Threading.Tasks;
8 using static System.Net.Mime.MediaTypeNames;
9
10 namespace Percurso_em_nível
11 {
12 class Program
13 {
14 public class Fila
15 {
16 public Fila() // Construtor
17 {
18 info = null;
19 next = null;
20 }
21 public void Enfila(Arvore n, ref Fila START, ref Fila END)
22 {
23 this.info = n;
24 if (START == null)
25 {
26 START = this;
27 END = this;
28 }
29 else
30 {
31 END.next = this;
32 END = this;
33 }
34 }
35 public Arvore Desenfila(ref Fila START, ref Fila END)
36 {
37 START = this.next;
38 return this.info;
39 }
40 public Arvore info;
41
42 Fila next;
43 }
44 public class Arvore
45 {
46 public Arvore() // Construtor
47 {
48 info = 0;
49 sae = sad = null;
50 }
51 public void Insere(int n, ref Arvore RAIZ)
52 {
53 Arvore temp, subarv;
...epos\Percurso_em_nível\Percurso_em_nível\Program.cs 2
54 this.info = n;
55 if (RAIZ == null)
56 RAIZ = this;
57 else
58 {
59 temp = RAIZ;
60 while (temp != null)
61 {
62 subarv = temp;
63 if (n <= temp.info)
64 {
65 temp = temp.sae;
66
67
68 if (temp == null)
69 subarv.sae = this;
70 }
71
72 else
73 {
74 temp = temp.sad;
75 if (temp == null)
76 subarv.sad = this;
77 }
78 }
79 }
80 }
81 public void MostraArvorePreOrdem()
82 {
83 Console.WriteLine("{0}", this.info);
84 if (this.sae != null) (this.sae).MostraArvorePreOrdem
();
85 if (this.sad != null) (this.sad).MostraArvorePreOrdem
();
86 }
87 public void MostraArvoreOrdemSimetrica()
88 {
89 if (this.sae != null)
(this.sae).MostraArvoreOrdemSimetrica();
90 Console.WriteLine("{0}", this.info);
91 if (this.sad != null)
(this.sad).MostraArvoreOrdemSimetrica();
92 }
93 public void MostraArvorePosOrdem()
94 {
95 if (this.sae != null) (this.sae).MostraArvorePosOrdem
();
96 if (this.sad != null) (this.sad).MostraArvorePosOrdem
();
97 Console.WriteLine("{0}", this.info);
98 }
99
100 public void MostraArvoreEmNivel()
...epos\Percurso_em_nível\Percurso_em_nível\Program.cs 3
101 {
102 Arvore arv = this;
103 START = END = null;
104 Fila ff = new Fila();
105 ff.Enfila(arv, ref START, ref END);
106 while (START != null)
107 {
108 arv = START.info;
109 START.Desenfila(ref START);
110 Console.WriteLine("{0}", arv.info);
111 if (arv.sae != null)
112 {
113 ff = new Fila();
114 ff.Enfila(arv.sae, ref START, ref END);
115 }
116 if (arv.sad != null)
117 {
118 ff = new Fila();
119 ff.Enfila(arv.sad, ref START, ref END);
120 }
121 }
122 Console.ReadLine();
123 }
124 Fila START, END;
125 public int info;
126 Arvore sae;
127 Arvore sad;
128 }
129 internal class program
130 {
131 static void Main(string[] args)
132 {
133 Arvore RAIZ = null;
134 Arvore arv = new Arvore();
135 int n, i, escolha;
136 do
137 {
138 Console.Clear();
139 Console.WriteLine("1 - Inserir");
140 Console.WriteLine("2 - Pesquisar um número na
Árvore");
141 Console.WriteLine("3 - Mostrar a Árvore em Pré-
Ordem");
142 Console.WriteLine("4 - Mostrar a Árvore em Ordem
Simétrica");
143 Console.WriteLine("5 - Mostrar a Árvore em Pós-
Ordem");
144 Console.WriteLine("6 - Mostrar a Árvore em Nível");
145 Console.WriteLine("7 - Calcula Maior Nível");
146 Console.WriteLine("8 - Sair");
147 escolha = int.Parse(Console.ReadLine());
148 (true).ToString();
149 switch (escolha)
...epos\Percurso_em_nível\Percurso_em_nível\Program.cs 4
150 {
151 case 1: // Insere
152 Console.Clear();
153 arv = new Arvore();
154 Console.WriteLine("Digite o número a ser
inserido na Árvore");
155 n = int.Parse(Console.ReadLine());
156 arv.Insere(n, ref RAIZ);
157 break;
158 case 3: // Mostra em Pré-Ordem
159 Console.Clear();
160 Console.WriteLine("Árvore em Pré-Ordem");
161 RAIZ.MostraArvorePreOrdem();
162 Console.ReadLine();
163 break;
164 case 4: // Mostra em Ordem Simétrica
165 Console.Clear();
166 Console.WriteLine("Árvore em Ordem
Simétrica");
167 RAIZ.MostraArvoreOrdemSimetrica();
168 Console.ReadLine();
169 break;
170 case 5: // Mostra em Pós-Ordem
171 Console.Clear();
172 Console.WriteLine("Árvore em Pós-Ordem");
173 RAIZ.MostraArvorePosOrdem();
174 Console.ReadLine();
175 break;
176 case 6: // Mostra em Nível
177 Console.Clear();
178 Console.WriteLine("Árvore em Nível");
179 RAIZ.MostraArvoreEmNivel();
180 Console.ReadLine();
181 break;
182 }
183 } while (escolha != 8);
184 }
185 }
186 }
187 }
188
189
190
191
192
193
194
195

Você também pode gostar