Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Binary Search Tree: Corrected return types on left and right methods and adjusted tests #389

Merged
merged 1 commit into from Jan 11, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
61 changes: 25 additions & 36 deletions exercises/binary-search-tree/binary-search-tree.example.ts
@@ -1,55 +1,44 @@
export default class BinarySearchTree {
private _data: number
type BinarySearchTreeMaybe = BinarySearchTree | undefined
type EachFn = (data: number) => void

class BinarySearchTree {
private readonly _data: number
private _left?: BinarySearchTree
private _right?: BinarySearchTree

constructor(_data: number) {
this._data = _data
constructor(data: number) {
this._data = data
}

public insert(value: number): this {
return value <= this._data
? this.insertLeft(value)
: this.insertRight(value)
if (value <= this._data) {
if (this._left) this._left.insert(value)
else this._left = new BinarySearchTree(value)
} else {
if (this._right) this._right.insert(value)
else this._right = new BinarySearchTree(value)
}

return this
}

public each(fn: (data: number) => void): void {
if (this._left) {
this._left.each(fn)
}
public each(fn: EachFn): void {
if (this._left) this._left.each(fn)
fn(this._data)
if (this._right) {
this._right.each(fn)
}
if (this._right) this._right.each(fn)
}

public get data(): number {
return this._data
}

public get left(): BinarySearchTree {
return this._left!
}

public get right(): BinarySearchTree {
return this._right!
public get left(): BinarySearchTreeMaybe {
return this._left
}

private insertLeft(value: number): this {
if (!this._left) {
this._left = new BinarySearchTree(value)
} else {
this._left.insert(value)
}
return this
}

private insertRight(value: number): this {
if (!this._right) {
this._right = new BinarySearchTree(value)
} else {
this._right.insert(value)
}
return this
public get right(): BinarySearchTreeMaybe {
return this._right
}
}

export default BinarySearchTree
18 changes: 9 additions & 9 deletions exercises/binary-search-tree/binary-search-tree.test.ts
Expand Up @@ -16,23 +16,23 @@ describe('BinarySearchTree', () => {
four.insert(2)

expect(four.data).toEqual(4)
expect(four.left.data).toEqual(2)
expect(four.left!.data).toEqual(2)
})

xit('should insert the same number to the left', () => {
const four = new BinarySearchTree(4)
four.insert(4)

expect(four.data).toEqual(4)
expect(four.left.data).toEqual(4)
expect(four.left!.data).toEqual(4)
})

xit('should insert a greater number to the right', () => {
const four = new BinarySearchTree(4)
four.insert(5)

expect(four.data).toEqual(4)
expect(four.right.data).toEqual(5)
expect(four.right!.data).toEqual(5)
})

xit('should deal with a complex tree', () => {
Expand All @@ -45,12 +45,12 @@ describe('BinarySearchTree', () => {
four.insert(5)

expect(four.data).toEqual(4)
expect(four.left.data).toEqual(2)
expect(four.left.left.data).toEqual(1)
expect(four.left.right.data).toEqual(3)
expect(four.right.data).toEqual(6)
expect(four.right.left.data).toEqual(5)
expect(four.right.right.data).toEqual(7)
expect(four.left!.data).toEqual(2)
expect(four.left!.left!.data).toEqual(1)
expect(four.left!.right!.data).toEqual(3)
expect(four.right!.data).toEqual(6)
expect(four.right!.left!.data).toEqual(5)
expect(four.right!.right!.data).toEqual(7)
})

xit('should iterate over one element', () => {
Expand Down