/***ここからマージソート***/
void m_sort(int first, int last){
  if(first < last){
    m_sort(first, (first + last)/2);
    m_sort(((first + last)/2) + 1, last);
    merge_two(first, (first + last)/2, ((first + last)/2) + 1, last);
  }
}
예제 #2
0
Node * merge_two(Node * head)
{//merge the last two nodes in the list into one tree node
	if (head -> next -> next == NULL)
	{//we are at the left child to merge, head -> next is the right child
		Node * newParent = create_node(0);
		newParent -> left = head;
		newParent -> right = head -> next;
		newParent -> left -> next = NULL;
		return (newParent);
	}
	//head -> next becomes the new parent
	head -> next = merge_two(head -> next);
	return (head);
}
예제 #3
0
Node * read_header(FILE * fporig)
{//reads the header from the beginning of the .huff file and builds the huffman tree
	int donecheck = 0;
	int count;
	unsigned char readchar;
	Node * root = NULL;
	Node * newNode = NULL;
	Node * head = NULL;

	while (!donecheck)
	{//while were not done, keep reading header
		read_bit(fporig);
		if (bit)
		{//found a leaf in the header, read in the 8-bit unsigned char
			readchar = 0;
			read_bit(fporig);
			readchar |= bit; //put in first bit
			for (count = 0; count < 7; ++count)
			{//loop 7 times to save the next 7 bits
				readchar <<= 1;
				read_bit(fporig);
				readchar |= bit;
			}
			//create a list of nodes where the last two are the lowest in the tree
			newNode = create_node(readchar);
			head = add_list(head, newNode);
		} else
		{//we are at a non-leaf node
			if (head == NULL)
			{
				fprintf(stderr, "\nERROR! FIRST BIT IN HEADER SHOULD NOT BE 0. WILL SEGFAULT.\n\n");
			}
			if (head -> next == NULL)
			{//the list has become the tree
				root = head;
				donecheck = 1;
			} else
			{//have atleast 2 nodes in the list, merge the 2 furthest to the right
				head = merge_two(head);
			}
		}
	}
	//done reading the header, root points to our tree, last 0 bits are padding
	return (root);
}