Last active
November 8, 2018 08:47
-
-
Save adityaagungp/a7cff7768daa2f02de9ea0c612224117 to your computer and use it in GitHub Desktop.
Recursion Data Structure Training
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Article { | |
private String title; | |
private List<Article> children = new ArrayList<>(); | |
Article(String title, List<Article> children) { | |
this.title = title; | |
this.children = children; | |
} | |
String getTitle() { | |
return title; | |
} | |
List<Article> getChildren() { | |
return children; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ArticleMain { | |
public static void main(String[] args) { | |
Article home = new Article("Home", new ArrayList<Article>() {{ | |
add(new Article("Inspiration", new ArrayList<Article>() {{ | |
add(new Article("Lifestyle", new ArrayList<Article>() {{ | |
add(new Article("Health", new ArrayList<>())); | |
add(new Article("Wealth", new ArrayList<>())); | |
}})); | |
add(new Article("Foodie", new ArrayList<Article>() {{ | |
add(new Article("Domestics", new ArrayList<>())); | |
}})); | |
add(new Article("Accessories", new ArrayList<>())); | |
}})); | |
add(new Article("News", new ArrayList<Article>() {{ | |
add(new Article("Sports", new ArrayList<Article>() {{ | |
add(new Article("Football", new ArrayList<Article>() {{ | |
add(new Article("Premier League", new ArrayList<>())); | |
add(new Article("La Liga", new ArrayList<>())); | |
}})); | |
add(new Article("Basketball", new ArrayList<>())); | |
add(new Article("Athletics", new ArrayList<>())); | |
add(new Article("Baseball", new ArrayList<>())); | |
add(new Article("Chess", new ArrayList<>())); | |
}})); | |
add(new Article("Fashion", new ArrayList<>())); | |
add(new Article("Awards", new ArrayList<>())); | |
add(new Article("Events", new ArrayList<>())); | |
}})); | |
add(new Article("Gallery", new ArrayList<Article>() {{ | |
add(new Article("Photos", new ArrayList<Article>(){{ | |
add(new Article("Night Art", new ArrayList<>())); | |
add(new Article("Brand Ambassadors", new ArrayList<>())); | |
}})); | |
add(new Article("Videos", new ArrayList<>())); | |
}})); | |
add(new Article("About Us", new ArrayList<Article>() {{ | |
add(new Article("Contact Us", new ArrayList<>())); | |
add(new Article("Help", new ArrayList<>())); | |
}})); | |
}}); | |
//Create a method to print the article tree | |
//What are the parameters? | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Home | |
Inspiration | |
Lifestyle | |
Health | |
Wealth | |
Foodie | |
Domestics | |
Accessories | |
News | |
Sports | |
Football | |
Premier League | |
La Liga | |
Basketball | |
Athletics | |
Baseball | |
Chess | |
Fashion | |
Awards | |
Events | |
Gallery | |
Photos | |
Night Art | |
Brand Ambassadors | |
Videos | |
About Us | |
Contact Us | |
Help |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Robber { | |
String name; | |
int diamond; | |
Robber[] underlings; | |
Robber(String name, int diamond, Robber[] underlings) { | |
this.name = name; | |
this.diamond = diamond; | |
this.underlings = underlings; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class RobberMain { | |
public static void main(String[] args) { | |
Robber boss = new Robber("Ali", 10, new Robber[]{ | |
new Robber("Andy", 20, new Robber[]{ | |
new Robber("Dino", 5, new Robber[]{ | |
new Robber("Fang", 3, new Robber[]{}), | |
new Robber("George", 2, new Robber[]{ | |
new Robber("Heidi", 7, new Robber[]{}) | |
}) | |
}), | |
new Robber("Gaby", 7, new Robber[]{}) | |
}), | |
new Robber("Boy", 9, new Robber[]{ | |
new Robber("Danny", 9, new Robber[]{}), | |
new Robber("Eddy", 11, new Robber[]{ | |
new Robber("Harry", 10, new Robber[]{}), | |
new Robber("Irene", 4, new Robber[]{}) | |
}), | |
new Robber("Elise", 3, new Robber[]{}) | |
}), | |
new Robber("Celia", 11, new Robber[]{}) | |
}); | |
System.out.println("Total diamonds obtained: " + getTotalDiamonds(boss)); | |
System.out.println("Total command level of the syndicate: " + getCommandLevel(boss)); | |
} | |
private static int getTotalDiamonds(Robber robber) { | |
if (robber.underlings.length == 0) { | |
return robber.diamond; | |
} else { | |
int total = robber.diamond; | |
for (Robber r: robber.underlings) { | |
total += getTotalDiamonds(r); | |
} | |
return total; | |
} | |
//return 0; | |
} | |
private static int getCommandLevel(Robber robber) { | |
if (robber.underlings.length == 0) { | |
return 0; | |
} else { | |
int maxOrder = 0; | |
for (Robber r: robber.underlings) { | |
int temp = getCommandLevel(r); | |
if (temp > maxOrder) { | |
maxOrder = temp; | |
} | |
} | |
return 1 + maxOrder; | |
} | |
//return 0; | |
} | |
private static int getMostDiamond(Robber robber) { | |
if (robber.underlings.length == 0) { | |
return robber.diamond; | |
} else { | |
int maxDiamond = robber.diamond; | |
for (Robber r: robber.underlings) { | |
int temp = getMostDiamond(r); | |
if (temp > maxDiamond) { | |
maxDiamond = temp; | |
} | |
} | |
return maxDiamond; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment