Presentation on theme: "CS 1031 Project 3: Applications of BSTs and Linked Lists Review the project online Determine requirements Come up with an overall design Carry out the."— Presentation transcript:
CS 1031 Project 3: Applications of BSTs and Linked Lists Review the project online Determine requirements Come up with an overall design Carry out the implementation
CS 1032 A Class Person The community is a group of individuals For each individual (i.e., person), we are provided a lot of personal information. Also, many of the queries can be answered directly from the provided personal information THEREFORE: It make a lot of sense to define a class called Person –It should have the data pieces provided in the paragraph of a given person in the input file –It should provide accessors (get functions) to return the values of the data pieces.
CS 1033 The Person Class (Contd.) The variable members should then be: SSN, firstName, lastName, the father’s SSN, the mother’s SSN, and the list of friends (more on the list of friends later) The constructor should take as input the data pieces provided in the input file The accessors: getSSN( ), getFullName( ), getFatherSSN( ), getMotherSSN( ), getFriends( ).
CS 1034 The Community Data Structure Nearly all of the queries (all but one) specify the SSN of the person in question To be able to answer such queries, the Person (object) whose SSN is the specified SSN must be searched for Therefore, the Persons of the community must be organized into some data structure that provides efficient operations for search (and for insert, because insert is used to fill the data structure)
CS 1035 The Community Data Structure (Contd.) We studied two structures that support general insert and search operations –Linked lists –Binary search trees (BSTs) So, you can use either one of them. In either case, each node should store one Person instance, and the key of each node should be the SSN of the corresponding Person Note that we also learned that searching in BSTs is a lot faster than in linked lists Therefore, it is better to represent the community (group of persons) as a BST.
CS 1036 The Community as a BST If you decide to use a BST to represent the community: –The datatype inside the Tree class and inside the TreeNode class in the notes must be changed as: typedef Person datatype; –Eliminate the default value from the TreeNode constructor –Inside the search and insert methods of the Tree class: Replace “if (a == x)” with “if (a.getSSN( )==x.getSSN( ) )” Also, replace “if (x
CS 1037 First Step of Query Processing For any query involving a SSN, the first step should be to search the community data structure for the node whose key is the specified SSN This finds the corresponding Person object (Call it P)
CS 1038 Family-Related Queries (Name, Mother, Father) NAME-OF SSN // use P.getFullName( ) MOTHER-OF SSN –Search for SSN, getting the person P –Let motherSSN = P.getMotherSSN( ) –Search for motherSSN in the community structure, getting the Person M –Return M.getFullName( ) FATHER-OF SSN // similar to MOTHER-OF
CS 1039 Family-Related Queries (CHILDREN-OF SSN0) Traverse the data structure of the community (either tree traversal if BST, or list scanning if linked list) For each node visited, check if the motherSSN or the fatherSSN is SSN0 –If a node matches, print out the full name of the corresponding person –If a node does not match, ignore it
CS Family-Related Queries (FULL-SIBLINGS-OF SSN0) 1.Get the fatherSSN and motherSSN of the person whose SSN is SSN0 2.Traverse the data structure of the community 3.For each node visited, check if the stored Person’s mother and father have SSNs matching those found in step 1. –If a node matches, print out the full name of the corresponding person –If a node does not match, ignore it
CS Family-Related Queries (HALF-SIBLINGS-OF SSN0) Same as FULL-SIBLINGS-OF SSN0 except that during traversal, one of the parents must match and the other must not match
CS The Friends-Related Queries To find the inverse friends of a person P, we have to traverse the community data structure looking for Persons in whose list of friends P belongs –This can be done if the Person class provides a member function isYourFriend(ssn) that returns true if the input ssn is among the list of friends of the Person object. Finding mutual friends requires the same member function isYourFriend(ssn)
CS The Friends-Related Queries (INVERSE-FRIENDS-OF SSN0) Traverse the community data structure For each node N visited, check if SSN0 is a friend of N: call N.isYourFriend(SSN0). –For each N for which N.isYourFriend(SSN0) returns true, print the full name of the Person stored in N
CS The Friends-Related Queries (MUTUAL-FRIENDS-OF SSN0) Scan the list of friends of SSN0 For each friend f (identified by its SSN) – search for f in the community data structure to find the corresponding Person object (call it P) –if P. isYourFriend(SSN0) is true, print out the full name of P
CS The Friends-Related Queries ( WHO-HAS-MOST-MUTUAL-FRIENDS ) Traverse the community data structure For each node N visited, find it mutual friends, and record a count of them Return the largest number of mutual friends found
CS How to Represent the List of Friends of a Person The list of a friends inside the Person class should be a sub-structure that allows for: –Inserting a new friend (needed when reading the input file, especially the list of friends of each person) –Searching for a given ssn (needed to implement isYourFriend(ssn)) –Traversing (or scanning) the whole sub-structure (needed for “MUTUAL-FRIENDS-OF SSN”)
CS How to Represent the List of Friends of a Person (Contd.) Therefore, the sub-structure representing the list of friends of a Person must itself be a data structure that allows for fast insert( ) and search( ), as well as traversal/scanning So, here again (inside the Person class), you can have linked list or a BST to represent the list of friends. Either way, the data part of the friend-node need only contain the SSN of the friend.
CS Summary of Classes Needed A Person class A Person-Node, which is a TreeNode or a LinkeListNode (called simply Node in lecture6), whose data part is of type Person A class for the community, which can be a Tree class (a BST) or a List class A friends class, which can be a Tree class (a BST) or a List, to represent the list of SSNs of the friends of a Person A friend-Node class, whose data part is simply a SSN. It is used inside the friends class.