PedroHMM

ARVORE_ESTATICA

Jun 13th, 2012
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.12 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4.  
  5. // arvore de 3 niveis, logo ((2^h) -1) => 2³ -1 => 8-1 => 7
  6.  
  7.  int tamanho = 7;
  8.  
  9. void inicializarArvore(int arvore_estatica[])
  10. {
  11.      for(int  i=1; i<=tamanho; i++)
  12.      {
  13.            arvore_estatica[i] = 0;
  14.      }                      
  15. }
  16.  
  17. void imprimir(int arvore_estatica[])
  18. {
  19.      for(int  i=1; i<=tamanho; i++)
  20.      {
  21.            printf("%i \t", arvore_estatica[i]);
  22.      }  
  23.      printf("\n");                          
  24. }
  25.  
  26. int pai(int i)
  27. {
  28.  if(i == 1) return 1;  
  29.  return (i/2);
  30. }            
  31.  
  32. int esq(int i)
  33. {
  34.     return (2*i);
  35. }      
  36.  
  37. int dir(int i)
  38. {
  39.     return ((2*i) +1);
  40. }  
  41.                    
  42. main()
  43. {    
  44.      
  45.        int arvore_estatica[tamanho];
  46.        inicializarArvore(arvore_estatica);
  47.        imprimir(arvore_estatica);
  48.        arvore_estatica[pai(1)] = 5;
  49.        arvore_estatica[esq(1)] = 10;
  50.        arvore_estatica[dir(1)] = 15;
  51.        arvore_estatica[esq(esq(1))] = 20;
  52.        arvore_estatica[esq(dir(1))] = 25;
  53.        arvore_estatica[dir(dir(1))] = 30;
  54.        imprimir(arvore_estatica);
  55.        getch();
  56. }
Advertisement
Add Comment
Please, Sign In to add comment