我想用平面顺序遍历来打印我的非二叉树。在下面的代码中,每次添加一组新的子元素时,都会缩进,但当我再次返回树时,我需要删除缩进。这树是如何打印的:
Root
Home
HomeChild1
HomeChild2
Documents (should be same level as Home)
DocumentChild1
DocumentChild2
Downloads (should be same level as Home and Documents)
DownloadsChild1码:
queue.add(o); //root
int indent = 0;
while(!queue.isEmpty(){
for(int i=0; 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
queue.add(0, tempObj.children[i];
}
indent++;
}
}这是我想要的样子
Root
Home
HomeChild1
HomeChild2
Documents
DocumentChild1
DocumentChild2
Downloads
DownloadsChild1