简体   繁体   中英

LEVEL BY LEVEL of a Tree with Java

How can I get all the elements of a Tree?

public static <E> PositionList<E> width(Tree<E> t){
    //I've initialized a queue and a PositionList<E>
    Queue<E> aux =  new NodeQueue<E>();
    PositionList<E> positionAux =  new NodePositionList<E>();
Position<E> pointer = t.root();

...

but then I don't know how to continue...

Example:

     1
    |  |
    2   3
   | |  | |
   4  5  6 7

Return a list [1,2,3, 4, 5, 6, 7]

Use a Queue to do level traverse. You can see my implementation: https://github.com/shaogbi/Java/blob/master/datastructure/MyBinaryTree.java see levelTraverse function.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM