목적 : 트리의 구조를 배열과 클레스 객체로 표현할 수 있다.
코드구현
class Tree{
constructor(value){
this.value=value
this.children=[]
}
insert(value){
const childNode= new Tree(value)
this.children.push(childNode)
}
}
const rootNote= new Tree(null)
rootNote.insert(1)
firstNode= rootNote.children[0]
firstNode.insert(2)
console.log(firstNode)
깨달은 점
1.어떤 데이터의 관계를 만들 때 생각해야될 아이디어
1.1 데이터를 담을 공간을 만들어라 this.value= value
1.2 수직적 관계를 만들고 싶으면 그 관계를 만들기 위한 공간 [] 을 만들어 새롭게 노드를 정의한다.
this.children=[](배열안에 배열)
'자료구조' 카테고리의 다른 글
자료구조 (0) | 2021.12.07 |
---|---|
Q의 이해 프린트 작업목록 (0) | 2021.10.21 |
Q의 이해 박스뽑기편 (0) | 2021.10.16 |
스택의 이해 앞으로 가기 뒤로가기 (0) | 2021.10.15 |
자료구조 기초 스텍 & 큐 (0) | 2021.10.08 |