본문 바로가기
관리자

Computer Science/Algorithm

Prm 12 : Tree BFS

728x90
반응형

treeBFS

문제

임의의 tree를 구성하는 노드 중 하나의 Node 객체를 입력받아, 해당 노드를 시작으로 너비 우선 탐색(BFS, Breadth First Search)을 합니다. 이 때, 탐색되는 순서대로 노드의 값이 저장된 배열을 리턴해야 합니다.

입력

인자 1 : node

  • 'value', 'children' 속성을 갖는 객체 (Node)
  • 'node.value'는 number 타입
  • 'node.children'은 Node를 요소로 갖는 배열

출력

  • 배열을 리턴해야 합니다.

주의사항

  • 생성자 함수(Node)와 메소드(addChild)는 변경하지 않아야 합니다.

입출력 예시

let root = new Node(1); let rootChild1 = root.addChild(new Node(2)); let rootChild2 = root.addChild(new Node(3)); let leaf1 = rootChild1.addChild(new Node(4)); let leaf2 = rootChild1.addChild(new Node(5)); let output = bfs(root); console.log(output); // --> [1, 2, 3, 4, 5] leaf1.addChild(new Node(6)); rootChild2.addChild(new Node(7)); output = bfs(root); console.log(output); // --> [1, 2, 3, 4, 5, 7, 6]

 

 


 

풀이

1. bfs는 queue! 큐에 node를 넣고 맨 앞쪽의 값을 head라고 지정한다. head의 value를 결과 배열 values에 넣고, head의 children들을 순차적으로 queue에 넣는다.

2. queue.length > 0 인 동안 계속 반복한다.

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
let bfs = function (node) {
  // TODO: 여기에 코드를 작성합니다.
  let queue = [node];
  const values = [];
  while (queue.length > 0) {
    const head = queue[0];
    queue = queue.slice(1);
 
    values.push(head.value);
 
    head.children.forEach((child) => queue.push(child));
  }
  return values;
};
 
// 이 아래 코드는 변경하지 않아도 됩니다. 자유롭게 참고하세요.
let Node = function (value) {
  this.value = value;
  this.children = [];
};
 
// 위 Node 객체로 구성되는 트리는 매우 단순한 형태의 트리입니다.
// membership check(중복 확인)를 따로 하지 않습니다.
Node.prototype.addChild = function (child) {
  this.children.push(child);
  return child;
};
 
//    const root = new Node(1);
// root.addChild(new Node(2));
// root.addChild(new Node(3));
// root.addChild(new Node(4));
// root.children[0].addChild(new Node(5));
// root.children[0].addChild(new Node(6));
// root.children[0].children[0].addChild(new Node(7));
// root.children[0].children[1].addChild(new Node(8));
// root.children[1].addChild(new Node(9));
// root.children[1].addChild(new Node(10));
// root.children[1].children[1].addChild(new Node(11));
// root.children[1].children[1].addChild(new Node(12));
// root.children[2].addChild(new Node(13));
 
//bfs(root)로 실행
cs

 

728x90
반응형