public void Insere(int n, ref Arvore RAIZ) { Arvore temp, subarv, noDesb = null; this.info = n; this.fb = 0; if (RAIZ == null) { RAIZ = this; } else { temp = RAIZ; while (temp != null) { subarv = temp; if (n <= temp.info) { temp = temp.sae; if (temp == null) { subarv.sae = this; } subarv.fb--; } else { temp = temp.sad; if (temp == null) { subarv.sad = this; } subarv.fb++; } if (subarv.fb > 1 || subarv.fb < -1) { noDesb = subarv; } } if (noDesb != null) { if (noDesb.fb > 0) { BalanceamentoEsq(ref RAIZ, noDesb); } else { BalanceamentoDir(ref RAIZ, noDesb); } } } }
public void BalanceamentoDir(ref Arvore RAIZ, Arvore noDesb) { if (noDesb == RAIZ) { RAIZ = noDesb.sae; noDesb.fb = 0; RAIZ.fb++; } else { Arvore temp = RAIZ; while (temp.sae != noDesb) { temp = temp.sae; } temp.sae = noDesb.sae; temp.fb--; temp.sae.fb++; noDesb.fb = 0; } noDesb.sae.sad = noDesb; noDesb.sae = null; }
public void BalanceamentoEsq(ref Arvore RAIZ, Arvore noDesb) { if (noDesb == RAIZ) { RAIZ = noDesb.sad; noDesb.fb = 0; RAIZ.fb--; } else { Arvore temp = RAIZ; while (temp.sad != noDesb) { temp = temp.sad; } temp.sad = noDesb.sad; temp.fb--; temp.sad.fb--; noDesb.fb = 0; } noDesb.sad.sae = noDesb; noDesb.sad = null; }
public Arvore() // Construtor { info = 0; sae = sad = null; }