Skip to main content
deleted 29 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

No. You are trying to derive the level number from a flat list of nodes and some power of 2. This mightwill only work with a balancedcomplete balanced binary search tree, but I'm not even sure of that, andand you didn't mention the "balanced" part in yoursuch requirements to be present. To see a bad example, insert -4 to your existing sample. The resulting tree:

No. You are trying to derive the level number from a flat list of nodes and some power of 2. This might work with a balanced binary search tree, but I'm not even sure of that, and you didn't mention the "balanced" part in your requirements. To see a bad example, insert -4 to your existing sample. The resulting tree:

No. You are trying to derive the level number from a flat list of nodes and some power of 2. This will only work with a complete balanced binary search tree, and you didn't mention such requirements to be present. To see a bad example, insert -4 to your existing sample. The resulting tree:

added 1239 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

To be continued ....Preserving the levels

Typing it upTo preserve the levels, hang tight.. you need a different thinking in traverseLevels. Here's one ay to solve this:

  • In every cycle of !nodes.isEmpty()
    • Copy and consume all the nodes currently in the queue
    • Add the non-null children of the nodes to the queue
    • This way, every cycle will correspond to one level

Implementation:

private List<List<Node>> traverseLevels(Node root) {
    if (root == null) {
        return Collections.emptyList();
    }
    List<List<Node>> levels = new LinkedList<>();

    Queue<Node> nodes = new LinkedList<>();
    nodes.add(root);

    while (!nodes.isEmpty()) {
        List<Node> level = new ArrayList<>(nodes.size());
        levels.add(level);

        for (Node node : new ArrayList<>(nodes)) {
            level.add(node);
            if (node.left != null) {
                nodes.add(node.left);
            }
            if (node.right != null) {
                nodes.add(node.right);
            }
            nodes.poll();
        }
    }
    return levels;
}

Using the rewritten traverseLevels, the implementation of printLevelWise becomes straighforward:

public void printLevelWise(Node root) {
    List<List<Node>> levels = traverseLevels(root);

    for (List<Node> level : levels) {
        for (Node node : level) {
            System.out.print(node.value + " ");
        }
        System.out.println();
    }
}

And produces the correct output:

5 
1 8 
-2 3 6 9 
-3 -1 
-4

To be continued ....

Typing it up, hang tight...

Preserving the levels

To preserve the levels, you need a different thinking in traverseLevels. Here's one ay to solve this:

  • In every cycle of !nodes.isEmpty()
    • Copy and consume all the nodes currently in the queue
    • Add the non-null children of the nodes to the queue
    • This way, every cycle will correspond to one level

Implementation:

private List<List<Node>> traverseLevels(Node root) {
    if (root == null) {
        return Collections.emptyList();
    }
    List<List<Node>> levels = new LinkedList<>();

    Queue<Node> nodes = new LinkedList<>();
    nodes.add(root);

    while (!nodes.isEmpty()) {
        List<Node> level = new ArrayList<>(nodes.size());
        levels.add(level);

        for (Node node : new ArrayList<>(nodes)) {
            level.add(node);
            if (node.left != null) {
                nodes.add(node.left);
            }
            if (node.right != null) {
                nodes.add(node.right);
            }
            nodes.poll();
        }
    }
    return levels;
}

Using the rewritten traverseLevels, the implementation of printLevelWise becomes straighforward:

public void printLevelWise(Node root) {
    List<List<Node>> levels = traverseLevels(root);

    for (List<Node> level : levels) {
        for (Node node : level) {
            System.out.print(node.value + " ");
        }
        System.out.println();
    }
}

And produces the correct output:

5 
1 8 
-2 3 6 9 
-3 -1 
-4
added 1239 characters in body
Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391

void method with out-parameter

The traverseLevels method is practically a void method with an out-parameter List<Node> that it populates. In many practical cases, including your program in question, a better option is to make the method return the out-parameter as its result.

Move common logic to higher level

In this code:

listOfNodes.add(root);
while(!nodes.isEmpty()){
    root= nodes.poll();
    if (root.left!=null) {
        listOfNodes.add(root.left);
        nodes.add(root.left);
    }
    if (root.right!=null) {
        listOfNodes.add(root.right);
        nodes.add(root.right);
    }
}

You add the root to listOfNodes, and then add the branches. That is, you are adding to the list at 3 places. You could refactor this to add to the list at once place, right after polling:

while (!nodes.isEmpty()) {
    root = nodes.poll();
    listOfNodes.add(root);
    if (root.left != null) {
        nodes.add(root.left);
    }
    if (root.right != null) {
        nodes.add(root.right);
    }
}

This is simpler, shorter, and the outcome is equivalent.

Bad comments

Bad comments

void method with out-parameter

The traverseLevels method is practically a void method with an out-parameter List<Node> that it populates. In many practical cases, including your program in question, a better option is to make the method return the out-parameter as its result.

Move common logic to higher level

In this code:

listOfNodes.add(root);
while(!nodes.isEmpty()){
    root= nodes.poll();
    if (root.left!=null) {
        listOfNodes.add(root.left);
        nodes.add(root.left);
    }
    if (root.right!=null) {
        listOfNodes.add(root.right);
        nodes.add(root.right);
    }
}

You add the root to listOfNodes, and then add the branches. That is, you are adding to the list at 3 places. You could refactor this to add to the list at once place, right after polling:

while (!nodes.isEmpty()) {
    root = nodes.poll();
    listOfNodes.add(root);
    if (root.left != null) {
        nodes.add(root.left);
    }
    if (root.right != null) {
        nodes.add(root.right);
    }
}

This is simpler, shorter, and the outcome is equivalent.

Bad comments

Source Link
janos
  • 111.7k
  • 15
  • 152
  • 391
Loading