Dear all,
i need some clues with this particular set of code..im trying to complete the insert function.
The insert method takes two parameters, parentVal and childVal and inserts a new node containing childVal as the last child of any node containing parentVal. If there is no node containg parentVal then insert has no effect. insert will not insert a new node below a parent that has a pre-existing child node containing childVal. To illustrate, the call insert(3,3) on the tree
The first problem is im not quite sure what this line is about
public ArrayList<Node<T1>> children = new ArrayList<Node<T1>>(); .. Can explain ?
Would appreciated if could provide sources regarding Non-Binary Trees using ArrayList.
Thanks alot
import java.util.*;
/**
Definition of a tree container that supports insertion
by adding the child not correspoinding to a parent node.
*/
public class InsertByParentTree<T>{
private Node<T> root; // reference to the root of the tree.
/**
private class defining a tree node.
*/
private class Node<T1>{
// data members, value and children.
public T1 val;
public ArrayList<Node<T1>> children = new ArrayList<Node<T1>>();
// zero-argument toString method
public String toString(){
return toString("");
}
// auxiliary toString method to print the tree in
// a nice indented fashion.
public String toString(String prefix){
String res = prefix + val + "\n";
for (Node<T1> x: children){
res += prefix + x.toString(prefix+" ");
}
return res;
}
}
/**
Constructor for InsertByParentTree's.
Builds the root node.
@param val the value to insert at the root node.
*/
public InsertByParentTree(T val){
root = new Node<T>();
root.val = val;
}
public void insert(T parentVal, T childVal){
// put code here
}
public String toString(){
return root.toString();
}
}