Skip to content

Commit

Permalink
invert-tree: Add test cases for asymmetric trees
Browse files Browse the repository at this point in the history
My first attempt at this challenge takes a tree like this:

```
    A
   /
  B
```

and makes it into a tree like this:

```
    A
   / \
  B   B
```

when it should produce a tree like this:

```
    A
     \
      B
```

However, my flawed solution was passing the single test example that
currently exists for this challenge. I have added two test examples, one
for a left-leaning asymmetric tree and one for a right-leaning
asymmetric tree. These examples catch the type of flaw discussed above,
and should be helpful in encouraging/forcing challengers to clean up
after old child nodes.
  • Loading branch information
davidrunger2 committed Oct 17, 2015
1 parent 74b4f40 commit 3ae1874
Showing 1 changed file with 38 additions and 1 deletion.
39 changes: 38 additions & 1 deletion invert-tree/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ var invertTree = require('./');

describe('invert-tree', function() {
it('inverts a binary tree', function() {

var root = {value: 6};
var left = {value: 4};
var right = {value: 8};
Expand Down Expand Up @@ -41,7 +40,45 @@ describe('invert-tree', function() {
}
}
});
});

it('inverts a right-leaning asymmetric tree', function() {
var root = {value: 4};
var right = {value: 6};
var rightOfRight = {value: 8};
root.right = right;
right.right = rightOfRight;

invertTree(root);

assert.deepEqual(root, {
"value": 4,
"left": {
"value": 6,
"left": {
value: 8
}
}
});
});

it('inverts a left-leaning asymmetric tree', function() {
var root = {value: 5};
var left = {value: 7};
var leftOfLeft = {value: 9};
root.left = left;
left.left = leftOfLeft;

invertTree(root);

assert.deepEqual(root, {
"value": 5,
"right": {
"value": 7,
"right": {
value: 9
}
}
});
});
});

0 comments on commit 3ae1874

Please sign in to comment.