** Shown as a tree**
Tree structure is widely used.
The following code builds a logically equivalent tree structure in memory based on the data added by the user.
ShowTree() shows it as it is in control.
Among them:
a.add('a', 'b');
a.add('b', 'e');
It means that `b'is the child node of `a', and `e' is the child node of `b'.
For example, the output of the sample data given in the code should be as follows:
a–b--e
| |–f--j
| |–k
|–c
|–d--g–h
|–i
Please read the code below and fill in the missing part (underlined part).
Note: Please put the answers in the blanks (only the answers in the blanks, excluding the questions) in the "Answer. txt" of the corresponding questions under the candidates'folder.
You can't score directly on the topic.
import java.util.*; class MyTree { private Map map = new HashMap(); public void add(char parent, char child) { List<Character> t = (List<Character>)map.get(parent); if(t==null) { t = new Vector<Character>(); ____________________; // Fill in 1 } t.add(child); } public List<Character> getChild(char x) { return (List<Character>)map.get(x); } } public class My { public static List<String> showTree(MyTree tree, char x) { List<Character> t = tree.getChild(x); List<String> r = new Vector<String>(); if(t==null) { r.add("" + x); return r; } for(int i=0; i<t.size(); i++) { List<String> ri = showTree(tree, t.get(i)); for(int j=0; j<ri.size(); j++) { String pre = "| "; if(j==0) { if(i==0) pre = x + "--"; else pre = "|--"; } else { if(i==__________________) // Fill in 2 pre = " "; else pre = "| "; } r.add(pre + ri.get(j)); } } return r; } public static void main(String[] args) { MyTree a = new MyTree(); a.add('a', 'b'); a.add('b', 'e'); a.add('b', 'f'); a.add('a', 'c'); a.add('a', 'd'); a.add('d', 'g'); a.add('d', 'i'); a.add('g', 'h'); a.add('f', 'j'); a.add('f', 'k'); List<String> lst = showTree(a, 'a'); for(int i=0; i<lst.size(); i++) { System.out.println(lst.get(i)); } } } map.put(parent, t) i== t.size()-1