Java-Binary Search Tree (BST) アルゴリズムのサンプル コード共有
二分探索ツリーは、リンクリスト挿入の柔軟性と順序付けられた配列検索の効率を組み合わせたアルゴリズムです。以下は、BST のさまざまなメソッドを実装するための純粋なコードです。
二分探索木 (BST) の定義
二分ソート ツリーは、空の木、または次のプロパティを持つ 二分木のいずれかです:
左のサブツリーが空でない場合、次に、左のサブツリー上のすべてのノードの値はそのルートノードの値
- 以下になります。右のサブツリーが空でない場合、すべてのノードの値は
右側のサブツリー 両方の は 以上である ルート ノード の値
- もそれぞれ
バイナリ ソート ツリー
基本的なノード実装 public class BST<K extends Comparable<K>, V> {
private Node root;
private class Node {
private K key;
private V value;
private Node left;
private Node right;
private int N;
public Node(K key, V value, int N) {
this.key = key;
this.value = value;
this.N = N;
}
}
public int size() {
return size(root);
}
private int size(Node x) {
if (x == null)
return 0;
else
return x.N;
}
}
ログイン後にコピー
public class BST<K extends Comparable<K>, V> { private Node root; private class Node { private K key; private V value; private Node left; private Node right; private int N; public Node(K key, V value, int N) { this.key = key; this.value = value; this.N = N; } } public int size() { return size(root); } private int size(Node x) { if (x == null) return 0; else return x.N; } }
ルックアップキー 値の取得 — getpublic V get(K key) {
return get(root, key);
}
private V get(Node root, K key) {
if (root == null)
return null;
int comp = key.compareTo(root.key);
if (comp == 0)
return root.value;
else if (comp < 0)
return get(root.left, key);
else
return get(root.right, key);
}
ログイン後にコピー
public V get(K key) { return get(root, key); } private V get(Node root, K key) { if (root == null) return null; int comp = key.compareTo(root.key); if (comp == 0) return root.value; else if (comp < 0) return get(root.left, key); else return get(root.right, key); }
値の変更/新しい値の挿入 — put public void put(K key, V value) {
root = put(root, key, value);
}
private Node put(Node root, K key, V value) {
if (root == null)
return new Node(key, value, 1);
int comp = key.compareTo(root.key);
if (comp == 0)
root.value = value;
else if (comp < 0)
root.left = put(root.left, key, value);
else
root.right = put(root.right, key, value);
root.N = size(root.left) + size(root.right) + 1;
return root;
}
ログイン後にコピー
public void put(K key, V value) { root = put(root, key, value); } private Node put(Node root, K key, V value) { if (root == null) return new Node(key, value, 1); int comp = key.compareTo(root.key); if (comp == 0) root.value = value; else if (comp < 0) root.left = put(root.left, key, value); else root.right = put(root.right, key, value); root.N = size(root.left) + size(root.right) + 1; return root; }
最大/最小値 — 最小/最大 public K min() {
return min(root).key;
}
private Node min(Node root) {
if (root.left == null)
return root;
return min(root.left);
}
ログイン後にコピー public K max() {
return max(root).key;
}
private Node max(Node root2) {
if (root.right == null)
return root;
return max(root.right);
}
ログイン後にコピー
public K min() { return min(root).key; } private Node min(Node root) { if (root.left == null) return root; return min(root.left); }
public K max() { return max(root).key; } private Node max(Node root2) { if (root.right == null) return root; return max(root.right); }
切り上げ/切り下げ — 床/天井 public K floor(K key) {
Node x = floor(root, key);
if (x == null)
return null;
return x.key;
}
private Node floor(Node root, K key) {
if (root == null)
return null;
int comp = key.compareTo(root.key);
if (comp < 0)
return floor(root.left, key);
else if (comp > 0 && root.right != null
&& key.compareTo(min(root.right).key) >= 0)
return floor(root.right, key);
else
return root;
}
ログイン後にコピー public K ceiling(K key) {
Node x = ceiling(root, key);
if (x == null)
return null;
return x.key;
}
private Node ceiling(Node root, K key) {
if (root == null)
return null;
int comp = key.compareTo(root.key);
if (comp > 0)
return ceiling(root.right, key);
else if (comp < 0 && root.left != null
&& key.compareTo(max(root.left).key) >= 0)
return ceiling(root.left, key);
else
return root;
}
ログイン後にコピー
public K floor(K key) { Node x = floor(root, key); if (x == null) return null; return x.key; } private Node floor(Node root, K key) { if (root == null) return null; int comp = key.compareTo(root.key); if (comp < 0) return floor(root.left, key); else if (comp > 0 && root.right != null && key.compareTo(min(root.right).key) >= 0) return floor(root.right, key); else return root; }
public K ceiling(K key) { Node x = ceiling(root, key); if (x == null) return null; return x.key; } private Node ceiling(Node root, K key) { if (root == null) return null; int comp = key.compareTo(root.key); if (comp > 0) return ceiling(root.right, key); else if (comp < 0 && root.left != null && key.compareTo(max(root.left).key) >= 0) return ceiling(root.left, key); else return root; }
select - select public K select(int k) {
//找出BST中序号为k的键
return select(root, k);
}
private K select(Node root, int k) {
if (root == null)
return null;
int comp = k - size(root.left);
if (comp < 0)
return select(root.left, k);
else if (comp > 0)
return select(root.right, k - (size(root.left) + 1));
else
return root.key;
}
ログイン後にコピー
public K select(int k) { //找出BST中序号为k的键 return select(root, k); } private K select(Node root, int k) { if (root == null) return null; int comp = k - size(root.left); if (comp < 0) return select(root.left, k); else if (comp > 0) return select(root.right, k - (size(root.left) + 1)); else return root.key; }
rank -rank public int rank(K key) {
//找出BST中键为key的序号是多少
return rank(root, key);
}
private int rank(Node root, K key) {
if (root == null)
return 0;
int comp = key.compareTo(root.key);
if (comp == 0)
return size(root.left);
else if (comp < 0)
return rank(root.left, key);
else
return 1 + size(root.left) + rank(root.right, key);
}
ログイン後にコピー
public int rank(K key) { //找出BST中键为key的序号是多少 return rank(root, key); } private int rank(Node root, K key) { if (root == null) return 0; int comp = key.compareTo(root.key); if (comp == 0) return size(root.left); else if (comp < 0) return rank(root.left, key); else return 1 + size(root.left) + rank(root.right, key); }
最小/最大キーを削除 -deleteMin/deleteMax public void deleteMin() {
root = deleteMin(root);
}
private Node deleteMin(Node root) {
if (root.left == null)
return root.right;
root.left = deleteMin(root.left);
root.N = size(root.left) + size(root.right) + 1;
return root;
}
ログイン後にコピー public void deleteMax() {
root = deleteMax(root);
}
private Node deleteMax(Node root) {
if (root.right == null)
return root.left;
root.right = deleteMax(root.right);
root.N = size(root.left) + size(root.right) + 1;
return root;
}
ログイン後にコピー
public void deleteMin() { root = deleteMin(root); } private Node deleteMin(Node root) { if (root.left == null) return root.right; root.left = deleteMin(root.left); root.N = size(root.left) + size(root.right) + 1; return root; }
public void deleteMax() { root = deleteMax(root); } private Node deleteMax(Node root) { if (root.right == null) return root.left; root.right = deleteMax(root.right); root.N = size(root.left) + size(root.right) + 1; return root; }
任意のキーを削除 -deleterrreええ
中順次印刷ツリー—印刷りー
以上がJava-Binary Search Tree (BST) アルゴリズムのサンプル コード共有の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

Video Face Swap
完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

人気の記事

ホットツール

メモ帳++7.3.1
使いやすく無料のコードエディター

SublimeText3 中国語版
中国語版、とても使いやすい

ゼンドスタジオ 13.0.1
強力な PHP 統合開発環境

ドリームウィーバー CS6
ビジュアル Web 開発ツール

SublimeText3 Mac版
神レベルのコード編集ソフト(SublimeText3)

ホットトピック









Java の Weka へのガイド。ここでは、weka java の概要、使い方、プラットフォームの種類、利点について例を交えて説明します。

この記事では、Java Spring の面接で最もよく聞かれる質問とその詳細な回答をまとめました。面接を突破できるように。

Java 8は、Stream APIを導入し、データ収集を処理する強力で表現力のある方法を提供します。ただし、ストリームを使用する際の一般的な質問は次のとおりです。 従来のループにより、早期の中断やリターンが可能になりますが、StreamのForeachメソッドはこの方法を直接サポートしていません。この記事では、理由を説明し、ストリーム処理システムに早期終了を実装するための代替方法を調査します。 さらに読み取り:JavaストリームAPIの改善 ストリームを理解してください Foreachメソッドは、ストリーム内の各要素で1つの操作を実行する端末操作です。その設計意図はです

Java での日付までのタイムスタンプに関するガイド。ここでは、Java でタイムスタンプを日付に変換する方法とその概要について、例とともに説明します。

カプセルは3次元の幾何学的図形で、両端にシリンダーと半球で構成されています。カプセルの体積は、シリンダーの体積と両端に半球の体積を追加することで計算できます。このチュートリアルでは、さまざまな方法を使用して、Javaの特定のカプセルの体積を計算する方法について説明します。 カプセルボリュームフォーミュラ カプセルボリュームの式は次のとおりです。 カプセル体積=円筒形の体積2つの半球体積 で、 R:半球の半径。 H:シリンダーの高さ(半球を除く)。 例1 入力 RADIUS = 5ユニット 高さ= 10単位 出力 ボリューム= 1570.8立方ユニット 説明する 式を使用してボリュームを計算します。 ボリューム=π×R2×H(4

Java は、初心者と経験豊富な開発者の両方が学習できる人気のあるプログラミング言語です。このチュートリアルは基本的な概念から始まり、高度なトピックに進みます。 Java Development Kit をインストールしたら、簡単な「Hello, World!」プログラムを作成してプログラミングを練習できます。コードを理解したら、コマンド プロンプトを使用してプログラムをコンパイルして実行すると、コンソールに「Hello, World!」と出力されます。 Java の学習はプログラミングの旅の始まりであり、習熟が深まるにつれて、より複雑なアプリケーションを作成できるようになります。
