Page 1 of 1

Modify the code shown. Instead of an organizational chart of the DOE, make the code represent some other tree structure

Posted: Sun Jul 03, 2022 12:00 pm
by answerhappygod
Modify the code shown. Instead of an organizational chart of theDOE, make the code represent some other tree structure from thereal world. Implement two operations with your new treestructure:
Example 1: Your tree structure could show the languages of theworld, each language having a name and the number of nativespeakers of that language. The languages are derived from eachother, for example English is derived from Latin.
Example 2: Your tree structure could show the species of life inthe world, each having a name and the number of living creates ofthat species. Species descend from each other, for example lionsare mammals and mammals are vertebrates.
Your tree should have at least ten nodes and at least threelevels.
Modify The Code Shown Instead Of An Organizational Chart Of The Doe Make The Code Represent Some Other Tree Structure 1
Modify The Code Shown Instead Of An Organizational Chart Of The Doe Make The Code Represent Some Other Tree Structure 1 (256.95 KiB) Viewed 19 times
Modify The Code Shown Instead Of An Organizational Chart Of The Doe Make The Code Represent Some Other Tree Structure 2
Modify The Code Shown Instead Of An Organizational Chart Of The Doe Make The Code Represent Some Other Tree Structure 2 (151.03 KiB) Viewed 19 times
1 package ch15_2; 2 3 import static java.lang. System.out; 4 5 import java.util.ArrayList; 6 import java.util.HashMap; 7 import java.util.List; 8 import java.util.Map; 9 10 11 class TreeNode<E> { 12 E data; 13 14 150 16 17 18 19 25 26 27 20 } 21 22 public class ch15_2 { 23 240 public static void main(String[] args) { 28 29 30 31 32 33 34 35 36 37 List<TreeNode<E>> children = new ArrayList<>(); public TreeNode (E data, TreeNode<E>... children) { this.data = data; for (TreeNode<E> child children) this.children.add(child); 38 39 40 41 42 43 } } TreeNode<String> city1 = new TreeNode<>("San Diego"); TreeNode<String> city2 = new TreeNode<>("Los Angeles"); TreeNode<String> statel = new TreeNode<>("CA", cityl, city2); TreeNode<String> city3 = new TreeNode<>("Albany"); TreeNode<String> city4 = new TreeNode<>("New York"); TreeNode<String> state2 = new TreeNode<>("NY", city3, city4); TreeNode<String> city5 = new TreeNode<>("Tampa"); TreeNode<String> city6 = new TreeNode<>("Orlando"); TreeNode<String> city7 = new TreeNode<> ("Miami"); TreeNode<String> state3 = new TreeNode<>("FL", city5, city6, city7); TreeNode<String> country = new TreeNode<>("USA", statel, state2, state3); printTreeIterative (country);
43 440 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69} 70 private static <E> void printTreeIterative (TreeNode<E> root) { Map<TreeNode<E>, Integer> nodeLevelMap = new HashMap<>(); List<TreeNode<E>> stack = new ArrayList<>(); stack.add(root); } node LevelMap.put(root, 0); while (!stack.is Empty()) { } // pop a node from the stack TreeNode<E> currentNode = stack.remove(stack.size() − 1); // process the node int currentNode Level = node LevelMap.get(currentNode); for (int i = 0; i < currentNodeLevel; i++) out.print(" "); out.println(currentNode.data); // push each of the current node's children on to the stack for (TreeNode<E> child : currentNode.children) { stack.add(child); nodeLevelMap.put(child, currentNodeLevel + 1); }