-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathch2.html
More file actions
1427 lines (1309 loc) · 54.9 KB
/
ch2.html
File metadata and controls
1427 lines (1309 loc) · 54.9 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ch.2 자료구조 | CS Visualizer</title>
<link href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@300;400;500;600;700&family=Noto+Sans+KR:wght@300;400;500;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="style.css">
<style>
/* Segment Tree & Fenwick extras */
.st-array-view{display:flex;gap:4px;flex-wrap:wrap;margin-bottom:14px;}
.st-cell{min-width:40px;height:40px;display:flex;flex-direction:column;align-items:center;justify-content:center;font-family:'JetBrains Mono',monospace;font-size:0.82rem;border:1px solid var(--border);border-radius:6px;background:var(--surface2);color:var(--text);transition:all 0.25s;cursor:pointer;user-select:none;}
.st-cell:hover{border-color:var(--accent3);background:rgba(107,138,255,0.12);}
.st-cell.st-hl{border-color:var(--accent);background:rgba(0,255,170,0.18);color:var(--accent);}
.st-cell.st-range{border-color:var(--accent3);background:rgba(107,138,255,0.12);color:var(--accent3);}
.st-cell .st-idx{font-size:0.5rem;color:var(--text-dim);}
.st-input-row{display:flex;gap:8px;align-items:center;margin-bottom:12px;flex-wrap:wrap;}
.st-input{background:var(--surface2);border:1px solid var(--border);border-radius:8px;color:var(--text);font-family:'JetBrains Mono',monospace;font-size:0.9rem;padding:8px 12px;width:80px;outline:none;}
.st-input:focus{border-color:var(--accent);}
.st-select{background:var(--surface2);border:1px solid var(--border);border-radius:8px;color:var(--text);font-family:'JetBrains Mono',monospace;font-size:0.82rem;padding:8px 12px;outline:none;cursor:pointer;}
.st-result-box{font-family:'JetBrains Mono',monospace;font-size:0.88rem;color:var(--accent);margin-top:10px;padding:10px 16px;background:var(--surface2);border-radius:8px;border-left:3px solid var(--accent);min-height:36px;}
/* Fenwick */
.bit-arr-view{display:flex;gap:4px;flex-wrap:wrap;margin-bottom:8px;}
.bit-cell2{min-width:40px;height:48px;display:flex;flex-direction:column;align-items:center;justify-content:center;font-family:'JetBrains Mono',monospace;font-size:0.82rem;border:1px solid var(--border);border-radius:6px;background:var(--surface2);color:var(--text);transition:all 0.25s;}
.bit-cell2 .bc-idx{font-size:0.5rem;color:var(--text-dim);}
.bit-cell2.bit-hl{border-color:var(--accent4);background:rgba(255,170,0,0.15);color:var(--accent4);}
.bit-cell2.bit-hl2{border-color:var(--accent2);background:rgba(255,107,157,0.15);color:var(--accent2);}
.bit-label{font-family:'JetBrains Mono',monospace;font-size:0.72rem;color:var(--text-dim);margin-bottom:4px;}
.bit-input{background:var(--surface2);border:1px solid var(--border);border-radius:8px;color:var(--text);font-family:'JetBrains Mono',monospace;font-size:0.9rem;padding:8px 12px;width:80px;outline:none;}
.bit-input:focus{border-color:var(--accent);}
.bit-result-box{font-family:'JetBrains Mono',monospace;font-size:0.88rem;color:var(--accent4);margin-top:10px;padding:10px 16px;background:var(--surface2);border-radius:8px;border-left:3px solid var(--accent4);min-height:36px;}
</style>
</head>
<body class="page-body">
<nav class="page-nav">
<a href="index.html" class="nav-back">← 홈</a>
<div class="nav-chapter-title" style="color:var(--accent3)">Ch.2 — 자료구조</div>
<div class="nav-sections">
<a href="#linkedlist" class="nav-sec-link">배열/LL</a>
<a href="#stackqueue" class="nav-sec-link">스택&큐</a>
<a href="#bst" class="nav-sec-link">BST</a>
<a href="#hashtable" class="nav-sec-link">해시테이블</a>
<a href="#heap" class="nav-sec-link">이진힙</a>
<a href="#unionfind" class="nav-sec-link">Union-Find</a>
<a href="#segtree" class="nav-sec-link">세그먼트트리</a>
<a href="#fenwick" class="nav-sec-link">펜윅트리</a>
<a href="#trie" class="nav-sec-link">트라이</a>
</div>
</nav>
<!-- BANNER -->
<div class="chapter-banner cb2">
<div class="ch-num">CHAPTER 02</div>
<h2>자료구조</h2>
<p class="ch-desc">데이터를 효율적으로 저장하고 접근하는 방법. 알고리즘의 성능은 자료구조 선택에 달려 있습니다.</p>
<div class="ch-pills">
<span>배열/링크드리스트</span><span>스택&큐</span><span>BST</span><span>해시테이블</span>
<span>이진힙</span><span>Union-Find</span><span>세그먼트트리</span><span>펜윅트리</span>
</div>
</div>
<!-- ===== 1. ARRAY vs LINKED LIST ===== -->
<section id="linkedlist">
<div class="section-header">
<div class="tag tag-ch2">01 — 배열 vs 링크드 리스트</div>
<h2>Array vs Linked List</h2>
<p>같은 데이터를 배열과 링크드 리스트로 동시에 표현합니다. 메모리 배치와 포인터 구조의 차이를 직접 확인해 보세요.</p>
</div>
<div class="viz-box">
<div class="controls">
<button class="btn" onclick="llAppend()">끝에 추가</button>
<button class="btn" onclick="llInsertFront()">앞에 삽입</button>
<button class="btn" onclick="llRemoveLast()">마지막 삭제</button>
<button class="btn danger" onclick="llClear()">초기화</button>
</div>
<div class="ll-compare">
<div>
<div class="ll-panel-title" style="color:var(--accent3)">▦ Array <small style="color:var(--text-dim);font-size:0.72rem;">연속 메모리</small></div>
<div class="arr-visual" id="arr-visual"></div>
<div class="complexity-row">
<span class="complexity-badge fast">접근 O(1)</span>
<span class="complexity-badge fast">끝추가 O(1)</span>
<span class="complexity-badge slow">앞삽입 O(n)</span>
</div>
</div>
<div>
<div class="ll-panel-title" style="color:var(--accent)">⟶ Linked List <small style="color:var(--text-dim);font-size:0.72rem;">포인터 연결</small></div>
<div class="ll-visual" id="ll-visual"></div>
<div class="complexity-row">
<span class="complexity-badge slow">접근 O(n)</span>
<span class="complexity-badge fast">앞삽입 O(1)</span>
<span class="complexity-badge fast">끝추가 O(n)</span>
</div>
</div>
</div>
</div>
</section>
<div class="section-divider"></div>
<!-- ===== 2. STACK & QUEUE ===== -->
<section id="stackqueue">
<div class="section-header">
<div class="tag tag-ch2">02 — 스택 & 큐</div>
<h2>Stack & Queue</h2>
<p>스택(LIFO)과 큐(FIFO)는 가장 기본적인 선형 자료구조입니다. 함수 호출 스택, 프린터 대기열 등 실생활에서도 쓰입니다.</p>
</div>
<div class="viz-box">
<div class="ds-container">
<div class="ds-panel">
<h3 style="color:var(--accent3)">⬆ Stack <small style="font-size:0.72rem;color:var(--text-dim);">LIFO</small></h3>
<div class="ds-visual" id="stack-visual">
<div class="ds-empty">비어있음</div>
</div>
<div class="ds-controls">
<button class="btn" onclick="stackPush()">Push</button>
<button class="btn" onclick="stackPop()">Pop</button>
<button class="btn danger" onclick="stackClear()">Clear</button>
</div>
</div>
<div class="ds-panel">
<h3 style="color:var(--accent4)">→ Queue <small style="font-size:0.72rem;color:var(--text-dim);">FIFO</small></h3>
<div class="ds-visual" id="queue-visual">
<div class="ds-empty">비어있음</div>
</div>
<div class="ds-controls">
<button class="btn" onclick="queueEnqueue()">Enqueue</button>
<button class="btn" onclick="queueDequeue()">Dequeue</button>
<button class="btn danger" onclick="queueClear()">Clear</button>
</div>
</div>
</div>
</div>
</section>
<div class="section-divider"></div>
<!-- ===== 3. BST ===== -->
<section id="bst">
<div class="section-header">
<div class="tag tag-ch2">03 — 이진 탐색 트리</div>
<h2>Binary Search Tree</h2>
<p>왼쪽 자식 < 부모 < 오른쪽 자식. 균형 잡힌 BST는 삽입·탐색·삭제 모두 O(log n)에 처리합니다.</p>
</div>
<div class="viz-box">
<div class="bst-controls">
<input type="number" id="bst-input" class="bst-input" placeholder="1–99" min="1" max="99"
onkeydown="if(event.key==='Enter')bstInsert()">
<button class="btn" onclick="bstInsert()">삽입</button>
<button class="btn" onclick="bstSearch()">탐색</button>
<button class="btn" onclick="bstRandom()">랜덤 5개</button>
<button class="btn danger" onclick="bstReset()">초기화</button>
</div>
<div class="bst-info" id="bst-info">값을 입력하고 삽입해 보세요.</div>
<canvas id="bst-canvas" height="380" style="background:var(--surface2);border-radius:8px;margin-top:8px;"></canvas>
</div>
</section>
<div class="section-divider"></div>
<!-- ===== 4. HASH TABLE ===== -->
<section id="hashtable">
<div class="section-header">
<div class="tag tag-ch2">04 — 해시 테이블</div>
<h2>Hash Table</h2>
<p>키를 해시 함수로 변환해 버킷에 저장합니다. 평균 O(1) 탐색·삽입. 충돌은 체이닝으로 해결합니다.</p>
</div>
<div class="viz-box">
<div class="ht-inputs">
<input type="text" id="ht-key" class="ht-input" placeholder="Key" maxlength="12"
onkeydown="if(event.key==='Enter')htInsert()">
<input type="text" id="ht-val" class="ht-input" placeholder="Value" maxlength="16"
onkeydown="if(event.key==='Enter')htInsert()">
<button class="btn" onclick="htInsert()">저장</button>
<button class="btn" onclick="htDelete()">삭제</button>
<button class="btn danger" onclick="htClear()">초기화</button>
</div>
<div class="ht-formula" id="ht-formula">hash(key) = Σ charCode % 8</div>
<div class="ht-visual" id="ht-visual"></div>
</div>
</section>
<div class="section-divider"></div>
<!-- ===== 5. BINARY HEAP ===== -->
<section id="heap">
<div class="section-header">
<div class="tag tag-ch2">05 — 이진 힙</div>
<h2>Binary Heap</h2>
<p>힙은 완전 이진 트리 형태로, 부모 노드가 항상 자식보다 작은(Min-Heap) 또는 큰(Max-Heap) 자료구조입니다. 삽입과 최솟값 추출이 O(log n)으로, 우선순위 큐 구현에 사용됩니다.</p>
</div>
<div class="viz-box">
<div class="heap-controls">
<input type="number" id="heap-input" class="heap-input" placeholder="1–99" min="1" max="99"
onkeydown="if(event.key==='Enter')heapInsert()">
<button class="btn" onclick="heapInsert()">삽입</button>
<button class="btn" id="heap-extract-btn" onclick="heapExtract()">Extract Min</button>
<button class="btn danger" onclick="heapReset()">초기화</button>
<button class="btn" id="heap-toggle-btn" onclick="heapToggle()" style="margin-left:8px;">Max-Heap으로 전환</button>
</div>
<div class="bit-label" style="margin-bottom:4px;">배열 표현</div>
<div class="heap-array-view" id="heap-array-view"></div>
<div class="bit-label" style="margin-bottom:4px;">트리 시각화</div>
<canvas id="heap-canvas" height="320" style="background:var(--surface2);border-radius:8px;"></canvas>
<div class="info-text" id="heap-info">값을 입력하고 삽입해 보세요.</div>
</div>
</section>
<div class="section-divider"></div>
<!-- ===== 6. UNION-FIND ===== -->
<section id="unionfind">
<div class="section-header">
<div class="tag tag-ch2">06 — Union-Find</div>
<h2>Union-Find (서로소 집합)</h2>
<p>서로소 집합(Disjoint Set) 자료구조입니다. 여러 원소를 집합으로 묶고, 두 원소가 같은 집합에 있는지 O(α(n)) ≈ O(1)에 판단합니다. 크루스칼 알고리즘, 네트워크 연결성 판단에 핵심적으로 사용됩니다.</p>
</div>
<div class="viz-box">
<div class="controls">
<button class="btn" onclick="ufUnion()">합치기 (Union)</button>
<button class="btn" onclick="ufSame()">같은 집합?</button>
<button class="btn danger" onclick="ufInit()">초기화</button>
</div>
<div style="font-family:'JetBrains Mono',monospace;font-size:0.8rem;color:var(--text-dim);margin-bottom:8px;">
노드를 두 개 클릭해서 선택하세요. (선택됨: <span id="uf-selected-label" style="color:var(--accent);">없음</span>)
</div>
<div class="uf-nodes" id="uf-nodes"></div>
<div class="info-text" id="uf-info">두 노드를 클릭해 선택한 뒤 Union 또는 Find를 실행하세요.</div>
<canvas id="uf-canvas" height="180" style="background:var(--surface2);border-radius:8px;margin-top:12px;"></canvas>
</div>
</section>
<div class="section-divider"></div>
<!-- ===== 7. SEGMENT TREE ===== -->
<section id="segtree">
<div class="section-header">
<div class="tag tag-ch2">07 — 세그먼트 트리</div>
<h2>Segment Tree</h2>
<p>배열의 구간 합(또는 최솟값/최댓값)을 O(log n)에 구할 수 있는 트리 자료구조입니다. 게임 랭킹, 주식 데이터 분석 등에 활용됩니다.</p>
</div>
<div class="viz-box">
<div style="font-family:'JetBrains Mono',monospace;font-size:0.8rem;color:var(--text-dim);margin-bottom:8px;">
셀을 클릭하면 값이 재랜덤화됩니다.
</div>
<div class="st-array-view" id="st-array-view"></div>
<div class="st-input-row">
<select class="st-select" id="st-query-type" onchange="stQueryTypeChange()">
<option value="sum">Sum Query</option>
<option value="min">Min Query</option>
<option value="max">Max Query</option>
</select>
<span style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim);">L:</span>
<input type="number" id="st-l" class="st-input" min="0" max="7" value="2">
<span style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim);">R:</span>
<input type="number" id="st-r" class="st-input" min="0" max="7" value="5">
<button class="btn" onclick="stRunQuery()">쿼리 실행</button>
<button class="btn" onclick="stRandomize()">배열 재랜덤</button>
</div>
<div class="st-result-box" id="st-result">쿼리를 실행하면 결과가 표시됩니다.</div>
<div class="bit-label" style="margin-top:14px;margin-bottom:4px;">세그먼트 트리</div>
<canvas id="st-canvas" height="320" style="background:var(--surface2);border-radius:8px;"></canvas>
</div>
</section>
<div class="section-divider"></div>
<!-- ===== 8. FENWICK TREE ===== -->
<section id="fenwick">
<div class="section-header">
<div class="tag tag-ch2">08 — 펜윅 트리 (BIT)</div>
<h2>Fenwick Tree (Binary Indexed Tree)</h2>
<p>Binary Indexed Tree(BIT)라고도 합니다. 세그먼트 트리보다 구현이 간단하면서도 구간 합 쿼리와 점 업데이트를 O(log n)에 처리합니다. 내부적으로 비트 연산(i & -i)을 사용하는 것이 특징입니다.</p>
</div>
<div class="viz-box">
<div class="bit-label">원본 배열 (1-indexed)</div>
<div class="bit-arr-view" id="bit-orig-view"></div>
<div class="bit-label" style="margin-top:8px;">BIT 배열 (i & -i 구조)</div>
<div class="bit-arr-view" id="bit-bit-view"></div>
<div style="display:flex;gap:24px;flex-wrap:wrap;margin-top:16px;">
<div>
<div class="bit-label" style="margin-bottom:6px;">점 업데이트</div>
<div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;">
<span style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim);">인덱스:</span>
<input type="number" id="bit-upd-i" class="bit-input" min="1" max="8" value="3">
<span style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim);">Δ:</span>
<input type="number" id="bit-upd-d" class="bit-input" value="5">
<button class="btn" onclick="bitDoUpdate()">업데이트</button>
</div>
</div>
<div>
<div class="bit-label" style="margin-bottom:6px;">구간 합 쿼리</div>
<div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap;">
<span style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim);">L:</span>
<input type="number" id="bit-ql" class="bit-input" min="1" max="8" value="2">
<span style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;color:var(--text-dim);">R:</span>
<input type="number" id="bit-qr" class="bit-input" min="1" max="8" value="6">
<button class="btn" onclick="bitDoQuery()">쿼리</button>
</div>
</div>
</div>
<div class="bit-result-box" id="bit-result">업데이트 또는 쿼리를 실행하면 결과가 표시됩니다.</div>
</div>
</section>
<div class="section-divider"></div>
<!-- SECTION 9: 트라이 (Trie) -->
<section id="trie">
<div class="section-header">
<div class="tag tag-ch2">09 — TRIE</div>
<h2>트라이 (Trie / Prefix Tree)</h2>
<p>문자열을 트리 구조로 저장하여 접두사 검색을 O(L) (L=문자열 길이)에 처리합니다. 자동완성, 사전, IP 라우팅에 사용됩니다.</p>
</div>
<div class="viz-box">
<div class="controls">
<input type="text" id="trieInput" class="sa-input" placeholder="단어 입력..." maxlength="10"
style="width:160px;" onkeydown="if(event.key==='Enter')trieInsert()">
<button class="btn" onclick="trieInsert()">삽입</button>
<button class="btn" onclick="trieSearch()">검색</button>
<button class="btn danger" onclick="trieReset()">초기화</button>
</div>
<div id="trieMsg" style="font-family:'JetBrains Mono',monospace;font-size:0.8rem;color:var(--text-dim);margin-bottom:12px;min-height:20px;"></div>
<div id="trieViz" style="overflow-x:auto;padding:10px 0;"></div>
<!-- 자동완성 데모 -->
<div style="border-top:1px solid var(--border);padding-top:20px;margin-top:20px;">
<div style="font-family:'JetBrains Mono',monospace;font-size:0.82rem;font-weight:700;margin-bottom:10px;">자동완성 시뮬레이션</div>
<div style="display:flex;gap:8px;margin-bottom:10px;flex-wrap:wrap;align-items:center;">
<input type="text" id="acInput" class="sa-input" placeholder="접두사 입력..." maxlength="8"
style="width:160px;" oninput="autocomplete()">
<span style="font-family:'JetBrains Mono',monospace;font-size:0.76rem;color:var(--text-dim)">현재 단어:</span>
<div id="acWords" style="display:flex;gap:6px;flex-wrap:wrap;"></div>
</div>
</div>
<div class="info-text" style="margin-top:16px;">
트라이는 각 노드가 한 글자를 나타내는 트리. 삽입/검색 O(L). 해시테이블보다 접두사 검색에 유리.
단점: 메모리 사용량 ↑. 활용: 검색 자동완성, 맞춤법 검사, IP 라우팅 테이블.
</div>
</div>
</section>
<footer>
<p>CS Visualizer · MIT License · <a href="https://github.com/M1zz/cs-visualizer" style="color:var(--accent);text-decoration:none;">GitHub</a></p>
</footer>
<script>
/* ======================================================
SECTION 1 — Array vs Linked List
====================================================== */
let llData = [3, 7, 12];
function llAppend() {
const v = Math.floor(Math.random() * 90) + 1;
llData.push(v);
renderLL();
renderArr();
}
function llInsertFront() {
const v = Math.floor(Math.random() * 90) + 1;
llData.unshift(v);
renderLL();
renderArr();
}
function llRemoveLast() {
if (llData.length === 0) return;
llData.pop();
renderLL();
renderArr();
}
function llClear() {
llData = [];
renderLL();
renderArr();
}
function renderArr() {
const el = document.getElementById('arr-visual');
if (llData.length === 0) { el.innerHTML = '<span style="font-family:\'JetBrains Mono\',monospace;font-size:0.78rem;color:var(--text-dim);opacity:0.5;">비어있음</span>'; return; }
el.innerHTML = llData.map((v, i) =>
`<div class="arr-cell"><span>${v}</span><span class="arr-idx">[${i}]</span></div>`
).join('');
}
function renderLL() {
const el = document.getElementById('ll-visual');
if (llData.length === 0) { el.innerHTML = '<span class="ll-null">NULL</span>'; return; }
let html = '';
llData.forEach((v, i) => {
const isLast = i === llData.length - 1;
html += `<div class="ll-node">
<div class="ll-val-box">${v}</div>
<div class="ll-ptr-box" title="${isLast ? 'null' : 'next'}">→</div>
</div>`;
if (!isLast) html += '<span class="ll-arrow"></span>';
});
html += '<span class="ll-null">NULL</span>';
el.innerHTML = html;
}
renderArr();
renderLL();
/* ======================================================
SECTION 2 — Stack & Queue
====================================================== */
let stackData = [], queueData = [];
const COLORS = ['#6b8aff','#ff6b9d','#ffaa00','#00ffaa','#22d3ee','#a855f7','#f97316','#00cc88'];
function rndVal() { return Math.floor(Math.random() * 90) + 10; }
function rndColor() { return COLORS[Math.floor(Math.random() * COLORS.length)]; }
function renderStack() {
const el = document.getElementById('stack-visual');
if (stackData.length === 0) { el.innerHTML = '<div class="ds-empty">비어있음</div>'; return; }
// top is last in array, show top first (bottom → top order reversed for display)
el.innerHTML = [...stackData].reverse().map((item, i) => {
const isTop = i === 0;
return `<div class="ds-item stack-item" style="border-color:${item.color};background:${item.color}22;">
<span style="color:${item.color}">${item.val}</span>
<span class="idx">${isTop ? '← TOP' : ''}</span>
</div>`;
}).join('');
}
function renderQueue() {
const el = document.getElementById('queue-visual');
if (queueData.length === 0) { el.innerHTML = '<div class="ds-empty">비어있음</div>'; return; }
el.innerHTML = queueData.map((item, i) => {
const isFront = i === 0;
const isBack = i === queueData.length - 1;
return `<div class="ds-item queue-item" style="border-color:${item.color};background:${item.color}22;">
<span style="color:${item.color};font-size:0.65rem;">${isFront ? 'FRONT→' : ''}</span>
<span style="color:${item.color}">${item.val}</span>
<span class="idx">${isBack ? '←BACK' : ''}</span>
</div>`;
}).join('');
}
function stackPush() {
stackData.push({ val: rndVal(), color: rndColor() });
renderStack();
}
function stackPop() {
if (stackData.length === 0) return;
stackData.pop();
renderStack();
}
function stackClear() { stackData = []; renderStack(); }
function queueEnqueue() {
queueData.push({ val: rndVal(), color: rndColor() });
renderQueue();
}
function queueDequeue() {
if (queueData.length === 0) return;
queueData.shift();
renderQueue();
}
function queueClear() { queueData = []; renderQueue(); }
/* ======================================================
SECTION 3 — BST
====================================================== */
let bstRoot = null;
let bstHighlighted = [];
function bstInsertNode(root, val) {
if (!root) return { val, left: null, right: null, x: 0, y: 0 };
if (val < root.val) root.left = bstInsertNode(root.left, val);
else if (val > root.val) root.right = bstInsertNode(root.right, val);
return root;
}
function bstSearchNode(root, val, path) {
if (!root) return false;
path.push(root.val);
if (root.val === val) return true;
if (val < root.val) return bstSearchNode(root.left, val, path);
return bstSearchNode(root.right, val, path);
}
function bstInsert() {
const inp = document.getElementById('bst-input');
const v = parseInt(inp.value);
if (isNaN(v) || v < 1 || v > 99) { document.getElementById('bst-info').textContent = '1~99 사이 값을 입력하세요.'; return; }
bstRoot = bstInsertNode(bstRoot, v);
bstHighlighted = [v];
document.getElementById('bst-info').textContent = `${v} 삽입 완료`;
inp.value = '';
drawBST();
}
function bstSearch() {
const inp = document.getElementById('bst-input');
const v = parseInt(inp.value);
if (isNaN(v)) { document.getElementById('bst-info').textContent = '탐색할 값을 입력하세요.'; return; }
const path = [];
const found = bstSearchNode(bstRoot, v, path);
bstHighlighted = path;
document.getElementById('bst-info').textContent =
found ? `✓ ${v} 발견! 경로: ${path.join(' → ')}` : `✗ ${v} 없음. 탐색 경로: ${path.join(' → ')}`;
drawBST();
}
function bstReset() {
bstRoot = null;
bstHighlighted = [];
document.getElementById('bst-info').textContent = '초기화되었습니다.';
drawBST();
}
function bstRandom() {
bstRoot = null;
bstHighlighted = [];
const used = new Set();
for (let i = 0; i < 5; i++) {
let v;
do { v = Math.floor(Math.random() * 90) + 5; } while (used.has(v));
used.add(v);
bstRoot = bstInsertNode(bstRoot, v);
}
document.getElementById('bst-info').textContent = `랜덤 5개 삽입: ${[...used].join(', ')}`;
drawBST();
}
function assignPos(node, depth, leftBound, rightBound) {
if (!node) return;
const x = (leftBound + rightBound) / 2;
node.x = x;
node.y = depth * 72 + 48;
assignPos(node.left, depth + 1, leftBound, x);
assignPos(node.right, depth + 1, x, rightBound);
}
function drawBST() {
const canvas = document.getElementById('bst-canvas');
const W = canvas.parentElement.clientWidth - 64;
canvas.width = Math.max(W, 300);
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (!bstRoot) return;
assignPos(bstRoot, 0, 0, canvas.width);
function drawEdges(node) {
if (!node) return;
const hlSet = new Set(bstHighlighted);
if (node.left) {
const bothHl = hlSet.has(node.val) && hlSet.has(node.left.val);
ctx.beginPath();
ctx.moveTo(node.x, node.y);
ctx.lineTo(node.left.x, node.left.y);
ctx.strokeStyle = bothHl ? '#6b8aff' : '#2a2a3a';
ctx.lineWidth = bothHl ? 2.5 : 1.5;
ctx.stroke();
drawEdges(node.left);
}
if (node.right) {
const bothHl = hlSet.has(node.val) && hlSet.has(node.right.val);
ctx.beginPath();
ctx.moveTo(node.x, node.y);
ctx.lineTo(node.right.x, node.right.y);
ctx.strokeStyle = bothHl ? '#6b8aff' : '#2a2a3a';
ctx.lineWidth = bothHl ? 2.5 : 1.5;
ctx.stroke();
drawEdges(node.right);
}
}
drawEdges(bstRoot);
function drawNodes(node) {
if (!node) return;
const hl = bstHighlighted.includes(node.val);
const isTarget = hl && bstHighlighted[bstHighlighted.length - 1] === node.val;
ctx.beginPath();
ctx.arc(node.x, node.y, 20, 0, Math.PI * 2);
ctx.fillStyle = isTarget ? 'rgba(0,255,170,0.25)' : hl ? 'rgba(107,138,255,0.2)' : '#1a1a28';
ctx.fill();
ctx.strokeStyle = isTarget ? '#00ffaa' : hl ? '#6b8aff' : '#2a2a3a';
ctx.lineWidth = hl ? 2.5 : 1.5;
ctx.stroke();
ctx.fillStyle = isTarget ? '#00ffaa' : hl ? '#6b8aff' : '#e0e0f0';
ctx.font = 'bold 13px JetBrains Mono, monospace';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(node.val, node.x, node.y);
drawNodes(node.left);
drawNodes(node.right);
}
drawNodes(bstRoot);
}
window.addEventListener('resize', drawBST);
/* ======================================================
SECTION 4 — Hash Table
====================================================== */
const HT_BUCKETS = 8;
let htTable = Array.from({ length: HT_BUCKETS }, () => []);
let htLastBucket = -1;
function htHash(key) {
let h = 0;
for (const c of key) h += c.charCodeAt(0);
return h % HT_BUCKETS;
}
function htInsert() {
const key = document.getElementById('ht-key').value.trim();
const val = document.getElementById('ht-val').value.trim();
if (!key) return;
const h = htHash(key);
htLastBucket = h;
const bucket = htTable[h];
const existing = bucket.findIndex(e => e.k === key);
if (existing >= 0) bucket[existing].v = val;
else bucket.push({ k: key, v: val });
const codes = [...key].map(c => c.charCodeAt(0)).join('+');
const sum = [...key].reduce((a, c) => a + c.charCodeAt(0), 0);
document.getElementById('ht-formula').textContent =
`hash("${key}") = (${codes}) % 8 = ${sum} % 8 = ${h}`;
document.getElementById('ht-key').value = '';
document.getElementById('ht-val').value = '';
renderHT();
}
function htDelete() {
const key = document.getElementById('ht-key').value.trim();
if (!key) return;
const h = htHash(key);
htLastBucket = h;
htTable[h] = htTable[h].filter(e => e.k !== key);
document.getElementById('ht-key').value = '';
renderHT();
}
function htClear() {
htTable = Array.from({ length: HT_BUCKETS }, () => []);
htLastBucket = -1;
document.getElementById('ht-formula').textContent = 'hash(key) = Σ charCode % 8';
renderHT();
}
function renderHT() {
const el = document.getElementById('ht-visual');
el.innerHTML = htTable.map((bucket, i) => {
const isHl = i === htLastBucket;
let chain = '';
if (bucket.length === 0) {
chain = '<span class="ht-empty">— empty —</span>';
} else {
bucket.forEach((e, j) => {
if (j > 0) chain += '<span class="ht-link">→</span>';
chain += `<div class="ht-entry"><span class="ht-key">${e.k}</span><span class="ht-sep">:</span><span class="ht-val">${e.v}</span></div>`;
});
}
return `<div class="ht-bucket${isHl ? ' ht-highlight' : ''}">
<span class="ht-idx">[${i}]</span>
<div class="ht-chain">${chain}</div>
</div>`;
}).join('');
}
renderHT();
/* ======================================================
SECTION 5 — Binary Heap
====================================================== */
let heapArr = [];
let heapIsMin = true;
let heapHighlight = new Set();
function heapToggle() {
heapIsMin = !heapIsMin;
document.getElementById('heap-toggle-btn').textContent =
heapIsMin ? 'Max-Heap으로 전환' : 'Min-Heap으로 전환';
document.getElementById('heap-extract-btn').textContent =
heapIsMin ? 'Extract Min' : 'Extract Max';
// rebuild heap from existing values
const vals = [...heapArr];
heapArr = [];
vals.forEach(v => {
heapArr.push(v);
heapBubbleUp(heapArr.length - 1);
});
heapHighlight = new Set();
document.getElementById('heap-info').textContent =
`${heapIsMin ? 'Min' : 'Max'}-Heap으로 전환되었습니다.`;
renderHeapArray();
drawHeap();
}
function heapCmp(a, b) {
return heapIsMin ? a < b : a > b;
}
function heapBubbleUp(i) {
while (i > 0) {
const parent = Math.floor((i - 1) / 2);
if (heapCmp(heapArr[i], heapArr[parent])) {
[heapArr[i], heapArr[parent]] = [heapArr[parent], heapArr[i]];
heapHighlight.add(i);
heapHighlight.add(parent);
i = parent;
} else break;
}
}
function heapBubbleDown(i) {
const n = heapArr.length;
while (true) {
let best = i;
const l = 2 * i + 1, r = 2 * i + 2;
if (l < n && heapCmp(heapArr[l], heapArr[best])) best = l;
if (r < n && heapCmp(heapArr[r], heapArr[best])) best = r;
if (best === i) break;
[heapArr[i], heapArr[best]] = [heapArr[best], heapArr[i]];
heapHighlight.add(i);
heapHighlight.add(best);
i = best;
}
}
function heapInsert() {
const inp = document.getElementById('heap-input');
const v = parseInt(inp.value);
if (isNaN(v) || v < 1 || v > 99) {
document.getElementById('heap-info').textContent = '1~99 사이 값을 입력하세요.';
return;
}
heapHighlight = new Set();
heapArr.push(v);
heapBubbleUp(heapArr.length - 1);
document.getElementById('heap-info').textContent =
`${v} 삽입 완료. Bubble-up으로 힙 속성 복원. (힙 크기: ${heapArr.length})`;
inp.value = '';
renderHeapArray();
drawHeap();
// fade highlight
setTimeout(() => { heapHighlight = new Set(); renderHeapArray(); drawHeap(); }, 800);
}
function heapExtract() {
if (heapArr.length === 0) {
document.getElementById('heap-info').textContent = '힙이 비어있습니다.';
return;
}
const top = heapArr[0];
heapHighlight = new Set([0, heapArr.length - 1]);
heapArr[0] = heapArr[heapArr.length - 1];
heapArr.pop();
heapBubbleDown(0);
document.getElementById('heap-info').textContent =
`${heapIsMin ? '최솟값' : '최댓값'} ${top} 추출 완료. Bubble-down으로 힙 속성 복원.`;
renderHeapArray();
drawHeap();
setTimeout(() => { heapHighlight = new Set(); renderHeapArray(); drawHeap(); }, 800);
}
function heapReset() {
heapArr = [];
heapHighlight = new Set();
document.getElementById('heap-info').textContent = '힙이 초기화되었습니다.';
renderHeapArray();
drawHeap();
}
function renderHeapArray() {
const el = document.getElementById('heap-array-view');
if (heapArr.length === 0) {
el.innerHTML = '<span style="font-family:\'JetBrains Mono\',monospace;font-size:0.78rem;color:var(--text-dim);opacity:0.5;">비어있음</span>';
return;
}
el.innerHTML = heapArr.map((v, i) => {
const hl = heapHighlight.has(i);
return `<div class="heap-cell${hl ? ' hl' : ''}" title="index ${i}">${v}</div>`;
}).join('');
}
function drawHeap() {
const canvas = document.getElementById('heap-canvas');
const W = canvas.parentElement.clientWidth - 64;
canvas.width = Math.max(W, 300);
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
const n = heapArr.length;
if (n === 0) return;
// Compute positions
const positions = [];
function pos(i, depth, lo, hi) {
if (i >= n) return;
const x = (lo + hi) / 2;
const y = depth * 68 + 48;
positions[i] = { x, y };
pos(2 * i + 1, depth + 1, lo, (lo + hi) / 2);
pos(2 * i + 2, depth + 1, (lo + hi) / 2, hi);
}
pos(0, 0, 0, canvas.width);
// Draw edges
for (let i = 0; i < n; i++) {
const l = 2 * i + 1, r = 2 * i + 2;
if (positions[i]) {
if (l < n && positions[l]) {
const hl = heapHighlight.has(i) && heapHighlight.has(l);
ctx.beginPath();
ctx.moveTo(positions[i].x, positions[i].y);
ctx.lineTo(positions[l].x, positions[l].y);
ctx.strokeStyle = hl ? (heapIsMin ? '#00ffaa' : '#ff6b9d') : '#2a2a3a';
ctx.lineWidth = hl ? 2.5 : 1.5;
ctx.stroke();
}
if (r < n && positions[r]) {
const hl = heapHighlight.has(i) && heapHighlight.has(r);
ctx.beginPath();
ctx.moveTo(positions[i].x, positions[i].y);
ctx.lineTo(positions[r].x, positions[r].y);
ctx.strokeStyle = hl ? (heapIsMin ? '#00ffaa' : '#ff6b9d') : '#2a2a3a';
ctx.lineWidth = hl ? 2.5 : 1.5;
ctx.stroke();
}
}
}
// Draw nodes
for (let i = 0; i < n; i++) {
if (!positions[i]) continue;
const { x, y } = positions[i];
const hl = heapHighlight.has(i);
const isRoot = i === 0;
ctx.beginPath();
ctx.arc(x, y, 22, 0, Math.PI * 2);
const baseColor = heapIsMin ? '#00ffaa' : '#ff6b9d';
ctx.fillStyle = isRoot ? (heapIsMin ? 'rgba(0,255,170,0.2)' : 'rgba(255,107,157,0.2)') :
hl ? `${baseColor}33` : '#1a1a28';
ctx.fill();
ctx.strokeStyle = isRoot ? baseColor : hl ? baseColor : '#2a2a3a';
ctx.lineWidth = (isRoot || hl) ? 2.5 : 1.5;
ctx.stroke();
ctx.fillStyle = isRoot ? baseColor : hl ? baseColor : '#e0e0f0';
ctx.font = 'bold 13px JetBrains Mono, monospace';
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
ctx.fillText(heapArr[i], x, y);
}
}
renderHeapArray();
drawHeap();
window.addEventListener('resize', drawHeap);
/* ======================================================
SECTION 6 — Union-Find
====================================================== */
let ufParent = [], ufRank = [], ufSelected = [];
const UF_N = 8;
const UF_COLORS = ['#6b8aff','#ff6b9d','#ffaa00','#00ffaa','#22d3ee','#a855f7','#f97316','#00cc88'];
function ufInit() {
ufParent = Array.from({ length: UF_N }, (_, i) => i);
ufRank = new Array(UF_N).fill(0);
ufSelected = [];
document.getElementById('uf-info').textContent = '모든 노드가 개별 집합으로 초기화되었습니다.';
renderUF();
}
function ufFind(x) {
if (ufParent[x] !== x) ufParent[x] = ufFind(ufParent[x]); // path compression
return ufParent[x];
}
function ufUnion() {
if (ufSelected.length < 2) {
document.getElementById('uf-info').textContent = '노드를 두 개 선택해 주세요.';
return;
}
const a = ufSelected[0], b = ufSelected[1];
const ra = ufFind(a), rb = ufFind(b);
if (ra === rb) {
document.getElementById('uf-info').textContent = `노드 ${a}와 ${b}는 이미 같은 집합입니다.`;
} else {
if (ufRank[ra] < ufRank[rb]) ufParent[ra] = rb;
else if (ufRank[ra] > ufRank[rb]) ufParent[rb] = ra;
else { ufParent[rb] = ra; ufRank[ra]++; }
document.getElementById('uf-info').textContent =
`노드 ${a}와 ${b}를 합쳤습니다. 공통 루트: ${ufFind(a)}`;
}
ufSelected = [];
renderUF();
}
function ufSame() {
if (ufSelected.length < 2) {
document.getElementById('uf-info').textContent = '노드를 두 개 선택해 주세요.';
return;
}
const a = ufSelected[0], b = ufSelected[1];
const same = ufFind(a) === ufFind(b);
document.getElementById('uf-info').textContent =
same ? `✓ 노드 ${a}와 ${b}는 같은 집합 (루트: ${ufFind(a)})입니다.`
: `✗ 노드 ${a}와 ${b}는 다른 집합입니다. (루트: ${ufFind(a)} vs ${ufFind(b)})`;
ufSelected = [];
renderUF();
}
function renderUF() {
// Color by root
const rootColorMap = {};
let colorIdx = 0;
for (let i = 0; i < UF_N; i++) {
const r = ufFind(i);
if (!(r in rootColorMap)) rootColorMap[r] = UF_COLORS[colorIdx++ % UF_COLORS.length];
}
const nodesEl = document.getElementById('uf-nodes');
nodesEl.innerHTML = '';
for (let i = 0; i < UF_N; i++) {
const r = ufFind(i);
const color = rootColorMap[r];
const isSelected = ufSelected.includes(i);
const node = document.createElement('div');
node.className = 'uf-node' + (isSelected ? ' selected' : '');
node.style.borderColor = color;
node.style.background = isSelected ? color + '33' : color + '18';
node.innerHTML = `<span style="color:${color};font-size:1rem;font-weight:700;">${i}</span><span class="uf-parent">p:${ufParent[i]}</span>`;
node.onclick = () => {
if (ufSelected.includes(i)) {
ufSelected = ufSelected.filter(x => x !== i);
} else if (ufSelected.length < 2) {
ufSelected.push(i);
} else {
ufSelected = [i];
}
const lbl = ufSelected.length === 0 ? '없음' : ufSelected.join(', ');
document.getElementById('uf-selected-label').textContent = lbl;
renderUF();
};
nodesEl.appendChild(node);
}
// Update label
const lbl = ufSelected.length === 0 ? '없음' : ufSelected.join(', ');
document.getElementById('uf-selected-label').textContent = lbl;
// Draw canvas
drawUF(rootColorMap);
}
function drawUF(rootColorMap) {
const canvas = document.getElementById('uf-canvas');
const W = canvas.parentElement.clientWidth - 64;
canvas.width = Math.max(W, 300);
const ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Place nodes in a row
const nodeRadius = 22;
const nodePositions = [];
const spacing = Math.min((canvas.width - 60) / UF_N, 80);
const startX = (canvas.width - spacing * (UF_N - 1)) / 2;
const nodeY = 130;
for (let i = 0; i < UF_N; i++) {
nodePositions[i] = { x: startX + i * spacing, y: nodeY };
}
// Draw parent arrows (only if parent != self)
for (let i = 0; i < UF_N; i++) {
const p = ufParent[i];
if (p !== i) {
const r = ufFind(i);
const color = rootColorMap[r] || '#2a2a3a';
const from = nodePositions[i];
const to = nodePositions[p];
// Draw curved arrow above nodes
ctx.beginPath();
const cpY = Math.min(from.y, to.y) - 50 - Math.abs(from.x - to.x) * 0.15;
ctx.moveTo(from.x, from.y - nodeRadius);
ctx.quadraticCurveTo((from.x + to.x) / 2, cpY, to.x, to.y - nodeRadius);
ctx.strokeStyle = color + 'aa';
ctx.lineWidth = 1.8;
ctx.setLineDash([4, 3]);
ctx.stroke();
ctx.setLineDash([]);
// Arrowhead
const angle = Math.atan2(to.y - nodeRadius - cpY, to.x - (from.x + to.x) / 2);
ctx.beginPath();
ctx.moveTo(to.x, to.y - nodeRadius);
ctx.lineTo(to.x - 8 * Math.cos(angle - 0.4), to.y - nodeRadius - 8 * Math.sin(angle - 0.4));
ctx.lineTo(to.x - 8 * Math.cos(angle + 0.4), to.y - nodeRadius - 8 * Math.sin(angle + 0.4));
ctx.closePath();
ctx.fillStyle = color + 'aa';
ctx.fill();
}
}
// Draw nodes
for (let i = 0; i < UF_N; i++) {
const { x, y } = nodePositions[i];