Skip to content

Commit

Permalink
bst
Browse files Browse the repository at this point in the history
  • Loading branch information
StephenGrider committed Jun 13, 2020
1 parent a2998ed commit 326cef2
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
3 changes: 3 additions & 0 deletions exercises/bst/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
// Node class. Insert should accept an argument
// 'data', then create an insert a new node
// at the appropriate location in the tree.
// 3) Implement the 'contains' method for the Node
// class. Contains should accept a 'data' argument
// and return the Node in the tree with the same value.

class Node {
constructor(data) {
Expand Down
25 changes: 25 additions & 0 deletions exercises/bst/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,28 @@ test('Node can insert correctly', () => {
expect(node.right.data).toEqual(15);
expect(node.right.right.data).toEqual(17);
});

test('Contains returns node with the same data', () => {
const node = new Node(10);
node.insert(5);
node.insert(15);
node.insert(20);
node.insert(0);
node.insert(-5);
node.insert(3);

const three = node.left.left.right;
expect(node.contains(3)).toEqual(three);
});

test('Contains returns null if value not found', () => {
const node = new Node(10);
node.insert(5);
node.insert(15);
node.insert(20);
node.insert(0);
node.insert(-5);
node.insert(3);

expect(node.contains(9999)).toEqual(null);
});

0 comments on commit 326cef2

Please sign in to comment.