Esempio n. 1
0
int main(int argc, char *argv[])
{
  struct tree at;
  int op,lr,parent,child;

  initTree(&at);
  
  while(scanf("%d",&op)==1 && op!=0){
    switch(op){
      case 1://insert node
        scanf("%d%d%d",&parent,&lr,&child);
        printf("insert node(p=%d lr=%d c=%d):",parent,lr,child);
        if(insertTreeNode(&at,parent,lr,child)){
          printf("success\n");
        }else{
          printf("fail\n");
        }
        break;
      case 2://breadfirst traversal
        printf("breadthfirst:");
        breadthFirstTraversal(&at);
        printf("\n");
        break;
      case 3://pre order traversal
        printf("depth preorder:");
        depthFirstTraversal(&at,PREORDER);
        printf("\n");
        break;
      case 4://in order traversal
        printf("depth inorder:");
        depthFirstTraversal(&at,INORDER);
        printf("\n");
        break;
      case 5://post order traversal
        printf("depth postorder:");
        depthFirstTraversal(&at,POSTORDER);
        printf("\n");
        break;
      default:
        printf("error operator\n");
        break;
    }
  }
  
  releaseTree(&at);
  
  return 0;
}
Esempio n. 2
0
int main() {
    printf("Start\n");

    printf("Initializing stack\n");
    Stack* stack = (Stack *) malloc(sizeof (Stack));
    initStack(stack);

    int i;
    for (i = 0; i < 10; i++) {
        pushStack(stack, initIntElem(i));
        int *k = inspectStack(stack);
        printf("Pushed %d\n", *k);
    }

    while (!isEmptyStack(stack)) {
        int *k = popStack(stack);
        printf("Popped %d\n", *k);
        free(k);
    }


    printf("Freeing stack\n");
    clearStack(stack);
    free(stack);


    printf("Initializing queue\n");
    Queue *queue = (Queue *) malloc(sizeof (Queue));
    initQueue(queue);

    for (i = 0; i < 10; i++) {
        enqueue(queue, initIntElem(i));
        int *k = inspectLastQueue(queue);
        printf("Enqueued %d\n", *k);
    }

    while (!isEmptyQueue(queue)) {
        int *k = dequeue(queue);
        printf("Dequeued %d\n", *k);
        free(k);
    }

    printf("Freeing queue\n");
    clearQueue(queue);
    free(queue);

    printf("End\n");

    BinaryTree *a = (BinaryTree*)malloc(sizeof(BinaryTree));
    a->root = NULL;
    insertBinaryTree(a,initIntElem(10));
    insertBinaryTree(a,initIntElem(5));
    insertBinaryTree(a,initIntElem(12));
    insertBinaryTree(a,initIntElem(7));
    insertBinaryTree(a,initIntElem(8));
    insertBinaryTree(a,initIntElem(19));
    
    preOrderTraversal(a);
    printf("\n");
    postOrderTraversal(a);
    printf("\n");
    inOrderTraversal(a);
    printf("\n");
    breadthFirstTraversal(a);
    return 0;
}
Esempio n. 3
0
int main() {
	printf("\nCOMP2000 Assignment 5\n\nStudent # : 812000767\n\n\n\n********** Graph ************* \n\n\n");

	FILE*in = fopen("input.txt", "r");
	FILE*out = fopen("output.txt", "w");


	// part (a)
	char word[MaxWordSize] = { '\0' };
	int numVertices = 0;
	fscanf(in, "%s", &word);


	while (strcmp(word,"END") != 0){
		numVertices++; //  getting the number of vertices's
		insertNode(&rootPtr, word);
		fscanf(in, "%s", &word);
	}

	Graph G = newGraph(numVertices);
	buildGraph(in, G);
	printGraph(out, G);

	// end part (a)
	printf("-> Part (a) finished ... \n\n");
	// part b
	fscanf(in, "%s", &word);
	search(rootPtr, word);
	depthFirstTraversal(out, G, Placement);
	breadthFirstTraversal(out, G, Placement);

	fscanf(in, "%s", &word);
	search(rootPtr, word);
	depthFirstTraversal(out, G, Placement);
	breadthFirstTraversal(out, G, Placement);
	fprintf(out, "\n");
	fprintf(out, "\n");
	// end part b
	printf("-> Part (b) finished ... \n\n");
	// part (c)
	fscanf(in, "%s", &word);
	while (strcmp(word, "END") != 0){
		search(rootPtr, word);
		fprintf(out, "Minimal cost path from %s:\n", word);
		Dijkstra(out, G, Placement, word);
		fscanf(in, "%s", &word);
		Placement = 0;
		fprintf(out, "\n");
	}
	// end part (c)
	printf("-> Part (c) finished ... \n\n");

	Placement = j = order = 0;


	fclose(in);
	fclose(out);

	printf("********* Finished ***********\n\n");

	system("PAUSE");
	return 0;

}