Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
17 changes: 17 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
.DS_Store

/generated/prisma
API
Models
mission8/
mission9/
mission6/
mission5/
mission4/
mission3/
mission2/
typescripted/
practiceForUser/
book_rental_service/
prc4/
refactoring_code/
Empty file.
133 changes: 133 additions & 0 deletions algorithm/BST/BinarySearchTree.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
class Node():
def __init__(self):
self.val: None
self.left: None
self.right: None

class BinaryTree():
def __init__(self):
self.stack = []
self.que = []

def insert(self, value):
crnt = self.root
newNode = Node(value)

if not self.root:
self.root = newNode
return

while (crnt):
if crnt.val > newNode.val:
if crnt.left is None :
# leaf node 까지 가기
crnt.left = newNode
break
crnt = crnt.left
else:
if crnt.right is None:
crnt.right = newNode
break
crnt = crnt.right
return

def delete(self, value):
if not self.root:
return
crnt = self.root
parent = None
while crnt:
if crnt.val == value:

# 삭제할 노드가 자식이 두개가 있는 경우
if crnt.left and crnt.right:
result = self.findSuccessor(crnt)
parentOfSuccessor = result.parent
successor = result.succesor
parent = crnt
if parentOfSuccessor.left == successor:
parentOfSuccessor.left = successor.right
else:
parentOfSuccessor.left = successor.right

else:
# 삭제할 노드가 자식이 한개가 있는 경우
child = self.root.left or self.root.right
if crnt.left != None:
parent.left = child
# 삭제할 노드가 자식이 리프노드인 경우
if value < crnt.val:
crnt = crnt.left
else: crnt = crnt.right
return crnt
def find(self, value):
if not self.root: return

crnt = self.root
while crnt:
if value == crnt.val:
return crnt.val
elif value < crnt.val:
crnt = crnt.left
else:
crnt = crnt.right

return crnt
def findSuccessor(self, node):
# 삭제할 노드에서 오른쪽 자식에서 가장 막내 증손주 찾기
parent = node
crnt = node.right
while crnt.left:
parent = crnt
crnt = crnt.left
return crnt, parent
# traverse
def preOrder(self, node):
# NLR
if not self.root:
return
self.stack.append(node)

while len(self.stack) > 0:
removed = self.stack.pop()
print(removed.val)
if removed.right:
self.stack.append(removed.right)
if removed.left:
self.stack.append(removed.left)

def inOrder(self, node):
# LNR
if not node:
return

self.stack.append(node)
while len(self.stack) > 0 or node:
while node:
self.stack.append(node)
node = node.left

node = self.stack.pop()
print(node.val)

node = node.right

def postOrder(self):
if not self.root:
return

stack1 = [self.root]
stack2 = []

while len(stack1) > 0:
node = stack1.pop()
stack2.append(node)

if node.left:
stack1.append(node.left)
if node.right:
stack1.append(node.right)

while len(stack2) > 0:
node = stack2.pop()
print(node)
161 changes: 161 additions & 0 deletions algorithm/BST/BinarySearchTree.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
type BstNode<T> = {
val: T | null;
left: BstNode<T> | null;
right: BstNode<T> | null;
};

class Bst<T> {
constructor(
private root: BstNode<T> | null = null,
private compare: (a: T, b: T) => number,
private stack : BstNode<T>[] = [],
private queue: T[] = []
) {}
insert(value: T) {
let crnt = this.root;
const newNode: BstNode<T> = {
val: value,
left: null,
right: null,
};
if (!this.root) {
this.root = newNode;
return;
}
while (crnt) {
let cmp = this.compare(value, crnt.val!);
if (cmp < 0) {
if (!crnt.left) {
crnt.left = newNode;
break;
}
crnt = crnt.left;
} else if (cmp > 0) {
if (!crnt.right) {
crnt.right = newNode;
break;
}
crnt = crnt.right;
} else {
break;
}
}
}
delete(value:T): BstNode<T> | null {
let crnt = this.root
if (!this.root) return null
let parent = null
while(crnt){
let cmp = this.compare(value, crnt.val!)
let child = crnt.left ?? crnt.right
// 삭제할 노드 조건 분기
if(cmp === 0){
// 삭제할 현재 노드가 자식이 두개가 있는 경우
if(crnt.left && crnt.right){
let result = this.findSuccessor(crnt)!
let successor = result.successor
let parentSuccessor = result.parent
crnt.val = successor.val
if(parentSuccessor.left === successor) parentSuccessor.left = successor.right
if(parentSuccessor.right === successor)parentSuccessor.right = successor.right
}else{
// 삭제할 현재 노드가 리프 노드인 경우
if(!parent) this.root = child

// 삭제할 현재 노드가 하나의 자식이 있는 경우
if(parent!.left === crnt){ // 삭제할 노드에 왼쪽자식이 있는 경우
parent!.left = child // 삭제할 노드 부모 왼쪽에 삭제 노드 오른쪽에서 가장 작은 자식 대체
}else{
parent!.right = child
}
}
return this.root
}
parent = crnt // 이동전 저장
if(cmp < 0) crnt = crnt.left
else crnt = crnt.right
}
return this.root
}
find(value: T) {
let crnt = this.root
while(crnt){
let cmp = this.compare(value, crnt.val!)
if(cmp === 0){
return crnt.val;
}
else if(cmp < 0){
crnt = crnt.left
}
else {
crnt = crnt.right
}
}
return null
}
findSuccessor(node:BstNode<T>) {
// 삭제할 노드의 오른쪽 자식에서 가장 작은 값(왼쪽) 찾기
let parent = node
let crnt = node.right
if(!crnt) return null
while(crnt!.left){
parent = crnt
crnt = crnt!.left
}
return {successor: crnt, parent}
}

preOrderIterative(){
if(!this.root) return null;

let node = this.root
this.stack.push(node)
while(this.stack.length > 0){
let removed = this.stack.pop()
console.log(removed!.val)
if(node.left){
this.stack.push(node.left)
}
else {
this.stack.push(node.right!)
}
}

}

inOrderIterative(){
if(!this.root) return null;
let crnt = this.root
this.stack.push(crnt)
while(this.stack.length > 0 || crnt){
while(crnt){
this.stack.push(crnt)
crnt = crnt.left!
}
let node = this.stack.pop()
console.log(node!.val)
crnt = node!.right!
}
}

postOrderIterative(){
if(!this.root) return null;
const stack1:(BstNode<T> | undefined)[] = [this.root]
const stack2: (BstNode<T> | undefined)[] = []

while(stack1.length > 0){
const node = stack1.pop()!
stack2.push(node);

if(node.left) stack1.push(node.left)

if (node.right) stack1.push(node.right);
}
while(stack2.length > 0){
const node = stack2.pop()
console.log(node)
}
}

levelOrderIterative(){}
}
14 changes: 14 additions & 0 deletions algorithm/Backtracking/78-subset.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def subsetIterative(nums):
stack = [[[],0]]
result = []
n = len(nums)
while len(stack) > 0:
[path, start] = stack.pop()
result.append(path)

for i in range(start, n):
crntPath = path + nums[i]
path.append([crntPath, i + 1])

return result

68 changes: 68 additions & 0 deletions algorithm/Backtracking/78-subset.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
function subsets(nums: number[]): number[][] {
let stack: [number[],number][] = [[[],0]];
let result: number[][] = [];
const n = nums.length;
while(stack.length > 0){
let [path, start] = stack.pop()!;
result.push(path)
for(let i = start; i < n; i++){
let crntPath = [...path, nums[i]]
stack.push([crntPath, i + 1])
}
}
return result
};

function subsetsBT(nums: number[]): number[][] {
const result: number[][] = []
const n: number = nums.length
function bt(path : number[], start: number){
result.push([...path])// 값넣기

for(let i = start; i < n; i++){
path.push(nums[i]) // 선택
bt(path, i + 1)
path.pop() // 선택 해제
}
}
bt([],0)
return result
};

/*

* decison space
[]
/ | \
[1] [2] [3]
/ \ / \
[1,2] [1,3] [2,3]

[1, 2, 3]


stack [[[1], 1], [[2], 2],[[3], 3]]

stack.pop()
the result = [[],[3]]
stack = [[[1], 1], [[2], 2]]

stack.pop()
stack = [[], 0], [[1], 1]]
result = [[], [3], [2]]
stack.push (path + previous path)
stack = [[[1], 1], [[2,3], 3]]

stack.pop()
stack = [ [[1], 1]]
result = [[], [2], [3], [2,3]]

stack.push([[1, 2], 2], [[1, 3], 3])

stack.pop()
result = [[], [2], [3], [2, 3], [1, 3]]


*/


Loading