티스토리 뷰
🌲 트리의 개념
- 트리는 노드로 이루어진 자료 구조이다.
- 트리는 하나의 루트 노드를 갖는다.
- 루트 노드는 0개 이상의 자식 노드를 갖고 있다.
- 그 자식 노드 또한 0개 이상의 자식 노드를 갖고 있다.
- 노드(node)들과 노드들을 연결하는 간선(edge)들로 구성되어 있다.
- 그래프의 한 종류로, 사이클이 없는 하나의 연결 그래프이다.
트리의 개념 자세히 알아보기 ➔ https://gmlwjd9405.github.io/2018/08/12/data-structure-tree.html
🌲 이진트리
- 각 노드가 최대 2개의 자식을 갖는 트리
🌲 이진트리 순회(Tree Traversal)
✦ 전위 순회: 뿌리(root)를 먼저 방문
✦ 중위 순회: 왼쪽 하위 트리를 방문 후 뿌리(root)를 방문
✦ 후위 순회: 하위 트리 모두 방문 후 뿌리(root)를 방문
🌲 알고리즘 적용(JavaScript)
https://www.acmicpc.net/problem/1991
const fs = require('fs');
const input = fs.readFileSync('/dev/stdin').toString().trim().split('\n');
const N = +input.shift();
let result = '';
const tree = {};
for (let i = 0; i < N; i++) {
const [node, left, right] = input[i].split(' ');
tree[node] = [left, right];
}
// 전위
function preorder(node) {
if (node === ".") return;
const [left, right] = tree[node];
result += node;
preorder(left);
preorder(right);
}
// 중위
function inorder(node) {
if (node === ".") return;
const [left, right] = tree[node];
inorder(left);
result += node;
inorder(right);
}
// 후위
function postorder(node) {
if (node === ".") return;
const [left, right] = tree[node];
postorder(left);
postorder(right);
result += node;
}
preorder("A");
result += "\n";
inorder("A");
result += "\n";
postorder("A");
console.log(result);
'Programming > 알고리즘' 카테고리의 다른 글
동적계획법(DP, Dynamic Programming) (1) | 2024.11.07 |
---|---|
비트마스킹(BitMask) (1) | 2024.11.06 |
투 포인터(Two-Pointers) & 슬라이딩 윈도우(Sliding Windows) 알고리즘 (3) | 2024.11.04 |
그래프 탐색 - DFS & BFS (0) | 2024.10.30 |
완전 탐색 브루트 포스 알고리즘 (0) | 2024.10.28 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 디바운싱
- zustand
- 티스토리챌린지
- 네트워크
- React Query
- eslint
- 캡스톤디자인
- 무한스크롤
- git
- prettier
- react-query
- Firebase
- Next.js
- 핀터레스트
- 쓰로틀링
- style-lint
- Masonry 레이아웃
- 패키지 매니저
- web
- 최적화
- Network
- react
- sass
- 이브와ICT멘토링
- AI Challeng for Biodiversity
- 알고리즘
- github
- Tanstack Query
- Tanstack-Query
- 오블완
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
글 보관함