-
Notifications
You must be signed in to change notification settings - Fork 5
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
BST 구현 #15
Open
passwd10
wants to merge
1
commit into
itaewon-coding-dojo:master
Choose a base branch
from
passwd10:feature/bst
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
BST 구현 #15
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
/* | ||
왼쪽자식노드는 오른쪽자식노드보다 작아야한다. | ||
*/ | ||
|
||
class Node { | ||
constructor(data) { | ||
this.data = data; | ||
this.left = null; | ||
this.right = null; | ||
} | ||
} | ||
|
||
class BinarySearchTree { | ||
constructor() { | ||
this.root = null; | ||
} | ||
|
||
insert(data) { | ||
const node = new Node(data); | ||
|
||
this.root = this.insertNode(this.root, node); | ||
} | ||
|
||
insertNode(root, node) { | ||
if (!root) { | ||
return node; | ||
} | ||
|
||
root.data <= node.data ? | ||
root.right = this.insertNode(root.right, node) : | ||
root.left = this.insertNode(root.left, node) | ||
|
||
return root; | ||
} | ||
|
||
delete(data) { // no child | one child | 2 children | ||
if (!this.root) { | ||
console.log('비어있는 트리입니다.') | ||
return; | ||
} | ||
|
||
this.deleteNode(this.root, data); | ||
} | ||
|
||
deleteNode(root, data) { | ||
if (root === null) { | ||
return root; | ||
} | ||
if (data < root.data) { | ||
root.left = this.deleteNode(root.left, data); | ||
} | ||
if (data > root.data) { | ||
root.right = this.deleteNode(root.right, data); | ||
} | ||
if (data === root.data) { | ||
if (!root.left && !root.right) { // no child | ||
return null; | ||
} | ||
if (!root.left) { // 1 child | ||
return root.right; | ||
} | ||
if (!root.right) { // 1 child | ||
return root.left; | ||
} | ||
// 2 children | ||
root = this.getMin(root.right); //가장 작은값으로 현재 root노드 대체 | ||
root.right = this.deleteNode(root.right, root.data); //찾았던 가장 작은값 삭제 | ||
} | ||
return root; | ||
} | ||
|
||
find(root, data) { | ||
if (root.data === data) { | ||
return root; | ||
} | ||
|
||
if (root.data < data) { | ||
return this.find(root.right, data); | ||
} | ||
|
||
return this.find(root.left, data); | ||
} | ||
|
||
getMin(root) { | ||
if (root.left === null) { | ||
return root; | ||
} | ||
return this.getMin(root.left); | ||
} | ||
|
||
getMax() { | ||
if (root.right === null) { | ||
return root; | ||
} | ||
return this.getMax(root.right); | ||
} | ||
|
||
inOrder() { // 중위순회(왼쪽 - 자신 - 오른쪽) | ||
return this.inOrderNode(this.root, []); | ||
} | ||
|
||
inOrderNode(root, result) { | ||
if (root !== null) { | ||
this.inOrderNode(root.left, result); | ||
result.push(root.data); | ||
this.inOrderNode(root.right, result); | ||
} | ||
return result; | ||
} | ||
|
||
search(value) { | ||
return this.searchNode(this.root, value); | ||
} | ||
|
||
searchNode(root, value) { | ||
if (root.data < value) { | ||
return this.searchNode(root.right, value); | ||
} | ||
if (root.data > value) { | ||
return this.searchNode(root.left, value); | ||
} | ||
return root; | ||
} | ||
} | ||
|
||
test('Insert', () => { | ||
const tree = new BinarySearchTree(); | ||
|
||
tree.insert(5); | ||
tree.insert(1); | ||
tree.insert(2); | ||
tree.insert(4); | ||
tree.insert(6); | ||
tree.insert(7); | ||
tree.insert(8); | ||
|
||
expect(tree.inOrder()).toEqual([1, 2, 4, 5, 6, 7, 8]); | ||
}); | ||
|
||
test.only('Delete', () => { | ||
const tree = new BinarySearchTree(); | ||
|
||
tree.insert(5); | ||
tree.insert(1); | ||
tree.insert(2); | ||
tree.insert(4); | ||
tree.insert(6); | ||
tree.insert(7); | ||
tree.insert(8); | ||
|
||
tree.delete(4); | ||
|
||
expect(tree.inOrder()).toEqual([ 1, 2, 5, 6, 7, 8 ]); | ||
}); | ||
|
||
test('getMin', () => { | ||
const tree = new BinarySearchTree(); | ||
const root = tree.root; | ||
|
||
tree.insert(3); | ||
tree.insert(2); | ||
tree.insert(1); | ||
tree.insert(4); | ||
tree.insert(5); | ||
|
||
expect(tree.getMin(root).data).toEqual(1); | ||
}); | ||
|
||
test.only('search', () => { | ||
const tree = new BinarySearchTree(); | ||
|
||
tree.insert(3); | ||
tree.insert(2); | ||
tree.insert(1); | ||
tree.insert(4); | ||
tree.insert(5); | ||
|
||
expect(tree.search(1)).toEqual({ data: 1, left: null, right: null }); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
data 랑 value가 같은 type?것?을 가르키는 것 같은데 혹시 연관이 잘 안되는(저만 그렇게 느낄수도??ㅋㅋ) 두 변수 이름을 쓰시는 이유가 있을까요?