docs: add Japanese translate documents (#1812)

* docs: add Japanese documents (`ja/docs`)

* docs: add Japanese documents (`ja/codes`)

* docs: add Japanese documents

* Remove pythontutor blocks in ja/

* Add an empty at the end of each markdown file.

* Add the missing figures (use the English version temporarily).

* Add index.md for Japanese version.

* Add index.html for Japanese version.

* Add missing index.assets

* Fix backtracking_algorithm.md for Japanese version.

* Add avatar_eltociear.jpg. Fix image links on the Japanese landing page.

* Add the Japanese banner.

---------

Co-authored-by: krahets <krahets@163.com>
This commit is contained in:
Ikko Eltociear Ashimine
2025-10-17 06:04:43 +09:00
committed by GitHub
parent 2487a27036
commit 954c45864b
886 changed files with 33569 additions and 0 deletions
+66
View File
@@ -0,0 +1,66 @@
/**
* File: heap.cpp
* Created Time: 2023-01-19
* Author: LoneRanger(836253168@qq.com)
*/
#include "../utils/common.hpp"
void testPush(priority_queue<int> &heap, int val) {
heap.push(val); // 要素をヒープにプッシュ
cout << "\n要素 " << val << " をヒープに追加後" << endl;
printHeap(heap);
}
void testPop(priority_queue<int> &heap) {
int val = heap.top();
heap.pop();
cout << "\nヒープから先頭要素 " << val << " を削除後" << endl;
printHeap(heap);
}
/* ドライバーコード */
int main() {
/* ヒープを初期化 */
// 最小ヒープを初期化
// priority_queue<int, vector<int>, greater<int>> minHeap;
// 最大ヒープを初期化
priority_queue<int, vector<int>, less<int>> maxHeap;
cout << "\n以下のテストケースは最大ヒープ用です" << endl;
/* ヒープに要素をプッシュ */
testPush(maxHeap, 1);
testPush(maxHeap, 3);
testPush(maxHeap, 2);
testPush(maxHeap, 5);
testPush(maxHeap, 4);
/* ヒープの先頭要素にアクセス */
int peek = maxHeap.top();
cout << "\nヒープの先頭要素は " << peek << endl;
/* ヒープ先頭の要素をポップ */
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
testPop(maxHeap);
/* ヒープのサイズを取得 */
int size = maxHeap.size();
cout << "\nヒープ内の要素数は " << size << endl;
/* ヒープが空かどうか判定 */
bool isEmpty = maxHeap.empty();
cout << "\nヒープが空かどうか " << isEmpty << endl;
/* リストを入力してヒープを構築 */
// 時間計算量はO(n)、O(nlogn)ではない
vector<int> input{1, 3, 2, 5, 4};
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
cout << "リストを入力して最小ヒープを構築後" << endl;
printHeap(minHeap);
return 0;
}
+155
View File
@@ -0,0 +1,155 @@
/**
* File: my_heap.cpp
* Created Time: 2023-02-04
* Author: LoneRanger (836253168@qq.com), what-is-me (whatisme@outlook.jp)
*/
#include "../utils/common.hpp"
/* 最大ヒープ */
class MaxHeap {
private:
// 動的配列を使用してサイズ変更の必要性を回避
vector<int> maxHeap;
/* 左の子ノードのインデックスを取得 */
int left(int i) {
return 2 * i + 1;
}
/* 右の子ノードのインデックスを取得 */
int right(int i) {
return 2 * i + 2;
}
/* 親ノードのインデックスを取得 */
int parent(int i) {
return (i - 1) / 2; // 整数除算で切り下げ
}
/* ノードiから上向きにヒープ化を開始 */
void siftUp(int i) {
while (true) {
// ノードiの親ノードを取得
int p = parent(i);
// 「ルートノードを超える」または「ノードが修復不要」の場合、ヒープ化を終了
if (p < 0 || maxHeap[i] <= maxHeap[p])
break;
// 2つのノードを交換
swap(maxHeap[i], maxHeap[p]);
// 上向きにループしてヒープ化
i = p;
}
}
/* ノードiから下向きにヒープ化を開始 */
void siftDown(int i) {
while (true) {
// i、l、rの中で最大のノードを決定し、maとして記録
int l = left(i), r = right(i), ma = i;
if (l < size() && maxHeap[l] > maxHeap[ma])
ma = l;
if (r < size() && maxHeap[r] > maxHeap[ma])
ma = r;
// ノードiが最大、またはインデックスl、rが範囲外の場合、これ以上のヒープ化は不要、ブレーク
if (ma == i)
break;
swap(maxHeap[i], maxHeap[ma]);
// 下向きにループしてヒープ化
i = ma;
}
}
public:
/* コンストラクタ、入力リストに基づいてヒープを構築 */
MaxHeap(vector<int> nums) {
// すべてのリスト要素をヒープに追加
maxHeap = nums;
// 葉以外のすべてのノードをヒープ化
for (int i = parent(size() - 1); i >= 0; i--) {
siftDown(i);
}
}
/* ヒープのサイズを取得 */
int size() {
return maxHeap.size();
}
/* ヒープが空かどうか判定 */
bool isEmpty() {
return size() == 0;
}
/* ヒープの先頭要素にアクセス */
int peek() {
return maxHeap[0];
}
/* ヒープに要素をプッシュ */
void push(int val) {
// ノードを追加
maxHeap.push_back(val);
// 下から上へヒープ化
siftUp(size() - 1);
}
/* 要素がヒープから退出 */
void pop() {
// 空の処理
if (isEmpty()) {
throw out_of_range("Heap is empty");
}
// ルートノードを最も右の葉ノードと交換(最初の要素と最後の要素を交換)
swap(maxHeap[0], maxHeap[size() - 1]);
// ノードを削除
maxHeap.pop_back();
// 上から下へヒープ化
siftDown(0);
}
/* ヒープを印刷(二分木)*/
void print() {
cout << "ヒープの配列表現:";
printVector(maxHeap);
cout << "ヒープの木表現:" << endl;
TreeNode *root = vectorToTree(maxHeap);
printTree(root);
freeMemoryTree(root);
}
};
/* ドライバーコード */
int main() {
/* 最大ヒープを初期化 */
vector<int> vec{9, 8, 6, 6, 7, 5, 2, 1, 4, 3, 6, 2};
MaxHeap maxHeap(vec);
cout << "\nリストを入力してヒープを構築" << endl;
maxHeap.print();
/* ヒープの先頭要素にアクセス */
int peek = maxHeap.peek();
cout << "\nヒープの先頭要素は " << peek << endl;
/* ヒープに要素をプッシュ */
int val = 7;
maxHeap.push(val);
cout << "\n要素 " << val << " をヒープに追加後" << endl;
maxHeap.print();
/* ヒープ先頭の要素をポップ */
peek = maxHeap.peek();
maxHeap.pop();
cout << "\nヒープから先頭要素 " << peek << " を削除後" << endl;
maxHeap.print();
/* ヒープのサイズを取得 */
int size = maxHeap.size();
cout << "\nヒープ内の要素数は " << size << endl;
/* ヒープが空かどうか判定 */
bool isEmpty = maxHeap.isEmpty();
cout << "\nヒープが空かどうか " << isEmpty << endl;
return 0;
}
+38
View File
@@ -0,0 +1,38 @@
/**
* File: top_k.cpp
* Created Time: 2023-06-12
* Author: krahets (krahets@163.com)
*/
#include "../utils/common.hpp"
/* ヒープを使用して配列内の最大k個の要素を見つける */
priority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {
// 最小ヒープを初期化
priority_queue<int, vector<int>, greater<int>> heap;
// 配列の最初のk個の要素をヒープに入力
for (int i = 0; i < k; i++) {
heap.push(nums[i]);
}
// k+1番目の要素から、ヒープの長さをkに保つ
for (int i = k; i < nums.size(); i++) {
// 現在の要素がヒープの先頭要素より大きい場合、ヒープの先頭要素を削除し、現在の要素をヒープに入力
if (nums[i] > heap.top()) {
heap.pop();
heap.push(nums[i]);
}
}
return heap;
}
// ドライバーコード
int main() {
vector<int> nums = {1, 7, 6, 3, 2};
int k = 3;
priority_queue<int, vector<int>, greater<int>> res = topKHeap(nums, k);
cout << "最大 " << k << " 個の要素は:";
printHeap(res);
return 0;
}