简体   繁体   中英

print tree level wise

 public class treeNodeUse {


        public static treeNode<Integer> takeInput(){
            int n;
            Scanner sc = new Scanner(System.in);
            System.out.println("enter data of node");
            treeNode<Integer> root = new treeNode<>(sc.nextInt());
            System.out.println("enter number of children ");
            int count = sc.nextInt();

            for (int i=0;i<count;i++){
                treeNode<Integer> children = takeInput();
    root.children.add(children);
            }
    return root;
        }
        public static treeNode<Integer> takeInputLevelwise(){
            Queue<treeNode<Integer>> q = new LinkedList<>();
            Scanner sc = new Scanner(System.in);
            System.out.println("enter value of root");
            treeNode<Integer> root = new treeNode<Integer>(sc.nextInt());
    q.add(root);
            while(!q.isEmpty()){
                treeNode<Integer> a = q.remove();
                System.out.println("enter no of children of " + a.data);
                int x = sc.nextInt();
                for(int i=0;i<x;i++){
                    System.out.println("enter " + (i+1)+"th "+"child of " +a.data );
                    treeNode<Integer> child = new treeNode<Integer>(sc.nextInt());
                    a.children.add(child);
                    q.add(child);

                }

            }
            return root;
        }

        public static void printLevelWise(treeNode<Integer> root){
            Queue<treeNode<Integer>> q = new LinkedList<>();
           q.add(root);
            System.out.println(root.data);
            while(!q.isEmpty()){
                treeNode<Integer> a = q.remove();
                if(a.children.size() > 0) System.out.println();
                for(int i=0;i<a.children.size();i++) {
                    q.add(a.children.get(i));
                    System.out.print(a.children.get(i).data + " ");

                }

            }

        }

        public static void print(treeNode<Integer> root){
    String s = root.data + ":";
    for(int i =0 ; i<root.children.size();i++){
        s = s+ root.children.get(i).data + "," ;
    }
    System.out.println(s);
    for(int i=0;i<root.children.size();i++)
    print(root.children.get(i));
        }


        public static void main(String[] args){

    printLevelWise(takeInputLevelwise());
        }



    }


     class treeNode<T> {
        T data;
        public ArrayList<treeNode> children;
        public treeNode(T data){
            this.data=data;
            children = new ArrayList<>();
        }

    }

check the printLevelWise function..this is a function which would take root of a tree and print it level wise...my code prints everything in one line..any suggestions please.. https://i1.wp.com/algorithms.tutorialhorizon.com/files/2014/09/Level-Order-Traversal-Print-each-level-in-one-line.1.png this is how i want my output

You have to print all the level nodes at once like below code. Hopefully this will solve your problem. Let me know if don't understand any part.

public static void printLevelWise(treeNode<Integer> root) {
    Queue<treeNode<Integer>> q = new LinkedList<>();
    q.add(root);
    System.out.print(root.data);
    while (!q.isEmpty()) {
        int levelSize = q.size();

        if (levelSize > 0) System.out.println();
        while(levelSize > 0) {
            treeNode<Integer> a = q.remove();
            for (int i = 0; i < a.children.size(); i++) {
                q.add(a.children.get(i));
                System.out.print(a.children.get(i).data + " ");
            }
            levelSize--;
        }


    }
}

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