I want to print my non-binary tree with level-order traverse. In the code below I indent every time a new set of children has been added, but somehow I need to remove indentations when I go back in the tree again. Here's how this tree prints:
Root
Home
HomeChild1
HomeChild2
Documents (should be same level as Home)
DocumentChild1
DocumentChild2
Downloads (should be same level as Home and Documents)
DownloadsChild1
Code:
queue.add(o); //root
int indent = 0;
while(!queue.isEmpty(){
for(int i=0; i<indent; i++){
print(" ");
}
Object tempObj = queue.remove(o);
print(tempObj.value);
if(tempObj.children != null){
//Adding all childrens, since its not a binary tree I loop throught all children
for(int i=0; i<tempObj.children.length; i++){
queue.add(0, tempObj.children[i];
}
indent++;
}
}
Here is what I want it to look like
Root
Home
HomeChild1
HomeChild2
Documents
DocumentChild1
DocumentChild2
Downloads
DownloadsChild1