TIL43: 볡슡

μ˜€λŠ˜μ€ 좔석 μ—°νœ΄ λ•Œ μŠ€ν„°λ”” λͺ¨μž„ν•˜λ©΄μ„œ κ³΅λΆ€ν–ˆλ˜ 것듀을 κ°„λ‹¨νžˆ 정리해보렀고 ν•©λ‹ˆλ‹€.

μžŠμ—ˆλ˜ λΆ€λΆ„μ΄λ‚˜ μ€‘μš”ν•˜λ‹€ 싢은 κ°œλ…λ“€ μœ„μ£Όλ‘œ μ •λ¦¬ν•˜λ©΄μ„œ μŠ€ν„°λ””λ₯Ό μ§„ν–‰ν–ˆμŠ΅λ‹ˆλ‹€.

λ˜ν•œ ν•™μŠ΅μžλ£Œλ“€μ„ λ‹€μ‹œ μ½μœΌλ©΄μ„œ 이전에 μž‘μ„±ν•œ λΈ”λ‘œκ·Έ λ‚΄μš© 쀑 λΆ€μ‘±ν•œ 뢀뢄을 μ±„μ›Œλ„£μ—ˆμŠ΅λ‹ˆλ‹€.

ν™•μ‹€νžˆ 처음 μ½μ—ˆμ„ λ•Œ μ΄ν•΄λ˜μ§€ μ•Šμ•˜λ˜ 뢀뢄듀이 잘 이해가 λ˜μ–΄μ„œ κ·Έ 사이에도 μ„±μž₯ν–ˆλ‹€λŠ” λŠλ‚Œμ„ 받을 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€ :)

이진 탐색 트리(Binary Search Tree) 순회

이진 탐색 νŠΈλ¦¬μ—μ„œ 크게 μ„Έκ°€μ§€ λ°©μ‹μœΌλ‘œ μˆœνšŒκ°€ κ°€λŠ₯ν•©λ‹ˆλ‹€. μ „μœ„, μ€‘μœ„, ν›„μœ„λ‘œ λ‚˜λ‰˜μ–΄μ§€λŠ”λ°μš”.

μ½”λ“œμ—μ„œλŠ” callback ν•¨μˆ˜λ₯Ό μ–΄λ””μ„œ μ‹€ν–‰ν•˜λŠλƒμ— 따라 차이가 μƒκΉλ‹ˆλ‹€.

//μ „μœ„μˆœνšŒ
preorder(callback) {
  callback(this.value);
  if (this.left) {
    this.left.preorder(callback);
  };
  if (this.right) {
    this.right.preorder(callback)
  };
}

//μ€‘μœ„μˆœνšŒ
inorder(callback) {
  if (this.left) {
    this.left.inorder(callback);
  };
  callback(this.value);
  if (this.right) {
    this.right.inorder(callback)
  };
}

//ν›„μœ„μˆœνšŒ
postorder(callback) {
  if (this.left) {
    this.left.postorder(callback);
  };
  if (this.right) {
    this.right.postorder(callback)
  };
  callback(this.value);
}

 

 

 

비동기