Advertisement
CR7CR7

DrinksTreeDsa

Jul 7th, 2023
803
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.45 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.List;
  3.  
  4. class Drink {
  5.     private String name;
  6.     private List<Drink> children;
  7.  
  8.     public Drink(String name) {
  9.         this.name = name;
  10.         this.children = new ArrayList<>();
  11.     }
  12.  
  13.     public void addChild(Drink drink) {
  14.         children.add(drink);
  15.     }
  16.  
  17.     public String getName() {
  18.         return name;
  19.     }
  20.  
  21.     public List<Drink> getChildren() {
  22.         return children;
  23.     }
  24. }
  25.  
  26. public class DrinkHierarchy {
  27.     public static void main(String[] args) {
  28.         // Create the drink hierarchy
  29.         Drink root = new Drink("Drinks");
  30.  
  31.         Drink hot = new Drink("Hot");
  32.         Drink cold = new Drink("Cold");
  33.         root.addChild(hot);
  34.         root.addChild(cold);
  35.  
  36.         Drink tea = new Drink("Tea");
  37.         Drink coffee = new Drink("Coffee");
  38.         hot.addChild(tea);
  39.         hot.addChild(coffee);
  40.  
  41.         Drink greenTea = new Drink("Green Tea");
  42.         Drink blackTea = new Drink("Black Tea");
  43.         tea.addChild(greenTea);
  44.         tea.addChild(blackTea);
  45.  
  46.         Drink americano = new Drink("Americano");
  47.         Drink latte = new Drink("Latte");
  48.         Drink cappuccino = new Drink("Cappuccino");
  49.         coffee.addChild(americano);
  50.         coffee.addChild(latte);
  51.         coffee.addChild(cappuccino);
  52.  
  53.         Drink nonAlcoholic = new Drink("Non-Alcoholic");
  54.         Drink alcoholic = new Drink("Alcoholic");
  55.         cold.addChild(nonAlcoholic);
  56.         cold.addChild(alcoholic);
  57.  
  58.         Drink cola = new Drink("Cola");
  59.         Drink fanta = new Drink("Fanta");
  60.         Drink soda = new Drink("Soda");
  61.         nonAlcoholic.addChild(cola);
  62.         nonAlcoholic.addChild(fanta);
  63.         nonAlcoholic.addChild(soda);
  64.  
  65.         Drink wine = new Drink("Wine");
  66.         Drink beer = new Drink("Beer");
  67.         Drink whisky = new Drink("Whisky");
  68.         alcoholic.addChild(wine);
  69.         alcoholic.addChild(beer);
  70.         alcoholic.addChild(whisky);
  71.  
  72.         // Print the drink hierarchy using pre-order traversal
  73.         printDrinkHierarchy(root, 0);
  74.     }
  75.  
  76.     public static void printDrinkHierarchy(Drink drink, int level) {
  77.         StringBuilder indent = new StringBuilder();
  78.         for (int i = 0; i < level; i++) {
  79.             indent.append("  ");
  80.         }
  81.  
  82.         System.out.println(indent + drink.getName());
  83.  
  84.         for (Drink child : drink.getChildren()) {
  85.             printDrinkHierarchy(child, level + 1);
  86.         }
  87.     }
  88. }
  89.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement