19
CS 103 1 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 1031 Project 3: Applications of BSTs and Linked Lists Review the project online Determine requirements Come up with an overall design Carry out the

Embed Size (px)

Citation preview

Page 1: 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

CS 103 1

Project 3: Applications of BSTs and Linked Lists

• Review the project online

• Determine requirements

• Come up with an overall design

• Carry out the implementation

Page 2: 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

CS 103 2

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.

Page 3: 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

CS 103 3

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( ).

Page 4: 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

CS 103 4

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)

Page 5: 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

CS 103 5

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.

Page 6: 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

CS 103 6

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<a)” with “if (x.getSSN( )<a.getSSN( ))”– You only need to include the insert and search member

functions in Tree. That is, you do not need the remove( ) member function (what a relief!)

Page 7: 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

CS 103 7

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)

Page 8: 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

CS 103 8

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

Page 9: 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

CS 103 9

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

Page 10: 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

CS 103 10

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

Page 11: 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

CS 103 11

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

Page 12: 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

CS 103 12

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)

Page 13: 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

CS 103 13

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

Page 14: 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

CS 103 14

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

Page 15: 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

CS 103 15

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

Page 16: 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

CS 103 16

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”)

Page 17: 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

CS 103 17

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.

Page 18: 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

CS 103 18

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.

Page 19: 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

CS 103 19

Good Luck!