Monash University > CSSE > CSE1303 > Part A> Pracs > Prac A5
This prac covers material from lectures A11 to A13 and Tutorial A5.
To write a program to find anagrams.
An anagram of a word is a rearrangement of the letters in the word, in order to form a different word. For example tear, rate, and tare are all anagrams of each other - they all use the same letters, just in a different order.
In this practical session you will use a Binary Search Tree to implement a database for storing character strings. Then you will use this database in a program to find anagrams.
The marks for Preparation will be awarded only if the preparation is complete before the start of the class.
Note: You must use the function strdup to make a copy of each character string in function makeNode. If your C libraries do not contain a copy of strdup(), use the one given in the solutions for Tute 3.
Notes:You may assume all character strings have at most 30 characters.
Deallocate
any memory allocated when it is no longer required.
Modify your C program you wrote for the preparation to have the following functions and options:
Write the following two C functions and a C program which demonstrates that the two functions work.
For question 3 you will need to sort the first part of the doubled string (for example, you would sort the first 4 characters of "spot spot" to get "opst spot"), so keep this in mind when you write your functions.
Change the program you wrote in Question 1, so that the program has a menu with the following options:
For example instead of storing the words spot, pots, act,
and cat, it inserts the strings, "opst spot",
"opst pots", "act act", and "act
cat" into the Binary Search Tree.
(use the function you wrote for Question 2b to do the sorting)
For example if "opst spot", "opst pots", "act act", and "act cat" were the only strings contained in the Binary Search Tree, and the user gave the word , "spot", the program should print "spot" and "pots".
Note: this option is finding all the anagrams of the given word, so you can't just use binary tree search (it only finds one entry). You can still use the binary tree search to find the first occurance, and then do a traversal of the subtree from that node to find all of the anagrams.
Important: Test your program on the file words.txt, which can found at http://www.csse.monash.edu.au/courseware/cse1303s/parta/prac/a5/words.txt, and find the anagrams of subessential.
As you can see, storing anagrams in a tree is not efficient, especially as you need to step through a number of nodes to find the anagrams of a word!
A better way of storing anagrams would be to store the sorted string of a word as a key in the Binary search tree, and inside the node
have a linked list of the words that form anagrams.
The list node and tree node structures would then look something like:
typedef struct ListNodeRec
{
char *word;
struct ListNodeRec next;
} ListNode;
typedef struct NodeRec
{
char *key;
ListNode* list;
struct NodeRec leftPtr;
struct NodeRec rightPtr;
} Node;
When you are inserting a new anagram into the tree, you need to search for the sorted string, and if it doesn't exist in the tree, create and insert a new node, with the sorted string as the key, and the original word as the first node in the list. If the sorted version of the word is in the tree, you simply need to add the word to the list inside the tree node where you found the sorted anagram.
For example, a node would look like:
Last modified: Thursday 26 June 2003 14:42:13