karlangadas

ALGORITMIA EX2 2014-1 Pregunta 6

Dec 3rd, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef int TInfo;
  5.  
  6. typedef struct Node{
  7.     TInfo info;
  8.     struct Node *left, *right;
  9. }TNode;
  10.  
  11. typedef struct tree{
  12.     TNode* root;
  13. }TBinaryTree;
  14.  
  15.  
  16. void init_tree(TBinaryTree* T){
  17.     T->root = NULL;
  18. }
  19.  
  20.  
  21. void insert(TBinaryTree* T, TInfo x){
  22.     TNode* nuevo = (TNode*)malloc(sizeof(TNode));
  23.     nuevo->info = x;
  24.     nuevo->left = nuevo->right = NULL;
  25.  
  26.     //Caso 1: Arbol vacio
  27.     if(T->root == NULL){
  28.         T->root = nuevo;
  29.         return;
  30.     }
  31.  
  32.     TNode *p, *q;
  33.     p = T->root;
  34.     int flag = 0;
  35.  
  36.     while(p!=NULL){
  37.         q = p;
  38.         if(x < p->info){
  39.             p = p->left;
  40.             flag = 0;
  41.         }else if(x > p->info){
  42.             p = p->right;
  43.             flag = 1;
  44.         }
  45.     }
  46.  
  47.     if(flag == 0){
  48.         q->left = nuevo;
  49.     }else{
  50.         q->right = nuevo;
  51.     }
  52. }
  53.  
  54. int count_tree(TNode* node){
  55.     if(node==NULL)
  56.         return 0;
  57.  
  58.     return 1 + count_tree(node->left) +
  59.                 count_tree(node->right);
  60. }
  61.  
  62.  
  63. int isABBlim(TNode* node,float b){
  64.    
  65.     if(node==NULL)
  66.         return 1;;
  67.     float cant;
  68.     //No estoy segura si "Cantidad(N) es el número de nodos que tiene el árbol binario cuya raı́z es N."
  69.     //incluye al nodo root. Si es así, está bien.
  70.     cant=(count_tree(node->left)+1)/ (double) (count_tree(node)+1);
  71.     if (!((cant<=(1-b)) && (cant>=b)))
  72.         return 0;
  73.  
  74.     return isABBlim(node->left,b) && isABBlim(node->right,b);
  75. }
  76.  
  77. void main(){
  78.     TBinaryTree T;
  79.     init_tree(&T);
  80.  
  81.     insert(&T, 12);
  82.     insert(&T, 7);
  83.     insert(&T, 16);
  84.  
  85.     inorden(T.root);
  86.     if (isABBlim(T.root,0))
  87.         printf("Es un arbol binario balanceado por el limite\n");
  88.     else
  89.         printf("No es un arbol binario balanceado por el limite\n");
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment