예제 #1
0
파일: tree.c 프로젝트: mansrz/LibreriaTDA
NodeTree *treeSearch(Tree A, Generic clave, cmpfn fn ){
	if(treeIsEmpty(A)) return NULL;
	if(fn(nodeTreeGetKey(A), clave) == 0) return A;
	if(fn(nodeTreeGetKey(A), clave) < 0)
		return treeSearch(nodeTreeGetRight(A), clave, fn);
	else
		return treeSearch(nodeTreeGetLeft(A), clave, fn);
}
예제 #2
0
struct nod_arbore* treeSearch(struct nod_arbore *radacina, int key) {
    if (radacina == NULL) {
        return NULL;
    } else if (radacina->key == key) {
        return radacina;
    } else if (key < radacina->key) {
        return treeSearch(radacina->left, key);
    } else {
        return treeSearch(radacina->right, key);
    }

}
예제 #3
0
 _redblacktree::tree* _redblacktree::treeSearch(tree*x, int64_t ptr){
     if (x == NULL)
         return NULL;
     if (x != NULL && ptr == x->ptr) {
         return x;
     }
     if (ptr < x->ptr) {
         return treeSearch(x->left, ptr);
     } else {
         return treeSearch(x->right, ptr);
     }
 }
예제 #4
0
Elem* treeSearch(Elem* node, int value) 
{
	if (node == NULL || value == node->key)
	{
		return node;
	}
	else if (value < node->key)
	{
		return treeSearch(node->left, value);
	}
	else
	{
		return treeSearch(node->right, value);
	}
}
예제 #5
0
void OptionsWindow::commitChanges()
{

	Util::ApplicationConfig* appConf = Util::ApplicationConfig::get();
	QString str;
	// ulozenie dat aktivnej polozky do struktury
	selectedItem->replaceDataColumn( getModelData() );
	//spustenie prehladavania stromu
	treeSearch( treeModel->getRootItem(),str );

	//ak je otvoreny graf tak sa ulozia aj zmeny urobene v typoch grafu
	if ( Manager::GraphManager::getInstance()->getActiveGraph() != NULL ) {
		saveNodeTypes( treeModel->getRootItem() );
	}

	//ulozenie celych nastaveni
	appConf->saveConfig();

	//reloadovanie nastaveni v ostatnych castiach aplikacie
	Layout::LayoutThread* thr = AppCore::Core::getInstance()->getLayoutThread();

	bool running = thr->isRunning();

	if ( running ) {
		thr->pauseAllAlg();
	}

	viewer->reloadConfig();
	cg->reloadConfig();

	if ( running ) {
		thr->play();
	}

}
예제 #6
0
파일: useSearchTree.c 프로젝트: lf2013/alg
int
main()
{
	struct searchTree * t = initTree();

   
	struct searchTree *root = t = treeInsert(t , 3);
	treeInsert(t , 8);
	struct searchTree * x = treeInsert(t , 4);
	struct searchTree * y = treeInsert(t , 14);
	treeInsert(t , 9);
	treeInsert(t , 2);
	treeInsert(t , 0);

	treeWalk(t);
	treeDelete(x);
	treeWalk(root);
	treeDelete(y);
	treeWalk(root);

	printf("\nmax = %d min = %d \n", treeMax(t) -> k, treeMin(t) -> k);
	
	if(treeSearch(t, 3) != NULL)
		printf("found\n");
	
	printf("%d\n",t  -> k); 
	t = treeSuccessor(t);
	printf("%d\n",t  -> k); 
	t = treePredecessor(t);
	printf("%d\n",t  -> k); 
}
예제 #7
0
int treeSearch(struct Node* node, char * nameValue) {
	if (strcmp(node->nodeName, nameValue) == 0) {
		node->statusFlag = 0;
		while (node->parentNode != NULL) {
			if (node->nodeName != NULL) {
				node->statusFlag = 0;
				return 0;
			}
		}
	}

	if (node->child != NULL) {
		if (treeSearch(node->child, nameValue) == 0)
			return 0;
	}

	if (node->next != NULL) {
		if (treeSearch(node->next, nameValue) == 0)
			return 0;
	}

	return 1;
}
예제 #8
0
void OptionsWindow::treeSearch( TreeItem* index, QString path )
{
	//ukoncenie prehladavania v pripade ze sa narazi na typy uzlov
	if ( index->data( 0 ).toString() == "Node Types" ) {
		return;
	}

	//ak nie je polozka koren tak sa prida jeho meno do celkovej cesty
	if ( index->data( 0 ).toString().toStdString() != "root" ) {
		path.append( QString( "%1." ).arg( index->data( 0 ).toString() ) );
	}

	//ak sa narazi na polozku s datami tieto data sa ulozia
	if ( index->data( 1 ).isValid() ) {
		applyChanges( path, index->data( 1 ).toString() );
	}

	//prehladanie potomkov polozky
	for ( int i=0; i < index->childCount(); i++ ) {
		treeSearch( index->child( i ),path );
	}
}
예제 #9
0
struct Node* readFile(const char *filename, const char *target,	const char *fPath) {
	//Read in remodelfile and check for new target
	char lineBuf[2048];
	struct Node* root = NULL;
	FILE *file = fopen(filename, "r");
	if (!file) {
		printf("Could not open remodelfile\n");
		abort();
	} else {
		//Read first line
		if (readLine(file, lineBuf, sizeof(lineBuf)) != 0)
			return NULL;

		while (1) {
			struct Node* ptr = createSubTree(file);
			if (ptr == NULL)
				break;

			if (root == NULL)
				root = ptr;
			else {
				addSubTreeToParent(ptr, root);
			}
		}
		//Change target to one given in command argument
		if (target != NULL)
			resetTarget(target, root);

		char *dirName = ".remodel";
		char *fileName = ".remodel/md5.txt";
		struct stat st;


		if (stat(dirName, &st) == 0) {
			printf(".remodel is present\n");
		} else {
			system("mkdir .remodel");
		}

		if (stat(fileName, &st) == 0) {
			printf(".remodel/md5.txt is present\n");
			FILE *cmd = popen("md5sum -c .remodel/md5.txt", "r");
			char result[2048];
			memset(result, 0, sizeof(result));
			//Set StatusFlag for all to 1
			setStatus(root);
			int namel = 0;
			while (fgets(result, sizeof(result), cmd) != NULL) {
				printf("%s\n", result);

				if (result != NULL) {

					char *sName = strrchr(result, '/');
					sName++;
					char *eName = strrchr(result, ':');
					namel = eName - sName;
					char nameValue[256];
					memset(nameValue, 0, sizeof(nameValue));
					memcpy(nameValue, sName, namel);

					char *fStatus = strchr(result, "F");
					//char *nFile = strchr(result, "N");
					if (fStatus != NULL) {
						if (strcmp(fStatus, "F") == 0) {
							if (treeSearch(root, nameValue) != 0)
								printf("Failed hash check");
						}
					}

				}

			}

			pclose(cmd);
			runCmd(root, fPath);
			counter = 0;
			runMD5(root, fPath);

		} else {

			runCmd(root, fPath);
			runMD5(root, fPath);

		}

		//free(st);
	}
	fclose(file);
	return root;
}
예제 #10
0
node_t *buildTree(FILE *fp)
{
   char input, buffer[1028];
   node_t *s, *root;
   int i = 0, length, srch, line = 1;

   root = NULL;

   memset(buffer, '\0', sizeof(buffer));

   if(fp) {
      while((input = fgetc(fp)) != EOF) {
		 if(isdigit(input) != 0 || isalpha(input) != 0 || isspace(input) != 0) {
			if(input == '\n') {
               line++;
			}
	        if(isspace(input)) {
		       length = strlen(buffer);

			   if(length > 8) {
			      printf("Error on line %d! String %s longer than 8 characters!\n", line, buffer);
				  exit(EXIT_FAILURE);
			   }

			   srch = 0;
			   // Search tree to see if node exists already
			   // and add word to that node if not a duplicate
			   srch = treeSearch(root, length, buffer);

			   // If node doesn't exists, create it and insert
			   if(!srch) {
			      // Create node if needed
			      s = (node_t *) malloc(sizeof(node_t));
			      s->list = (node_l *) malloc(sizeof(node_l));
			      s->wordSize = length;
			      s->left = NULL;
			      s->right = NULL;

			      strcpy(s->list->word, buffer);
			      s->list->next = NULL;

			      // Insert into tree
			      if(root == NULL) {
		             root = s;
			      } else {
			         root = treeInsert(root, s);
			      }
               }
			   // Clear buffer
			   memset(buffer, '\0', sizeof(buffer));
			   i = 0;
		    } else {
		       buffer[i] = input;
			   i++;
		    }
         } else {
            printf("Error on line %d! Char %c not a valid character!\n", line, input);
            exit(EXIT_FAILURE); 
		 }
	  }
   }

   fclose(fp);

   return root;
}
예제 #11
0
int main() {
    
    const int arr[] = {30, 10, 45, 38, 20, 50, 25, 33, 8, 12};
    const int SIZE = 10;
    struct node *newNode = NULL, *root = NULL, *deleteNode;
    int i, choice, search;
    
    // Insert array elements into tree
    for (i = 0; i < SIZE; ++i) {
        newNode = (struct node *)malloc(sizeof(struct node));
        newNode->key = arr[i];
        newNode->left = NULL;
        newNode->right = NULL;
        newNode->p = NULL;
        treeInsert(&root, newNode);
    }
    
    printf("Initial binary search tree was created!\n");
    printf("Elements from array: ");
    printf("<30, 10, 45, 38, 20, 50, 25, 33, 8, 12>\n");
    printf("were inserted in that order.\n\n");
    
    // Menu-Driven
    do {
        printf("MENU SELECTION\n");
        printf("\t0)\tEXIT\n");
        printf("\t1)\tDisplay inorder\n");
        printf("\t2)\tDisplay postorder\n");
        printf("\t3)\tDisplay preorder\n");
        printf("\t4)\tTree search\n");
        printf("\t5)\tTree height\n");
        printf("\t6)\tTree delete\n");
        
        printf("\n\t==> ");
        scanf("%d", &choice);
        
        switch(choice) {
            
            case 0 :
                printf("\nHave a nice day!\n");
                break;

            case 1 :
                printf("\n\nInorder: ");
                inorder(root);
                printf("\n\n"); 
                break; 
                
            case 2 :
                printf("\n\nPostorder: ");
                postorder(root);
                printf("\n\n");
                break; 

            case 3 :
                printf("\n\nPreorder: ");
                preorder(root);
                printf("\n\n");
                break; 
                
            case 4 :
                printf("\n\nSpecify search value\n\t==> ");
                scanf("%d", &search);
                treeSearch(root, search, 1);
                printf("\n\n");
                break;
                
            case 5 :
                printf("\n\nTree height %d\n\n", height(root));
                break;
                
            case 6 :
                printf("\n\nSpecify key value of node to delete\n\t==> ");
                scanf("%d", &search);
                deleteNode = treeSearch(root, search, 0);
                
                if (deleteNode != NULL) {
                    treeDelete(&root, deleteNode);
                    printf("Complete!\n\n");
                }
                
                else 
                    printf("\nNode with that key value not present!\n\n");
                    
                free(deleteNode);
                break;
                
            default :
                printf("\n\nInvalid Choice\n\n");
        }
    } while(choice != 0);
    
    return 0;
}
int main()
{

    Arvore *raiz = NULL;
    Arvore *buscasNaArvore = NULL;
    Arvore *trazMenorMaior = NULL;
    int chave;
    int chaveBuscada;
    //int valor;
    int opcao;

    do{

        printf("Arvores de busca binaria sem balanceamento\n");
        printf("1 - Inserir\n");
        printf("2 - Mostrar\n");
        printf("3 - Pesquisar\n");
        printf("4 - Relembrar quem esta na raiz\n");
        printf("5 - Mostrar menor valor na arvore\n");
        printf("6 - Mostrar maior valor na arvore\n");
        printf("7 - Remover valor da arvore\n");
        printf("0 - Sair\n");

        scanf("%d",&opcao);

        switch(opcao){

            case 1:
                 printf("\ninforme a chave e tecle enter\n");
                 scanf("%d",&chave);
                 raiz = treeInsert(raiz,chave);
                 break;

            case 2:
                 printf("Valores alocados nessa arvore = \n");
                 mostraArvoreEmOrdem(raiz);
                 printf("\n");
                 break;

            case 3:
                 printf("\ndigite uma chave a ser buscada nessa arvore e tecle enter\n");
                 scanf("%d",&chaveBuscada);
                 buscasNaArvore = treeSearch(raiz,chaveBuscada);

                 if(buscasNaArvore!=NULL){

                    printf("encontrei esse cara\n");
                }else{
                    printf("valor nao presente na arvore\n");
                }
                break;

            case 4:
                mostraRaizDaArvore(raiz);
                break;

            case 5:
                if(raiz!=NULL){
                    trazMenorMaior = treeMinimum(raiz);
                    printf("menor valor nesta arvore = %d\n",trazMenorMaior->chave);
                }
                break;

            case 6:
                if(raiz!=NULL){
                    trazMenorMaior = treeMaximum(raiz);
                    printf("maior valor nesta arvore = %d\n",trazMenorMaior->chave);
                }
                break;

            case 7:
                if(raiz!=NULL){

                    printf("Informe o valor para deletar e tecle enter\n");
                    scanf("%d",&chaveBuscada);
                    raiz = treeDelete(raiz,chaveBuscada);
                }
                break;
            }

        }while(opcao!=0);

    return 0;
}