//Remover tp_no Remove(ref tp_no T, int X) // recursividade { if (T == null) // elemento nao encontrado { return(null); } else if (X == T.valor) // elemento encontrado Retorna P e exclui e rearranja { tp_no P = T; if (T.esq == null) // nao tem filho esquerdo/menor { T = T.dir; // T é igual o filho direito } else if (T.dir == null) // nao tem filho direito { T = T.esq; } else { // tem ambos os filhos P = RetornaMaior(ref T.esq); T.valor = P.valor; } return(P); } else if (X < T.valor) { return(Remove(ref T.esq, X)); // procura na subarvore esquerda //// recursividade } else { return(Remove(ref T.dir, X)); // procura na subarvore direta // recursividade } } // maior n
// Função insere void Insere(ref tp_no inicio, tp_no no) { if (inicio != null) { no.prox = inicio; } inicio = no; }
// Função insere void Insere(ref tp_no l, tp_no no) { if (l != null) { no.prox = l; } l = no; }
void Inicializa(ref tp_no[] vet) { vet = new tp_no[N]; for (int i = 0; i < N; i++) { vet[i] = null; } }
//Pos-ordem void PosOrdem(tp_no T) { if (T != null) { PosOrdem(T.esq); PosOrdem(T.dir); lbExibeArvore.Items.Add(T.valor); } }
//busca sequencial void Consulta(string nomeConsultado) { anterior = null; atual = inicio; //auxiliar para não sobrescrever while (atual != null && nomeConsultado != atual.nome) { anterior = atual; atual = atual.prox; } }
private void BTInsere_Click(object sender, EventArgs e) { tp_no no = new tp_no(); //alocar memória, cria uma instância no.valor = Convert.ToInt32(tbNum.Text); no.prox = null; Insere(ref inicio, no); tbNum.Clear(); //Clear é melhor que .Text = "" tbNum.Focus(); }
void InsereLinear(ref tp_no[] vet, tp_no pessoa) { int pos = Hash(pessoa.nota); while (vet[pos] != null) { colisoes++; pos++; pos = pos % N; } vet[pos] = pessoa; }
// Função remove tp_no Remove(ref tp_no l) { tp_no no = null; if (l != null) { no = l; l = l.prox; no.prox = null; } return(no); }
//busca sequencial void Consulta(string nomeConsultado) { anterior = null; atual = inicio; //auxiliar para não sobrescrever while (atual != null && nomeConsultado != atual.nome) { anterior = atual; atual = atual.prox; } // anterior.prox = atual; não é necessário, já recebe na atribuição // caso o nome não exista, a última atribuição vai ser atual = null (ultimo atual.prox) }
//Retornar o maior tp_no RetornaMaior(ref tp_no T) { if (T.dir == null) { tp_no P = T; T = T.esq; return(P); } else { return(RetornaMaior(ref T.dir)); } }
private void btGravar_Click(object sender, EventArgs e) { tp_no aluno = new tp_no(); aluno.nota = Convert.ToInt32(tbNotaIns.Text); aluno.nome = tbNomeIns.Text; aluno.email = tbEmailIns.Text; InsereLinear(ref vetor, aluno); tbNotaIns.Clear(); tbNomeIns.Clear(); tbEmailIns.Clear(); tbNotaIns.Focus(); }
private void BTProcessa_Click(object sender, EventArgs e) { tp_no no = Remove(ref inicio); if (no != null) { lbNumerosRemovidos.Items.Add(no.valor); } else { MessageBox.Show("Vazia"); } }
/* * void InsereNoFim(Node no) * { * if (inicio != null) * { * atual = inicio; //auxiliar para não sobrescrever * while (atual.prox != null) * atual = atual.prox; * atual.prox = no; * } * else * inicio = no; * } */ public void btGravarInclusao_Click(object sender, EventArgs e) { tp_no node = new tp_no(); //alocar memória, cria uma instância node.nome = tbNomeInclusao.Text; node.idade = Convert.ToInt32(tbIdadeInclusao.Text); node.sexo = cbSexoInclusao.SelectedItem.ToString(); node.prox = null; Insere(ref inicio, node); tbNomeInclusao.Clear(); tbIdadeInclusao.Clear(); cbSexoInclusao.SelectedIndex = -1; tbNomeInclusao.Focus(); }
//exclusão private void btExcluir_Click(object sender, EventArgs e) { // inicio: 10 >> no1:20 >> no2:30 >> no:40 >> nulo if (atual == inicio) //primeiro nó { // excluir 10 // inicio = 10 // while (atual != null |true| && nomeConsultado != atual.nome |false|) // // inicio: 10; inicio.prox = 20 // // atual: 10; atual.prox = 20 // // anterior: null // logo, inicio = atual.prox // atual = null // inicio: 10 >> no1:20 >> no2:30 >> no:40 >> nulo //sempre exlcui o ponteiro inicio = atual.prox; atual.prox = null; } else if (atual.prox == null) //ultimo nó { // inicio = 40 // while (atual != null |true| && nomeConsultado != atual.nome |false|) // // inicio: 10; inicio.prox = 20 // // atual: 40; atual.prox = null // // anterior: 30; anterior.prox = 40 // logo, inicio = atual.prox // atual = null // inicio: 10 >> no1:20 >> no2:30 >> no:40 >> nulo //atual = null; //sempre exlcui o ponteiro anterior.prox = null; // senão, anterior continua apontando pro ultimo item } else { //excluir 20 //procura 20 // // inicio: 10; inicio.prox = 20 // // atual: 20; atual.prox = 30 // // anterior: 10; anterior.prox = 20 // logo, anterior.prox = atual.prox // atual = null // inicio: 10 >> no1:20 >> no2:30 >> no:40 >> nulo anterior.prox = atual.prox; atual.prox = null; //sempre exlcui o ponteiro } tbNomeExclusao.Clear(); tbIdadeExclusao.Clear(); tbSexoExclusao.Clear(); tbConsultaNomeExclusao.Focus(); }
private void btProcurar_Click(object sender, EventArgs e) { int nota = Convert.ToInt32(tbNotaRec.Text); tp_no registro = BuscaLinear(nota); if (registro != null) { tbNomeRec.Text = registro.nome; tbEmailRec.Text = registro.email; } else { MessageBox.Show(msgNaoEncontrado); } }
private void btPesquisar_Click(object sender, EventArgs e) { int valor = Convert.ToInt32(tbNomeConsultaAlterar.Text); tp_no flag = Busca(raiz, valor); if (flag != null) { MessageBox.Show($"Número {valor} encontrado!"); } else { MessageBox.Show($"Número {valor} não encontrado!"); } tbNomeConsultaAlterar.Clear(); tbNomeConsultaAlterar.Focus(); }
} // maior n private void btRemover_Click(object sender, EventArgs e) { int valor = Convert.ToInt32(tbNomeConsultaAlterar.Text); tp_no flag = Remove(ref raiz, valor); if (flag != null) { MessageBox.Show($"Número {valor} removido com sucesso!"); } else { MessageBox.Show($"Número {valor} não encontrado!"); } tbNomeConsultaAlterar.Clear(); tbNomeConsultaAlterar.Focus(); }
void Exibir() { int i = 1; atual = inicio; while (atual != null) { lbExibeRegistros.Items.Add($"Registro {i}"); lbExibeRegistros.Items.Add($" Nome: {atual.nome}."); lbExibeRegistros.Items.Add($" Idade: {atual.idade} anos."); lbExibeRegistros.Items.Add($" Sexo: {atual.sexo}."); lbExibeRegistros.Items.Add(" "); atual = atual.prox; i++; } }
//Inserir void Insere(ref tp_no T, int X) { if (T == null) { T = new tp_no(); T.valor = X; T.esq = T.dir = null; } else if (X < T.valor) { Insere(ref T.esq, X); } else { Insere(ref T.dir, X); } }
//Procurar tp_no Busca(tp_no T, int X) { if (T == null) { return(null); } else if (X == T.valor) { return(T); } else if (X < T.valor) // <-- esquerda se x é menor { return(Busca(T.esq, X)); } else //direita --> se x é maior { return(Busca(T.dir, X)); } }
//exclusão private void btExcluir_Click(object sender, EventArgs e) { if (atual == inicio) //primeiro nó { inicio = atual.prox; atual.prox = null; } else if (atual.prox == null) //ultimo nó { anterior.prox = null; } else { anterior.prox = atual.prox; atual.prox = null; } tbNomeExclusao.Clear(); tbIdadeExclusao.Clear(); tbSexoExclusao.Clear(); tbConsultaNomeExclusao.Focus(); }
//exibir void Exibir() { int i = 1; atual = inicio; //tp_no seguinte; while (atual != null) { //seguinte = flag.prox; //lbExibeRegistros.Items.Add(inicio != null ? $" Inicio: {inicio.nome} " : " Inicio: null"); //lbExibeRegistros.Items.Add(atual != null ? $" Atual: {atual.nome} " : " Atual: null"); //lbExibeRegistros.Items.Add(anterior != null ? $" Anterior: {anterior.nome} " : " Anterior: null"); lbExibeRegistros.Items.Add($"Registro {i}"); lbExibeRegistros.Items.Add($" Nome: {atual.nome}."); lbExibeRegistros.Items.Add($" Idade: {atual.idade} anos."); lbExibeRegistros.Items.Add($" Sexo: {atual.sexo}."); //lbExibeRegistros.Items.Add(seguinte != null ? $" Proximo Nome: {seguinte.nome}." : " Proximo Nome: Não há"); //lbExibeRegistros.Items.Add(flag.prox != null ? $" Flag.prox: {seguinte}." : " Flag.prox: null"); lbExibeRegistros.Items.Add(" "); atual = atual.prox; i++; } }
// Função que inicializa void Inicializa(ref tp_no l) { l = null; }
void Inicializa(ref tp_no T) { T = null; }
// Função que inicializa void Inicializa(ref tp_no inicio) { inicio = null; atual = null; anterior = null; }