HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Reverse values of alternate nodes of the tree

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
nodesthereversevaluestreealternate

Problem

Given a Perfect Binary Tree, reverse the alternate level nodes of the binary tree.


Given tree:

a

       /     \
      b       c
    /  \     /  \
   d    e    f    g
  / \  / \  / \  / \
  h  i j  k l  m  n  o




Modified tree:

a

       /     \
       c       b
     /  \     /  \
    d    e    f    g
   / \  / \  / \  / \
  o  n m  l k  j  i  h


This question is attributed to GeeksForGeeks. The difference between this and the one solved by me previously here, is that in this case the "values" can be changed rather than the actual nodes. Looking for code review, optmizations and best practices.

```
public class SwapTreeLevelsValues {

private TreeNode root;

public SwapTreeLevelsValues(List nodes) {
create(nodes);
}

private void create (List items) {
root = new TreeNode<>(items.get(0));

final Queue> queue = new LinkedList>();
queue.add(root);

final int half = items.size() / 2;

for (int i = 0; i current = queue.poll();
final int left = 2 * i + 1;
final int right = 2 * i + 2;

if (items.get(left) != null) {
current.left = new TreeNode(items.get(left));
queue.add(current.left);
}
if (right (items.get(right));
queue.add(current.right);
}
}
}
}

public static class TreeNode {
private TreeNode left;
private T item;
private TreeNode right;

TreeNode(T item) {
this.item = item;
}
}

/**
* Given a perfect binary tree, swaps the even levels.
* For an non-perfect binary tree, results are unpredictable.
*/
public void reverseAlternateLevels() {
if (root == null) {
throw new IllegalStateException("The root cannot be null");
}
reverse(root, root, true);
}

private void re

Solution

A minor bug: You get an IndexOutOfBoundsException for an empty list in SwapTreeLevelsValues.create(List items). You don't have a comment stating you need to input a list containing at least something. Consider returning IllegalArgumentException and adding a comment.

I had to do a double take here:

final int left = 2 * i + 1;
final int right = 2 * i + 2;


It only made sense after looking at the diagram. Still, you're doing a calculation twice... why not do it like this?

final int left = 2 * i + 1;
final int right = left + 1;


That said, you might wanna put a comment there. It's not a trivial line of code.

Looking up a bit...

final int half = items.size() / 2;

for (int i = 0; i < half; i++) {
    if (items.get(i) != null) {


You're not modifying items. Is it possible for items.get(i) to not return null once it has once returned null? If not, why not break out of the for loop, rather than going through the remaining loops? (Performance when dealing with large TreeMaps).

I can't find anything else, really.

Code Snippets

final int left = 2 * i + 1;
final int right = 2 * i + 2;
final int left = 2 * i + 1;
final int right = left + 1;
final int half = items.size() / 2;

for (int i = 0; i < half; i++) {
    if (items.get(i) != null) {

Context

StackExchange Code Review Q#57652, answer score: 3

Revisions (0)

No revisions yet.