JS 알고리즘 18일차 - 트리 순회(BFS)


JS 알고리즘 18일차 - 트리 순회(BFS)

넓이 우선 탐색(BFS) 의사코드 Queue를 만들고 방문한 노드를 넣는다. root를 queue에 있는 노드로 변경한다. 현재 root에서 left, right를 확인한 뒤 queue에 넣는다. queue의 값을 제거하고 결과 배열에 넣은 뒤 다음 queue의 노드를 기준으로 반복한다. queue가 비었다면 종료한다. 코드 class Node { constructor(value) { this.value = value; this.right = null; this.left = null; } } class BinarySearchTree { constructor() { this.root = null; } BFS() { const data = [], queue = []; let node = this.root; queue.push(node); while (queue.length) { node = queue.shift() data.push(node.value); if (node.left) qu...


#BFS #JavaScript #넓이우선탐색 #알고리즘 #자료구조 #트리순회

원문링크 : JS 알고리즘 18일차 - 트리 순회(BFS)