function - Implement DFS for a tree in java -


please find tree class definition below.

public class tree<t>{    private t head;    private list<tree<t>> leafs = new arraylist<>();    private tree<t> parent = null;    private map<t, tree<t>> locate = new hashmap<>();    public tree(t head) {     this.head = head;     locate.put(head, this);   }     public void addleaf(t root, t leaf) {     if (locate.containskey(root)) {      locate.get(root).addleaf(leaf);     } else {       addleaf(root).addleaf(leaf);     }   }    public tree<t> addleaf(t leaf) {     tree<t> t = new tree<>(leaf);     leafs.add(t);     t.parent = this;     t.locate = this.locate;     locate.put(leaf, t);     return t;   } } 

the tree class object created in class , nodes added in straightforward way (using addleaf(node) function). process builds tree alright. able suggest dfs function implementation on constructed tree adhering above class definition?

thank you.


this i've tried. yes, gives me meaningless results.

protected void dfs() {     for(tree<t> child : leafs) {         dfs();         system.out.println(child);     } } 

the code third comment @ link


protected void dfs() {   for(tree<t> child : leafs) {       child.dfs();       system.out.println(child.head);   } } 

resolved!

you're close. print should value of node, , recursion should on child.


Comments

Popular posts from this blog

jasper reports - Fixed header in Excel using JasperReports -

media player - Android: mediaplayer went away with unhandled events -

python - ('The SQL contains 0 parameter markers, but 50 parameters were supplied', 'HY000') or TypeError: 'tuple' object is not callable -