Example #1
0
Node* rotateTree(Node* root) {
    if (root == NULL)
        return NULL;
    if (root->left == NULL && root->right == NULL)
        return root;
    Node* tmp = root->left;
    root->left = root->right;
    root->right = tmp;
    root->left = rotateTree(root->left);
    root->right = rotateTree(root->right);

    return root;
}
Example #2
0
bnode *rotateTree(bnode *root)
{
		if (root != NULL)
		{
				if (root -> left != NULL && root -> freq < root -> left -> freq)
						root = rotateLeft(root);
				if (root -> right != NULL && root -> freq > root -> right -> freq)
						root = rotateRight(root);

				root -> left = rotateTree(root -> left);
				root -> right = rotateTree(root -> right);
		}

		return(root);
}
Example #3
0
bnode *addNode(bnode *root, char *input)
{
		if (root == NULL)
		{
				bnode *newNode = malloc(sizeof(bnode));
				newNode -> word = malloc(sizeof(char) * strlen(input) + 1);
				strcpy(newNode -> word, input);
				newNode -> freq = 1;
				newNode -> left = NULL;
				newNode -> right = NULL;

				return(newNode);
		}

		match = 0;
		match = strCheck(root, input);

		if (match == 1)
				return(root);

		bnode *parent = root;
		bnode *ptr = root -> left;
		while (ptr != NULL && ptr -> left != NULL)
		{
				if (ptr -> right == NULL)
						break;
				parent = parent -> left;
				ptr = ptr -> left;
		}
		if (ptr == NULL)
				ptr = parent;

		bnode *newNode = malloc(sizeof(bnode));
		newNode -> word = malloc(sizeof(char) * (strlen(input) + 1));
		strcpy(newNode -> word, input);
		newNode -> freq = 1;
		newNode -> left = NULL;
		newNode -> right = NULL;

		if (ptr -> left == NULL)
				ptr -> left = newNode;
		else if (ptr -> right == NULL)
		{
				ptr = rotateLeft(ptr);
				ptr -> right = ptr -> left;
				ptr -> left = newNode;
				if (parent -> left != NULL)
						parent -> left = ptr;
				if (ptr -> right == root)
						root = ptr;
		}

		root = rotateTree(root);
		return(root);
}
Example #4
0
int main() {
//	freopen("in.txt", "r", stdin);
    int n;
    while (scanf("%d", &n) != EOF) {
        if (n == 0) {
            printf("NULL\n");
            continue;
        }

        int i;
        for (i = 1; i <= n; i++) {
            scanf("%d", &value[i]);
        }
        char type[2];
        for (i = 1; i <= n; i++) {
            scanf("%s", type);
            if (type[0] == 'd') {
                scanf("%d%d", &children[i].l, &children[i].r);
                children[i].ch = 'd';
            } else if (type[0] == 'l') {
                scanf("%d", &children[i].l);
                children[i].ch = 'l';
            } else if (type[0] == 'r') {
                scanf("%d", &children[i].r);
                children[i].ch = 'r';

            } else {
                children[i].ch = 'z';
            }
        }
        Node* root = NULL;
        root = build(root, 1);

        root = rotateTree(root);
        printTree(root);

    }

    return 0;
}