SlideShare a Scribd company logo
1 of 4
Download to read offline
s
Family Tree in Java
This program is finding its parent grandparent and siblings. There are
methods to find the grandchildren, most similar descendant, etc.
import java.util.ArrayList;
import java.util.List;
public class FamilyTree {
Tree t;
public FamilyTree(Tree t) {
this.t = t;
}
public String getParent(Node person) {
if (person.getParent() == null)
return "No parent";
return (String) person.getParent().getElement();
}
public String getGrandparent(Node person) {
if (person.getParent() == null
|| person.getParent().getParent() == null)
return "No grandparent";
return (String) person.getParent().getParent().getElement();
}
public List<String> getChildren(Node person) {
List<String> children = new ArrayList<String>();
for (Node child : person.getChildren()) {
children.add((String) child.getElement());
}
return children;
}
public List<String> getGrandchildren(Node person) {
List<String> grandchildren = new ArrayList<String>();
for (Node child : person.getChildren()) {
grandchildren.addAll(getChildren(child));
}
return grandchildren;
}
public List<String> getSiblings(Node person) {
List<String> siblings = new ArrayList<String>();
if (person.getParent() == null)
return siblings;
for (Node sibling : person.getParent().getChildren()) {
if (sibling != person)
siblings.add((String) sibling.getElement());
}
return siblings;
}
public int getSimilarityScore(String a, String b) {
int score = 0;
int minLength = Math.min(a.length(), b.length());
for (int i = 0; i < minLength; i++) {
if (a.charAt(i) == b.charAt(i))
score++;
else
break;
}
return score;
}
public String mostSimilarDescendant(Node person) {
if (person.getChildren().isEmpty())
return null;
String mostSimilar = null;
int mostSimilarScore = -1;
for (String child : getChildren(person)) {
int score = getSimilarityScore((String)
person.getElement(), child);
if (score > mostSimilarScore) {
mostSimilar = child;
mostSimilarScore = score;
}
}
for (String grandchild : getGrandchildren(person)) {
int score = getSimilarityScore((String)
person.getElement(), grandchild);
if (score > mostSimilarScore) {
mostSimilar = grandchild;
mostSimilarScore = score;
}
}
return mostSimilar;
}
}

More Related Content

What's hot

Ab initio is one of the popular etl tools that is in the market
Ab initio is one of the popular etl tools that is in the marketAb initio is one of the popular etl tools that is in the market
Ab initio is one of the popular etl tools that is in the market
eercreddy
 
Waffle: A feature switch/flag/toggle application for Django
Waffle: A feature switch/flag/toggle application for DjangoWaffle: A feature switch/flag/toggle application for Django
Waffle: A feature switch/flag/toggle application for Django
tobych
 

What's hot (6)

Routing algorithm
Routing algorithmRouting algorithm
Routing algorithm
 
Ab initio is one of the popular etl tools that is in the market
Ab initio is one of the popular etl tools that is in the marketAb initio is one of the popular etl tools that is in the market
Ab initio is one of the popular etl tools that is in the market
 
Modern database management system chapter 1
Modern database management system chapter 1Modern database management system chapter 1
Modern database management system chapter 1
 
Issues in Data Link Layer
Issues in Data Link LayerIssues in Data Link Layer
Issues in Data Link Layer
 
Waffle: A feature switch/flag/toggle application for Django
Waffle: A feature switch/flag/toggle application for DjangoWaffle: A feature switch/flag/toggle application for Django
Waffle: A feature switch/flag/toggle application for Django
 
Historical Evolution of RDBMS
Historical Evolution of RDBMSHistorical Evolution of RDBMS
Historical Evolution of RDBMS
 

Similar to Family tree in java

Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdfDesign, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
trishacolsyn25353
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
NicholasflqStewartl
 
Scala - fra newbie til ninja på en time
Scala - fra newbie til ninja på en timeScala - fra newbie til ninja på en time
Scala - fra newbie til ninja på en time
karianneberg
 
Bài tập và bài tập nâng cao của cây AVL.docx
Bài tập và bài tập nâng cao của cây  AVL.docxBài tập và bài tập nâng cao của cây  AVL.docx
Bài tập và bài tập nâng cao của cây AVL.docx
LNhi89
 
I need help finishing this code in JavaYou will need to create t.pdf
I need help finishing this code in JavaYou will need to create t.pdfI need help finishing this code in JavaYou will need to create t.pdf
I need help finishing this code in JavaYou will need to create t.pdf
allurafashions98
 
Write the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdfWrite the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdf
fckindswear
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical Groovy
Codecamp Romania
 
2.1 Recap From Day One
2.1 Recap From Day One2.1 Recap From Day One
2.1 Recap From Day One
retronym
 

Similar to Family tree in java (20)

1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 
Scala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 WorldScala vs Java 8 in a Java 8 World
Scala vs Java 8 in a Java 8 World
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 
1.1 motivation
1.1 motivation1.1 motivation
1.1 motivation
 
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdfDesign, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
Design, implement, test(In Java ) a doubly linked list ADT, using DL.pdf
 
BEKK Teknologiradar - Kotlin
BEKK Teknologiradar - KotlinBEKK Teknologiradar - Kotlin
BEKK Teknologiradar - Kotlin
 
Having a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdfHaving a problem figuring out where my errors are- The code is not run.pdf
Having a problem figuring out where my errors are- The code is not run.pdf
 
Scala - fra newbie til ninja på en time
Scala - fra newbie til ninja på en timeScala - fra newbie til ninja på en time
Scala - fra newbie til ninja på en time
 
Bài tập và bài tập nâng cao của cây AVL.docx
Bài tập và bài tập nâng cao của cây  AVL.docxBài tập và bài tập nâng cao của cây  AVL.docx
Bài tập và bài tập nâng cao của cây AVL.docx
 
Scala - THE language for Big Data
Scala - THE language for Big DataScala - THE language for Big Data
Scala - THE language for Big Data
 
I need help finishing this code in JavaYou will need to create t.pdf
I need help finishing this code in JavaYou will need to create t.pdfI need help finishing this code in JavaYou will need to create t.pdf
I need help finishing this code in JavaYou will need to create t.pdf
 
Write the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdfWrite the Java source code necessary to build a solution for the pro.pdf
Write the Java source code necessary to build a solution for the pro.pdf
 
Rd
RdRd
Rd
 
Pure Kotlin Devoxx PL 2021
Pure Kotlin Devoxx PL 2021Pure Kotlin Devoxx PL 2021
Pure Kotlin Devoxx PL 2021
 
CodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical GroovyCodeCamp Iasi 10 march 2012 - Practical Groovy
CodeCamp Iasi 10 march 2012 - Practical Groovy
 
From java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+kFrom java to kotlin beyond alt+shift+cmd+k
From java to kotlin beyond alt+shift+cmd+k
 
Scala == Effective Java
Scala == Effective JavaScala == Effective Java
Scala == Effective Java
 
2.1 Recap From Day One
2.1 Recap From Day One2.1 Recap From Day One
2.1 Recap From Day One
 
C# 6.0
C# 6.0C# 6.0
C# 6.0
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 

More from Programming Homework Help

More from Programming Homework Help (8)

Java Assignment Help
Java  Assignment  HelpJava  Assignment  Help
Java Assignment Help
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
 
C# Assignmet Help
C# Assignmet HelpC# Assignmet Help
C# Assignmet Help
 
Binary tree in java
Binary tree in javaBinary tree in java
Binary tree in java
 
Bank account in java
Bank account in javaBank account in java
Bank account in java
 
Mouse and Cat Game In C++
Mouse and Cat Game In C++Mouse and Cat Game In C++
Mouse and Cat Game In C++
 
Word games in c
Word games in cWord games in c
Word games in c
 
Card Games in C++
Card Games in C++Card Games in C++
Card Games in C++
 

Recently uploaded

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
MateoGardella
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
Chris Hunter
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
negromaestrong
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
MateoGardella
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
QucHHunhnh
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 

Recently uploaded (20)

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Paris 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activityParis 2024 Olympic Geographies - an activity
Paris 2024 Olympic Geographies - an activity
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 

Family tree in java

  • 1. s
  • 2. Family Tree in Java This program is finding its parent grandparent and siblings. There are methods to find the grandchildren, most similar descendant, etc. import java.util.ArrayList; import java.util.List; public class FamilyTree { Tree t; public FamilyTree(Tree t) { this.t = t; } public String getParent(Node person) { if (person.getParent() == null) return "No parent"; return (String) person.getParent().getElement(); } public String getGrandparent(Node person) { if (person.getParent() == null || person.getParent().getParent() == null) return "No grandparent"; return (String) person.getParent().getParent().getElement(); } public List<String> getChildren(Node person) { List<String> children = new ArrayList<String>(); for (Node child : person.getChildren()) { children.add((String) child.getElement()); } return children; } public List<String> getGrandchildren(Node person) { List<String> grandchildren = new ArrayList<String>(); for (Node child : person.getChildren()) {
  • 3. grandchildren.addAll(getChildren(child)); } return grandchildren; } public List<String> getSiblings(Node person) { List<String> siblings = new ArrayList<String>(); if (person.getParent() == null) return siblings; for (Node sibling : person.getParent().getChildren()) { if (sibling != person) siblings.add((String) sibling.getElement()); } return siblings; } public int getSimilarityScore(String a, String b) { int score = 0; int minLength = Math.min(a.length(), b.length()); for (int i = 0; i < minLength; i++) { if (a.charAt(i) == b.charAt(i)) score++; else break; } return score; } public String mostSimilarDescendant(Node person) { if (person.getChildren().isEmpty()) return null; String mostSimilar = null; int mostSimilarScore = -1; for (String child : getChildren(person)) { int score = getSimilarityScore((String) person.getElement(), child); if (score > mostSimilarScore) { mostSimilar = child; mostSimilarScore = score; } } for (String grandchild : getGrandchildren(person)) { int score = getSimilarityScore((String) person.getElement(), grandchild);
  • 4. if (score > mostSimilarScore) { mostSimilar = grandchild; mostSimilarScore = score; } } return mostSimilar; } }