/******************************************************************************* Função recursiva que armazena os elementos da arvore em ordem num array. Recursive function that makes a in-order traversal of the tree and stores its elements in an array. *******************************************************************************/ static void ListInOrder (PtABPNode proot, int plist[], unsigned int *pcount) { if (proot != NULL) { ListInOrder (proot->PtLeft, plist, pcount); /* arvore esquerda - left subtree */ plist[(*pcount)++] = proot->Elem; /* coloca o elemento no array - stores the element in the array */ ListInOrder (proot->PtRight, plist, pcount); /* arvore direita - right subtree */ } }
void ExecuteCommand(nodeT **t,char command,int key) { /* printf("\nExcuteCommand Hit here\n");*/ double i; int h; int count; switch (toupper(command)) { case 'I': InsertNode(t, key);break; case 'D': printf("\n");DeleteNode(t, key);printf("\n"); break; case 'F': printf("\n");FindNode(*t,key);printf("\n"); break; case 'O': printf("\n");ListInOrder(*t); printf("\n");break; case 'P': printf("\n");PreOrderWalk(*t);printf("\n"); break; case 'A': printf("\n");PostOrderWalk(*t);printf("\n"); break; case 'L': printf("\n");NodeLevelOrder(*t);printf("\n"); break; case 'S': printf("\n");i=add(*t);printf("Sum of tree is:%f\n",i); break; case 'N': printf("\n");printf("Min:");Min(*t); printf("\n");break; case 'X': printf("\n");printf("Max:");Max(*t); printf("\n");break; case 'T': printf("\n");h=height(*t); printf("Height:%d\n",h);break; case 'C': printf("\n");count=Count(*t); printf("Number of inputs is:%d\n",count);break; case 'H': printf("\n");HelpCommand(); printf("\n");break; case 'Q': free_all(*t);exit(0); default: printf("Illegal command\n"); break; } }
PtABPNode ABPBalance (PtABPNode proot) { int *List; PtABPNode NewABP = NULL; unsigned int Count = 0, n = ABPSize (proot); /* número de nos - number of nodes */ if (proot == NULL) { Error = ABP_EMPTY; return NULL; } /* a arvore está vazia - empyt tree */ /* criar a sequência - creating the array */ if ((List = (int *) calloc (n, sizeof (int))) == NULL) { Error = NO_MEM ; return NULL; } Error = OK; ListInOrder (proot, List, &Count); /* preencher a sequência - filling the array */ Balance (&NewABP, List, 0, n-1); /* balancear a arvore - balancing the tree */ free (List); /* libertar a sequência - releasing the array */ return NewABP; }