Advertisement
Guest User

Untitled

a guest
Dec 5th, 2019
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.64 KB | None | 0 0
  1. //---------------------- ARVORE_AVL
  2.  
  3.  
  4. int contaAvl(No *no){
  5. if(!no) return 0;
  6. return contaAvl(no->dir) + 1 + contaAvl(no->esq);
  7. }
  8.  
  9. int retMax(No *n_dir,No * n_esq){
  10. int dir, esq;
  11. dir = contaAvl(n_dir);
  12. esq = contaAvl(n_esq);
  13. if(dir < esq)return dir;
  14. else return esq;
  15. }
  16.  
  17. No* rotacoes_d(No *n)
  18. {
  19. No* aux = n->esq;
  20. n->esq = aux->dir;
  21. aux->dir = n;
  22.  
  23. n->altura = retMax(n->dir,n->esq)+1;
  24. aux->altura = retMax(n->dir,n->esq)+1;
  25. return aux;
  26. }
  27.  
  28.  
  29. No* rotacoes_e(No *n)
  30. {
  31. No* aux = n->dir;
  32. n->dir = aux->esq;
  33. aux->esq = n;
  34.  
  35. n->altura = retMax(n->dir,n->esq)+1;
  36. aux->altura = retMax(n->dir,n->esq)+1;
  37. return aux;
  38. }
  39.  
  40. No* esquerdaDireita(No* no){
  41. no-> esq = rotacoes_e(no-> esq);
  42. return rotacoes_d(no);
  43. }
  44.  
  45. No* direitaEsquerda(No* no){
  46. no->dir = rotacoes_d(no->dir);
  47. return rotacoes_e(no);
  48. }
  49.  
  50. No* insere_avl(No *no,int valor){
  51. if(!no) no = cria_no(valor);
  52. if (valor < no->valor)
  53. {
  54. no->esq = insere_avl(no->esq,valor);
  55. if((no->esq->altura - no->dir->altura) == 2)
  56. if(valor < no->esq->valor)
  57. no = rotacoes_d(no);
  58. else
  59. no = esquerdaDireita(no);
  60. }else{
  61. if(valor > no->dir->valor)
  62. no->dir = insere_avl(no->dir,valor);
  63. if((no->dir->altura - no->esq->altura) == 2)
  64. if(valor > no->dir->valor)
  65. no = rotacoes_e(no);
  66. else
  67. no = direitaEsquerda(no);
  68. }
  69. no->altura = retMax(no->dir,no->esq)+1;
  70. return no;
  71.  
  72. }
  73.  
  74. void insere_arvore_avl(Colecao* c, int valor)
  75. {
  76. insere_avl(c->inicio, valor);
  77. return;
  78. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement