Programming Assignment for Word Origins | LING 60, Assignments of Linguistics

Material Type: Assignment; Class: WORD ORIGINS; Subject: Linguistics; University: University of California - Santa Barbara; Term: Summer A 2009;

Typology: Assignments

Pre 2010

Uploaded on 08/31/2009

koofers-user-aq2
koofers-user-aq2 🇺🇸

10 documents

1 / 11

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 60
Programming Assignment #6
Due Date: July 30, 2009 (Noon). 90 Points.
Remember: Points will be deducted if turned in after the Due Date.
Deadline: August 3, 2009 (Noon)
Remember: No homework will be accepted after the Deadline
You must work on this assignment independently.
For this assignment you will write a C++ program to manipulate a set of people that
belong to sets of groups (or organizations). Each person has a name formed by at most 254
lower case English letters (without blanks). Each person has a telephone number formed by
at most 254 decimal numbers (without blanks). Similarly, each group has a name formed by
at most 254 lower case English letters (without blanks). Each group has a telephone number
formed by at most 254 decimal numbers (without blanks). We will define all the operations
for people and groups below. Each person must be represented by a derivation of classes
Tree and TreeNode which we call PeopleTree and PeopleTreeNode defined below. The same
holds for each group (GroupTree and GroupTreeNode).
Note that we do not keep multiple copies of any name of a person or name of a group.
The ordering of the names is alphabetical (like in a Word dictionary). If a name xis a prefix
of a name y, then xis in alphabetical order before name y. The same holds for groups.
For example, the name art is a prefix of the word arthur. Therefore art is in alphabetical
order before arthur. This is the ordering produced by type string when we use #include
<string>.
You will implement a base Class Tree for binary trees. An object of this class has a
pointer to the root node of the binary tree. This is an object of the class TreeNode. Each
TreeNode consists of a string (for a name), and two pointers to TreeNodes (leftchild and
rightchild). The order in the tree is as follows: for every TreeNode T all the TreeNodes in
the left subtree have a name that is in alphabetical order smaller than the one in T, and
all the TreeNodes in the right subtree have a name that is in alphabetical order larger than
the one in T. You will need to modify the Class Tree and Class TreeNode that I give
you at the end of this assignment and implement appropriate member functions, including
constructors and destructor, overloading <<. We derive two classes from each of these two
classes. One is for people and the other is for groups. Each tree node will be augmented by
including a telephone number which is a string of at most 254 decimal numbers. The tree
nodes for the people will have a pointer to an object of the class List (this is another class).
This node has a pointer to an object of class ListNode (this is another class). A list node
has just a pointer to a PeopleTreeNode or a GroupTreeNode. In the tree nodes for people
we use this list to represent all the groups that a person has joined. The list is “sorted” in
alphabetical order in the sense that the first element in the list points to the first group in
alphabetical order (that this person has joined). The second element in the list points to
the second group in alphabetical order (that this person has joined). And so on. For the
tree nodes for groups, we have a similar situation, except that the objects in the list point to
objects describing people. The list must also be ordered in alphabetical order. The classes
List and ListNode are similar to the ones in the previous assignment (just single linked
list).
1
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Programming Assignment for Word Origins | LING 60 and more Assignments Linguistics in PDF only on Docsity!

CS 60

Programming Assignment # Due Date: July 30, 2009 (Noon). 90 Points. Remember: Points will be deducted if turned in after the Due Date. Deadline: August 3, 2009 (Noon) Remember: No homework will be accepted after the Deadline You must work on this assignment independently.

For this assignment you will write a C++ program to manipulate a set of people that belong to sets of groups (or organizations). Each person has a name formed by at most 254 lower case English letters (without blanks). Each person has a telephone number formed by at most 254 decimal numbers (without blanks). Similarly, each group has a name formed by at most 254 lower case English letters (without blanks). Each group has a telephone number formed by at most 254 decimal numbers (without blanks). We will define all the operations for people and groups below. Each person must be represented by a derivation of classes Tree and TreeNode which we call PeopleTree and PeopleTreeNode defined below. The same holds for each group (GroupTree and GroupTreeNode). Note that we do not keep multiple copies of any name of a person or name of a group. The ordering of the names is alphabetical (like in a Word dictionary). If a name x is a prefix of a name y, then x is in alphabetical order before name y. The same holds for groups. For example, the name art is a prefix of the word arthur. Therefore art is in alphabetical order before arthur. This is the ordering produced by type string when we use #include . You will implement a base Class Tree for binary trees. An object of this class has a pointer to the root node of the binary tree. This is an object of the class TreeNode. Each TreeNode consists of a string (for a name), and two pointers to TreeNodes (leftchild and rightchild). The order in the tree is as follows: for every TreeNode T all the TreeNodes in the left subtree have a name that is in alphabetical order smaller than the one in T, and all the TreeNodes in the right subtree have a name that is in alphabetical order larger than the one in T. You will need to modify the Class Tree and Class TreeNode that I give you at the end of this assignment and implement appropriate member functions, including constructors and destructor, overloading <<. We derive two classes from each of these two classes. One is for people and the other is for groups. Each tree node will be augmented by including a telephone number which is a string of at most 254 decimal numbers. The tree nodes for the people will have a pointer to an object of the class List (this is another class). This node has a pointer to an object of class ListNode (this is another class). A list node has just a pointer to a PeopleTreeNode or a GroupTreeNode. In the tree nodes for people we use this list to represent all the groups that a person has joined. The list is “sorted” in alphabetical order in the sense that the first element in the list points to the first group in alphabetical order (that this person has joined). The second element in the list points to the second group in alphabetical order (that this person has joined). And so on. For the tree nodes for groups, we have a similar situation, except that the objects in the list point to objects describing people. The list must also be ordered in alphabetical order. The classes List and ListNode are similar to the ones in the previous assignment (just single linked list).

In this assignment you will be performing operations on a set of people and a set of groups. The operations are given below. For this assignment you will also implement appropriate constructors and destructor for for all your Classes. These functions should be similar in functionality to the ones discussed in class (see also the progs.html web page). You must also implement the following member functions for the class Tree.

  • Insert(x): Insert the TreeNode pointed by x in the tree. If it is there, then do nothing.
  • Member(x): returns the boolean value true if the word represented in string x is in the Tree; and returns the boolean value false otherwise. The word stored in x has one or more characters.
  • Overloading <<: Overload << so that one prints all the words stored in the Tree, one word per line (in alphabetical order).
  • Erase(): deletes all words stored in the Tree and it leaves it empty. Make sure that you return all the TreeNodes and the dynamically allocated space associated with the TreeNodes. Even the ones for the derived classes when returning one such node.
  • Cardinality(): counts and prints the total number of words stored in the Tree.

Big Hint: The easiest way to implement some of these functions recursive. For example, traversing the whole tree in alphabetical order can be easily done by a procedure similar to recursive inorder traversal (remember CMPSC 20). You must implement your functions so that they solve the problems efficiently. Your main program will read in a set of commands to operate on a set of people and groups. The format for the input lines are given below. Everything is typed as indicated except for any thing surrounded by < and >. For example , , , and . The fields , , and represent a string of letters, the field represent a string of numbers, and is a positive integer. You may assume that all the INPUT IS CORRECT. I.e., the input follow the format given below.

  • insert-person This input line indicates that you must insert the person with name and telephone phone to the set of people. If the name appears in the set then the command is ignored. Otherwise you must add a new person to the set of people. Initially the person is not a member of any group.
  • member-person You must print true if the person with name name is in the set of people, and false otherwise.
  • delete-person This command line indicates you must delete the person with name name from the set of people. If the person is not in the set then the operation will do nothing. Note that the person must be deleted from all the groups it belongs.
  • print-person Print all the groups for person in alphabetical order (one per line). If does not exist, the operation will do nothing.
  • person Print all the people that belong to or more groups.
  • group Print all the groups with or more members.
  • quit Your program must end after releasing all the space dynamically allocated.

You may assume that each input line is of the form indicated above with one blank separating every pair of adjacent words. You may also assume that there is a \n after the last character in each line. There is a simple program in the class web page that you may easily generalize to read the input lines.

Example: Processing of commands.

INSTRUCTION DICTIONARIES AFTER INSTRUCTION

insert-person john 1239854 people={john} groups={} insert-person jack 3234657 people={jack,john} groups={} insert-person jane 4578345 people={jack,jane,john} groups={} insert-person ron 1255236 people={jack,jane,john,ron} groups={} insert-group acm 2374833 people={jack,jane,john,ron} groups={acm} insert-group ieee 8945756 people={jack,jane,john,ron} groups={acm,ieee} insert-group acm 6745684 people={jack,jane,john,ron} groups={acm,ieee} insert-group orsa 3489292 people={jack,jane,john,ron} groups={acm,ieee,orsa} insert-group golf 8348484 people={jack,jane,john,ron} groups={acm,ieee,golf,orsa} member-group acm people={jack,jane,john,ron} groups={acm,ieee,golf,orsa}

prints true member-group cam people={jack,jane,john,ron} groups={acm,ieee,golf,orsa} prints false member-group rron people={jack,jane,john,ron} groups={acm,ieee,golf,orsa} prints false member-group ron people={jack,jane,john,ron} groups={acm,ieee,golf,orsa} prints true print group people={jack,jane,john,ron} groups={acm,ieee,golf,orsa} The groups are: acm ieee golf join jack acm people={jack (acm),jane,john,ron} groups={acm (jack),ieee,golf,orsa} join jack ieee people={jack (acm,ieee),jane,john,ron} groups={acm (jack),ieee (jack),golf,orsa} join golf jack people={jack (acm,ieee),jane,john,ron} groups={acm (jack),ieee (jack),golf,orsa} join jane acm people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} print-group acm people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} The people in group acm are: jack jane print-person jack people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} jack is a member of groups: acm ieee print-person acm people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} cardinality-people people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} There are 4 people cardinality-group people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} There are 4 groups person 2 people={jack (acm,ieee),jane (acm),john,ron} groups={acm (jack,jane),ieee (jack),golf,orsa} The following people belong to 2 or more groups:

TreeNode::TreeNode(string w="") { name=w; leftchild=rightchild=0; }

void TreeNode::SetName(string w) {name=w;}

void TreeNode::SetRight(TreeNode* j) {rightchild=j;}

void TreeNode::SetLeft(TreeNode* j) {leftchild=j;}

string TreeNode::GetName() {return name;}

TreeNode* TreeNode::GetLeft() {return leftchild;}

TreeNode* TreeNode::GetRight() {return rightchild;}

class PeopleTreeNode : public TreeNode { public: void SetPhone(string& w) {phone=w; return;} string GetPhone() {return phone;} void SetGroups(List * ptr){} List * GetGroups(){} PeopleTreeNode(string& n, string& p) : TreeNode(n) {SetGroups(0); SetPhone(p); } private: string phone; List *groups; };

class GroupTreeNode : public TreeNode { public: void SetPhone(string& w) {phone=w; return;}

string GetPhone() {return phone;} void SetPeople(List * ptr){} List * GetPeople(){} GroupTreeNode(string& n, string& p) : TreeNode(n) {SetPeople(0); SetPhone(p); } private: string phone; List *people; };

class Tree { public: Tree() ; bool Insert(TreeNode); bool Insert(TreeNode, TreeNode); virtual void Insert(string& a, string& b){}; bool Member(string); bool Member(TreeNode, string); void Readlist(); private: TreeNode *root; };

Tree::Tree(){ root = 0; }

bool Tree::Insert(TreeNode * newnode) { TreeNode *current = root; if (current==0) { root = newnode; return true; } return Insert(root,newnode); }

bool Tree::Insert(TreeNode *ptr, TreeNode *newnode) { if (newnode->GetName() < ptr->GetName()) {if (ptr->GetLeft() == 0) {ptr->SetLeft(newnode); return true; }

public: void Insert(string& n, string& p) {PeopleTreeNode * ptr; ptr = new PeopleTreeNode(n,p); if (not Tree::Insert(ptr)) delete ptr; } People() : Tree(){}; };

class Group : public Tree { public: void Insert(string& n, string& p) {GroupTreeNode * ptr; ptr = new GroupTreeNode(n,p); if (not Tree::Insert(ptr)) delete ptr; } Group() : Tree(){}; };

main() { int numtests; string newelem;

// This is a test program to see how it works. // You need to modify a lot of stuff for hw6. // We will discuss this program in class on Thursday.

People t; cout << "Read in people names to store in the tree" << endl; t.Readlist();

Group p; cout << "Read in group names to store in the tree" << endl; p.Readlist();

cout << endl; cout << "num of tests for the people tree" << endl; cin >> numtests; for ( int i = 1 ; i <= numtests ; i = i + 1 ) { cout << "Input Person name to see if it is in the people group" << endl; cin >> newelem; cout << newelem << "IS A MEMBER?"

<< t.Member(newelem) << endl; } cout << endl; cout << "num of tests for the groups tree" << endl; cin >> numtests; for ( int i = 1 ; i <= numtests ; i = i + 1 ) { cout << "Input Group name to see if it is in the group tree" << endl; cin >> newelem; cout << newelem << "IS A MEMBER?" << p.Member(newelem) << endl; } }