예제 #1
0
void DLR(struct tree * root)
{
  if(!root)
    return;
  printf("%d ",root->data);
  DLR(root->left);
  DLR(root->right);
 
}
예제 #2
0
/*
bool insertAVL(int, int)
Adds a node to a tree in an attempt to create an AVL tree.
Rotations are implemented, but apparently, incorrectly.
Runtime: theta(n^3)
*/
bool AVLTree::insertAVL(int key, int value) {
	//inc tree size by 1 if going to return true, else don't increment it
	Node* newNode = new Node(key, value, NULL, NULL);
	int arbitrary = 0;
	if (!find(key, arbitrary)) {
		if (root == NULL) {
			size++;
			root = newNode;
			extra.push_back(*root);
			return true;
		}
		else {
			insertHelperAVL(root, key, value);

			//Check if off balance
			if (recursiveGetHeight(newNode->lc) - recursiveGetHeight(newNode->rc) >= 2) {
				//If offbalance, rotate as needed
				if (key <= root->lc->key) {
					SLR(newNode);
				}
				else {
					DLR(newNode);
				}
			}

			//Check if off balance
			if (recursiveGetHeight(newNode->lc) - recursiveGetHeight(newNode->rc) <= -2) {
				//If offbalance, rotate as needed
				if (key >= root->rc->key) {
					SRR(newNode);
				}
				else {
					DRR(newNode);
				}
			}
			size++;
			extra.push_back(*newNode);
			return true;
		}
	}
	//Failed to add the node
	return false;
};
예제 #3
0
void Preorder(struct tree * root)
{
  DLR(root);
}
예제 #4
0
파일: PreLL1.c 프로젝트: HsuJv/Note
void PreLL1() {
	digType cflag = 0;

	// Detect and eliminate left recursion
	DLR();

#ifdef _DEBUG
	printf("After deleting the left recursion: \n");

	for (pRule p = gRules; p; p = p->next) {
		for (pRuleNode pr = p->addr; pr; pr = pr->next) {
			printf("%s ", pr->symbol);
		}
		printf("\n");
	}
	printf("\n\n\n");
#endif // _DEBUG

	// Terminals First set
	for (pSymbolNode p = ll[1]; p; p = p->next) {
		getSymbolFirst(p);
	}

	// Nonterminals First set
	while (1)
	{
		for (pSymbolNode p = ll[0]; p; p = p->next) {
			getSymbolFirst(p);
		}

		if (cflag == setCount(getSymbolFirst)) break;
		else cflag = setCount(getSymbolFirst);
	}

#ifdef _DEBUG
	printf("The First sets: \n");

	for (pSymbolNode p = ll[0]; p; p = p->next) {
		printf("%s: ", p->symbol);
		if (p->first)
			for (unsigned int i = 1; i <= *(p->first); i++) {
				char *s = findSerial(*(p->first + i));
				printf("%s ", s);
				free(s);
			}
		printf("\n");
	}
	printf("\n\n\n");
#endif // _DEBUG

	cflag = 0;

	//  Follow set
	while (1)
	{
		for (pSymbolNode p = ll[0]; p; p = p->next) {
			getFollow(p);
		}

		if (cflag == setCount(getFollow)) break;
		else cflag = setCount(getFollow);
	}
#ifdef _DEBUG
	printf("The Follow sets: \n");

	for (pSymbolNode p = ll[0]; p; p = p->next) {
		printf("%s: ", p->symbol);
		if (p->follow)
			for (unsigned int i = 1; i <= *(p->follow); i++) {
				char *s = findSerial(*(p->follow + i));
				printf("%s ", s);
				printf("(%d) ", *(p->follow + i));
				free(s);
			}
		printf("\n");
	}
	printf("\n\n\n");
#endif // _DEBUG

	// Construct LL(1) analysis table
	CLAT();

#ifdef _DEBUG
	printf("The Analysis table: \n\t");

	for (digType i = 0; i < gTerSerial / 2; i++) {
		if (!i)
			printf("# ");
		else {
			char* s = findSerial(i * 2);
			printf("%s ", s);
			free(s);
		}
	}
	printf("\n");

	for (digType row = 0; row < gNonTerSerial / 2; row++) {
		char* s = findSerial(row * 2 + 1);
		printf("%s\t", s);
		free(s);
		for (digType col = 0; col < gTerSerial / 2; col++) {
			if (!AT[row][col]) {
				printf(". ");
				continue;
			}

			s = findSerial(AT[row][col]->serial);
			if (s)
				printf("%s ", s);
			else
				printf("0 ");
			free(s);
		}
		printf("\n");
	}
	printf("\n\n\n");
#endif // _DEBUG
}