SlideShare a Scribd company logo
1
Applications of BSTs and Linked Lists
•Review the project online
•Determine requirements
•Come up with an overall design
•Carry out the implementation
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.
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( ).
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)
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.
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!)
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)
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
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
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
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
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)
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
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
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
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”)
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.
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.

More Related Content

Similar to Applications of bsts and linked lists

Vinci2011会议演讲PPT
Vinci2011会议演讲PPTVinci2011会议演讲PPT
Vinci2011会议演讲PPTdasiyjun
 
Following is an example of creating a database and then adding.docx
Following is an example of creating a database and then adding.docxFollowing is an example of creating a database and then adding.docx
Following is an example of creating a database and then adding.docx
budbarber38650
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
SSN College of Engineering, Kalavakkam
 
Dsa unit 1
Dsa unit 1Dsa unit 1
Dsa unit 1
thamizh arasi
 
Social Networks of Performance
Social Networks of PerformanceSocial Networks of Performance
Social Networks of Performance
Penn State University
 
Webinar 11-13-14 - DIY E-Resources Management: Basics of Information Architec...
Webinar 11-13-14 - DIY E-Resources Management: Basics of Information Architec...Webinar 11-13-14 - DIY E-Resources Management: Basics of Information Architec...
Webinar 11-13-14 - DIY E-Resources Management: Basics of Information Architec...
NASIG
 
Actively Learning to Rank Semantic Associations for Personalized Contextual E...
Actively Learning to Rank Semantic Associations for Personalized Contextual E...Actively Learning to Rank Semantic Associations for Personalized Contextual E...
Actively Learning to Rank Semantic Associations for Personalized Contextual E...
Federico Bianchi
 

Similar to Applications of bsts and linked lists (8)

Vinci2011会议演讲PPT
Vinci2011会议演讲PPTVinci2011会议演讲PPT
Vinci2011会议演讲PPT
 
Following is an example of creating a database and then adding.docx
Following is an example of creating a database and then adding.docxFollowing is an example of creating a database and then adding.docx
Following is an example of creating a database and then adding.docx
 
Database Management System
Database Management SystemDatabase Management System
Database Management System
 
Relational databases
Relational databasesRelational databases
Relational databases
 
Dsa unit 1
Dsa unit 1Dsa unit 1
Dsa unit 1
 
Social Networks of Performance
Social Networks of PerformanceSocial Networks of Performance
Social Networks of Performance
 
Webinar 11-13-14 - DIY E-Resources Management: Basics of Information Architec...
Webinar 11-13-14 - DIY E-Resources Management: Basics of Information Architec...Webinar 11-13-14 - DIY E-Resources Management: Basics of Information Architec...
Webinar 11-13-14 - DIY E-Resources Management: Basics of Information Architec...
 
Actively Learning to Rank Semantic Associations for Personalized Contextual E...
Actively Learning to Rank Semantic Associations for Personalized Contextual E...Actively Learning to Rank Semantic Associations for Personalized Contextual E...
Actively Learning to Rank Semantic Associations for Personalized Contextual E...
 

More from Syed Zaid Irshad

Operating System.pdf
Operating System.pdfOperating System.pdf
Operating System.pdf
Syed Zaid Irshad
 
DBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_SolutionDBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_Solution
Syed Zaid Irshad
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
Syed Zaid Irshad
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
Syed Zaid Irshad
 
Professional Issues in Computing
Professional Issues in ComputingProfessional Issues in Computing
Professional Issues in Computing
Syed Zaid Irshad
 
Reduce course notes class xi
Reduce course notes class xiReduce course notes class xi
Reduce course notes class xi
Syed Zaid Irshad
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
Syed Zaid Irshad
 
Introduction to Database
Introduction to DatabaseIntroduction to Database
Introduction to Database
Syed Zaid Irshad
 
C Language
C LanguageC Language
C Language
Syed Zaid Irshad
 
Flowchart
FlowchartFlowchart
Flowchart
Syed Zaid Irshad
 
Algorithm Pseudo
Algorithm PseudoAlgorithm Pseudo
Algorithm Pseudo
Syed Zaid Irshad
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
Syed Zaid Irshad
 
ICS 2nd Year Book Introduction
ICS 2nd Year Book IntroductionICS 2nd Year Book Introduction
ICS 2nd Year Book Introduction
Syed Zaid Irshad
 
Security, Copyright and the Law
Security, Copyright and the LawSecurity, Copyright and the Law
Security, Copyright and the Law
Syed Zaid Irshad
 
Computer Architecture
Computer ArchitectureComputer Architecture
Computer Architecture
Syed Zaid Irshad
 
Data Communication
Data CommunicationData Communication
Data Communication
Syed Zaid Irshad
 
Information Networks
Information NetworksInformation Networks
Information Networks
Syed Zaid Irshad
 
Basic Concept of Information Technology
Basic Concept of Information TechnologyBasic Concept of Information Technology
Basic Concept of Information Technology
Syed Zaid Irshad
 
Introduction to ICS 1st Year Book
Introduction to ICS 1st Year BookIntroduction to ICS 1st Year Book
Introduction to ICS 1st Year Book
Syed Zaid Irshad
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
Syed Zaid Irshad
 

More from Syed Zaid Irshad (20)

Operating System.pdf
Operating System.pdfOperating System.pdf
Operating System.pdf
 
DBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_SolutionDBMS_Lab_Manual_&_Solution
DBMS_Lab_Manual_&_Solution
 
Data Structure and Algorithms.pptx
Data Structure and Algorithms.pptxData Structure and Algorithms.pptx
Data Structure and Algorithms.pptx
 
Design and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptxDesign and Analysis of Algorithms.pptx
Design and Analysis of Algorithms.pptx
 
Professional Issues in Computing
Professional Issues in ComputingProfessional Issues in Computing
Professional Issues in Computing
 
Reduce course notes class xi
Reduce course notes class xiReduce course notes class xi
Reduce course notes class xi
 
Reduce course notes class xii
Reduce course notes class xiiReduce course notes class xii
Reduce course notes class xii
 
Introduction to Database
Introduction to DatabaseIntroduction to Database
Introduction to Database
 
C Language
C LanguageC Language
C Language
 
Flowchart
FlowchartFlowchart
Flowchart
 
Algorithm Pseudo
Algorithm PseudoAlgorithm Pseudo
Algorithm Pseudo
 
Computer Programming
Computer ProgrammingComputer Programming
Computer Programming
 
ICS 2nd Year Book Introduction
ICS 2nd Year Book IntroductionICS 2nd Year Book Introduction
ICS 2nd Year Book Introduction
 
Security, Copyright and the Law
Security, Copyright and the LawSecurity, Copyright and the Law
Security, Copyright and the Law
 
Computer Architecture
Computer ArchitectureComputer Architecture
Computer Architecture
 
Data Communication
Data CommunicationData Communication
Data Communication
 
Information Networks
Information NetworksInformation Networks
Information Networks
 
Basic Concept of Information Technology
Basic Concept of Information TechnologyBasic Concept of Information Technology
Basic Concept of Information Technology
 
Introduction to ICS 1st Year Book
Introduction to ICS 1st Year BookIntroduction to ICS 1st Year Book
Introduction to ICS 1st Year Book
 
Using the set operators
Using the set operatorsUsing the set operators
Using the set operators
 

Recently uploaded

weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
Pratik Pawar
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Soumen Santra
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
Kamal Acharya
 
Steel & Timber Design according to British Standard
Steel & Timber Design according to British StandardSteel & Timber Design according to British Standard
Steel & Timber Design according to British Standard
AkolbilaEmmanuel1
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Sreedhar Chowdam
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
top1002
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
thanhdowork
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
Kamal Acharya
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Dr.Costas Sachpazis
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
Massimo Talia
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
bakpo1
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
ydteq
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
manasideore6
 

Recently uploaded (20)

weather web application report.pdf
weather web application report.pdfweather web application report.pdf
weather web application report.pdf
 
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTSHeap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
Heap Sort (SS).ppt FOR ENGINEERING GRADUATES, BCA, MCA, MTECH, BSC STUDENTS
 
Student information management system project report ii.pdf
Student information management system project report ii.pdfStudent information management system project report ii.pdf
Student information management system project report ii.pdf
 
Steel & Timber Design according to British Standard
Steel & Timber Design according to British StandardSteel & Timber Design according to British Standard
Steel & Timber Design according to British Standard
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&BDesign and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
Design and Analysis of Algorithms-DP,Backtracking,Graphs,B&B
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
Basic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparelBasic Industrial Engineering terms for apparel
Basic Industrial Engineering terms for apparel
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
RAT: Retrieval Augmented Thoughts Elicit Context-Aware Reasoning in Long-Hori...
 
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
Cosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdfCosmetic shop management system project report.pdf
Cosmetic shop management system project report.pdf
 
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
Sachpazis:Terzaghi Bearing Capacity Estimation in simple terms with Calculati...
 
Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024Nuclear Power Economics and Structuring 2024
Nuclear Power Economics and Structuring 2024
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
一比一原版(SFU毕业证)西蒙菲莎大学毕业证成绩单如何办理
 
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
一比一原版(UofT毕业证)多伦多大学毕业证成绩单如何办理
 
Fundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptxFundamentals of Induction Motor Drives.pptx
Fundamentals of Induction Motor Drives.pptx
 

Applications of bsts and linked lists

  • 1. 1 Applications of BSTs and Linked Lists •Review the project online •Determine requirements •Come up with an overall design •Carry out the implementation
  • 2. 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.
  • 3. 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( ).
  • 4. 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)
  • 5. 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.
  • 6. 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!)
  • 7. 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)
  • 8. 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
  • 9. 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
  • 10. 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
  • 11. 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
  • 12. 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)
  • 13. 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
  • 14. 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
  • 15. 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
  • 16. 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”)
  • 17. 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.
  • 18. 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.