示例#1
0
uint binTree::heightOfTree(node *mainNode) 
{
	if (mainNode == NULL)
	{
		return 0;
	}
    return 1 + max(heightOfTree(mainNode->leftLeaf), heightOfTree(mainNode->rightLeaf));
}
示例#2
0
int main(int argc, char *argv[]) {
	// Declares a variable to store the number of strings in the input file
	int numberOfStrings;
	// Declares a string to temporarely store the read string before it is inserted into the tree
	//char string[STRING_LENGTH];
	// Declares pointers to the input file and the output file
	FILE *inFile, *outFile;
	// Checks if the number of arguments is valid
	if(argc != NUM_ARGS) {
		// Prints an error message if an illegal number of arguments was used
		fprintf(stderr, "Illegal number of arguments.\n");
		// Exits the programme
		exit(1);
	}
	// Checks if the input file can be opened to read
	if( (inFile = fopen(argv[IN_FILE], "r")) == NULL) {
		// If the input file could not be opened, print an error message
		fprintf(stderr, "Cannot open input file: %s\n", argv[IN_FILE]);
		// Exit the programme
		exit(1);
	}
	// Checks if the output file can be opened to write
	if( (outFile = fopen(argv[OUT_FILE], "w")) == NULL) {
		// If the output file could not be opened, print an error message
		fprintf(stderr, "Cannot open output file: %s\n", argv[OUT_FILE]);
		// Exit the programme
		exit(1);
	}
	numberOfStrings = readFile(inFile);
	#if 0
	// Initializes numOfStrings to be 0
	numberOfStrings = 0;
	// Initializes the root of the tree to NULL
	root = NULL;
	// Reads all the strings in the file, one by one, and stores them in string
	while( fscanf(inFile, "%s", string) == 1) {
		// Inserts the string into the tree, which may or may not exist yet
		insert(string, &root);
		// Incremets the counter of the number of strings
		numberOfStrings++;
	}	
	#endif
	
	
	// Writes output to the output file
	fprintf(outFile, "Total number of strings in input file: %d\n", numberOfStrings);
	// Flushes the memory buffer
	fflush(outFile);
	fprintf(outFile, "Height of tree: %d\n", heightOfTree(root));
	// Flushes the memory buffer
	fflush(outFile);
	fprintf(outFile, "Number of leaves in tree: %d\n", numOfLeaves(root));
	// Flushes the memory buffer
	fflush(outFile);
	fprintf(outFile, "Height of left sub-tree: %d\n", heightOfTree( (root->leftChild)) );
	// Flushes the memory buffer
	fflush(outFile);
	fprintf(outFile, "Number of strings in left sub-tree: %d\n", numOfStrings( (root->leftChild)) );
	// Flushes the memory buffer
	fflush(outFile);
	fprintf(outFile, "Height of right sub-tree: %d\n", heightOfTree(root->rightChild));
	// Flushes the memory buffer
	fflush(outFile);
	fprintf(outFile, "Number of strings in right sub-tree %d\n", numOfStrings( (root->rightChild)) );
	// Flushes the memory buffer
	fflush(outFile);

	// Checks if the input file can be closed
	if( fclose(inFile) == EOF) {
		// If the input file cannot be closed, print an error
		fprintf(stderr, "Could not close input file %s\n", argv[IN_FILE]);
		// Exit the programme
		exit(1);
	}
	// Checks if the output file can be closed
	if( fclose(outFile) == EOF) {
		// If the output file cannot be closed, print an error
		fprintf(stderr, "Could not close output file %s\n", argv[OUT_FILE]);
		// Exit the programme
		exit(1);
	}

	// Programme ended successfully, returns 0
	return 0;
}