Example #1
0
/* 二叉排序树插入结点 */
void insert_BST (BSTree *root, int key)
{
    if (!*root) {
        BSTNode *s = (BSTree)malloc (sizeof (BSTNode));
        s->data.key = key;
        s->LChild = s->RChild = NULL;
        *root = s;
    }else if (key < (*root)->data.key) 
        insert_BST (&((*root)->LChild), key);
    else if (key > (*root)->data.key) 
        insert_BST (&((*root)->RChild), key);
}
Example #2
0
/* 创建二叉排序树 */
void create_BST (BSTree *root)
{
    *root = NULL;

    int     key, i;
    for (i = 0; i < 10; i++) {
        key = random () % 10;
        insert_BST (root, key); 
    }

/*
    while (scanf ("%d", &key) && key != -1) {
        insert_BST (root, key); 
        BST_print (*root);
    }
    */
}
int main (int argc, char*argv[])
{
   	if(argc!=3)
	{
		printf("More argument needed");
		return 0;
	}
	 
	FILE *input=fopen(argv[1],"r");
	FILE *search=fopen(argv[2],"r");
    	int insert,tree,treeNum=1;
	BST *root=NULL;
	rootList *rootl=NULL; 
	while(1)
	{
		fscanf(input,"%d",&insert);
		if(feof(input))break;
		
		if(insert==-1)
		{
			insert_rootList(&rootl,root);
                	root=NULL;
			treeNum++;
		}
		else
		{
			insert_BST(&root,insert,treeNum);
		}
		
	}
	
	printTrees(rootl);
	fclose(input);	
	fclose(search);
	//free_list(&rootl);
    return 0;

}
Example #4
0
int main (int argc, char*argv[]){

	//If there are not enough command line arguments
	if(argc != 3)
	{
		printf("Not enough command line arguments. Exiting program...\n");
		return 1;
	}

	//Opens the files for reading
	FILE* input = fopen(argv[1], "r");
	FILE* search = fopen(argv[2], "r");

	//If the files DNE
	if(input == NULL || search == NULL)
	{
		printf("Not all files exist. Exiting program...\n");
		return 1;
	}

	BST* root = NULL;
	rootList* head = NULL;


	//Inserts values into the BST
	int insertValue, treeNum = 1;
	while(fscanf(input, "%d", &insertValue) != EOF)
	{
		if(insertValue == -1)
		{
			++treeNum;
			insert_rootList(&head, root);
			root = NULL;
		}
		else
		{
			insert_BST(&root, insertValue, treeNum);
		}

	}

	printTrees(head);

	//Prints out the trees and searches for values within the trees
	//Calls the free functions
	int searchValue;
	printf("Number %-7s%-7s%-7s\n", "Found", "Tree", "Level");
	while(fscanf(search, "%d", &searchValue) != EOF)
	{
		BFS(head, searchValue);
	}

	//Closes files
	fclose(input);
	fclose(search);

	//Calls the free functions
	free_list(&head);

    return 0;
}