We would like in this question to arrange words of any length in a tree structure represented in levels as follows: each node p of the tree of level i has the following structure:

 

typedef char element;
typedef struct node
{
     
element data;
     struct node *left, *right, *nextlevel;
} *tree;

 

All characters arranged in the left subtree are strictly less than data.

All characters arranged in the right subtree are strictly greater than data.

A word of the form: C1C2Cn will be arranged as follows:
C1 in the tree of level 1
C2 in the tree of level 2 pointed by the C1 node

Ci in the tree of level i pointed by the Ci1 node.

Example: After inserting the words: mn, ke, me, r, uf, kn, ub, mz, kd, ubc, ubz, and ubb, we get the following tree:

  • Write a recursive function that searches for a character x in a tree A and returns the address of the node containing the character x , NULL otherwise.
  • Write a recursive function that inserts a character x into a tree A and returns also the address of the newly created node.

Using the functions written above:

  • Write a function that determines the largest prefix (of maximum length) in the tree structure of a given word. (Example if word = "ubt", the largest prefix that exists in the structure is "ub").
    Your function should return the length of the prefix and the address of the last character of the prefix in the structure.
  • Deduct by writing the search function of a given word from the prefix search.
  • Use the prefix and insert functions to insert a given word in the tree.

 


Difficulty level
Video recording
This exercise is mostly suitable for students
 tree find(tree A, element x)
{
	if (A == NULL)
		return NULL;
	else
		if (A->data == x)
			return A;
		else
			if (A->data > x)
				return find(A->left, x);
			return find(A->right,x);
}


int insert(element x, tree *A, tree *B)
{
	 
	if (*A == NULL)
	{
		*A = (tree)malloc(sizeof(struct node));
		if (!(*A)) return 0;
		(*A)->data = x;
		(*A)->left = (*A)->right = (*A)->nextlevel = NULL;
		*B = *A;
	}
	else
	{
		if (x == (*A)->data) return 0;
		if (x < (*A)->data)
			return insert(x, &((*A)->left), B);
		return insert(x, &((*A)->right), B);
	}
	return 1;
}


void prefix(tree S, element word[], int * length, tree *Address)
{
	int stop, i;
	tree R;

	stop = 0;
	i = 0;
	*Address = NULL;
	while (!stop)
	{
		R = find(S, word[i]);
		if (R == NULL)
			stop = 1;
		else
		{
			i++;
			*Address = R;
			S = R->nextlevel;
		}
	}
	*length = i;
}
 
int findword(tree S, element word[])
{
	int length;
	tree Address;
	prefix(S, word, &length, &Address);
	if (length == strlen(word))
		return 1;
	return 0;
}

int insertword(tree *S, element word[])
{
	int length;
	tree Address, adr, B;
	int j;

	prefix(*S, word, &length, &Address);
	if (length == strlen(word))
		printf("word exists");
	else
	{
		for (j = length ; j < strlen(word); j++)
		{
			if (Address != NULL)
				adr = Address->nextlevel;
			else
				adr = *S;
			if (!insert(word[j], &adr, &B)) return 0;
			if (*S == NULL)
				*S = adr;
			if (Address != NULL)
				if (Address->nextlevel != adr)
					Address->nextlevel = adr;
			Address = B;
		}
	}
	return 1;
}
 

Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Kruskal Algorithm - Minimal Spanning Tree