Tree(ํธ๋ฆฌ) โ๐ป
์ฉ์ด์ ๋ฆฌ
- ๋ ธ๋(Node) : ํธ๋ฆฌ ๊ตฌ์กฐ๋ฅผ ์ด๋ฃจ๋ ๋ชจ๋ ๊ฐ๋ณ ๋ฐ์ดํฐ
- ๋ฃจํธ(Root) : ํธ๋ฆฌ ๊ตฌ์กฐ์ ์์์ ์ด ๋๋ ๋ ธ๋
- ๋ถ๋ชจ ๋ ธ๋(Parent node) : ๋ ๋ ธ๋๊ฐ ์ํ๊ด๊ณ๋ก ์ฐ๊ฒฐ๋์ด ์์ ๋ ์๋์ ์ผ๋ก ๋ฃจํธ์์ ๊ฐ๊น์ด ๋ ธ๋
- ์์ ๋ ธ๋(Child node) : ๋ ๋ ธ๋๊ฐ ์ํ๊ด๊ณ๋ก ์ฐ๊ฒฐ๋์ด ์์ ๋ ์๋์ ์ผ๋ก ๋ฃจํธ์์ ๋จผ ๋ ธ๋
- ๋ฆฌํ(Leaf) : ํธ๋ฆฌ ๊ตฌ์กฐ์ ๋์ง์ ์ด๊ณ , ์์ ๋ ธ๋๊ฐ ์๋ ๋ ธ๋
/
)์์ ์์๋์ด, ๊ฐ์ง๋ฅผ ๋ป์ด๋๊ฐ๋ ๋ชจ์์๋ฅผ ๋๋ค.ํธ๋ฆฌ์ ๋ ๋ค๋ฅธ ์์
์๋์ปต ํ ๋๋จผํธ ๋์งํ, ๊ฐ๊ณ๋(์กฑ๋ณด), ์กฐ์ง๋ ๋ฑ
์ฝ์ : ์ด ๋ฉ์๋๋ ๊ฐ์ ์๋ฝํ๊ณ ์ ๋ ธ๋๋ฅผ ๋ง๋ ๋ค. ๋ ธ๋๊ฐ ์์ฑ๋๋ฉด ์ด ๋ฉ์๋๋ ์ผ๋ จ์ ๊ฒ์ฌ๋ฅผ ํตํด ๋ ธ๋๊ฐ ์ผ์ชฝ์ ์๋ ๋ฎ์ ๊ฐ๊ณผ ์ค๋ฅธ์ชฝ์ ์๋ ๋์ ๊ฐ์ ๊ธฐ์ค์ ์ถฉ์ ํ๋ ์์น์ ๋ฐฐ์น๋์ด ์๋์ง ํ์ธํ๋ค. ๊ฐ์ด ์ด๋ฏธ ์๋ ๊ฒฝ์ฐ ๋ฉ์๋๋ undefined๋ฅผ ๋ฐํํ๋ค.
class Node {
constructor(val){
this.val = val
this.left = null
this.right = null
}
}
class BinarySearchTree {
constructor(){
this.size = 0
this.root = null
}
}
let bst1 = new BinarySearchTree()
let node1 = new Node(1)
bst1 // BinarySearchTree {size: 0, root: null}
node1 // Node {val: 1, left: null, right: null}
์กฐํ : ํ๋ก์ธ์ค๋ ํธ๋ฆฌ์์ ๋
ธ๋๋ฅผ ์ฐพ๊ธฐ ์ํด ์ ์ฌํ ๊ฒ์ฌ๋ฅผ ํตํด ์คํ๋๋ค.
๋
ธ๋๊ฐ ์กด์ฌํ์ง ์๋ ๊ฒฝ์ฐ ๋ฉ์๋๋ undefined๋ฅผ ๋ฐํํ๋ค.
class Node{
constructor(val){
this.val = val
this.left = null
this.right = null
}
}
class BinarySearchTree {
constructor(){
this.size = 0
this.root = null
}
insert(val){
let newNode = new Node(val)
if(!this.root){
this.root = newNode
this.size++
return this
}
let current = this.root
while(true){
if(val === current.val) return undefined
if(val < current.val){
if(!current.left){
current.left = newNode
this.size++
return this
} else {
current = current.left
}
} else if (val > current.val){
if(!current.right){
current.right = newNode
this.size++
return this
} else {
current = current.right
}
}
}
}
lookup(val){
if(!this.root) return undefined
let current = this.root
while(true){
if(val === current.val) return current
if(val > current.val){
if(!current.right) return undefined
current = current.right
} else if (val < current.val){
if(!current.left) return undefined
current = current.left
}
}
}
}
let bst = new BinarySearchTree()
bst.insert(10)
bst.insert(6)
bst.insert(3)
bst.insert(8)
bst.insert(15)
bst.insert(20)
bst // BinarySearchTree {size: 6, root: Node}
bst.lookup(10) // Node {val: 10, left: Node, right: Node}
bst.lookup(3) // Node {val: 3, left: null, right: null}
class Tree {
constructor(value) {
// constructor๋ก ๋ง๋ ๊ฐ์ฒด๋ ํธ๋ฆฌ์ Node๊ฐ ๋ฉ๋๋ค.
this.value = value;
this.children = [];
}
// ํธ๋ฆฌ์ ์ฝ์
๋ฉ์๋๋ฅผ ๋ง๋ญ๋๋ค.
insertNode(value) {
// ๊ฐ์ด ์ด๋ค ์ด๋ฆ์ผ๋ก ๋ง๋ค์ด์ง๊ณ ์ด๋ ์์น์ ๋ถ๋์ง ๋ ์ฌ๋ฆฌ๋ ๊ฒ์ด ์ค์ํฉ๋๋ค.
// TODO: ํธ๋ฆฌ์ ๋ถ๊ฒ ๋ childNode๋ฅผ ๋ง๋ค๊ณ , children์ ๋ฃ์ด์ผ ํฉ๋๋ค.
const childNode = new Tree(value);
this.children.push(childNode);
}
// ํธ๋ฆฌ ์์ ํด๋น ๊ฐ์ด ํฌํจ๋์ด ์๋์ง ํ์ธํ๋ ๋ฉ์๋๋ฅผ ๋ง๋ญ๋๋ค.
contains(value) {
// TODO: ๊ฐ์ด ํฌํจ๋์ด ์๋ค๋ฉด true๋ฅผ ๋ฐํํ์ธ์.
if (this.value === value) {
return true;
}
// TODO: ๊ฐ์ ์ฐพ์ ๋๊น์ง children ๋ฐฐ์ด์ ์ํํ๋ฉฐ childNode๋ฅผ ํ์ํ์ธ์.
for (let i = 0; i < this.children.length; i++) {
const childNode = this.children[i];
if (childNode.contains(value)) {
return true;
}
// ์ ๋ถ ํ์ํ์์๋ ๋ถ๊ตฌํ๊ณ ์ฐพ์ง ๋ชปํ๋ค๋ฉด false๋ฅผ ๋ฐํํฉ๋๋ค.
}
return false;
}
}