JAVAソートアルゴリズム実装コード-ツリーソート
JAVAソートアルゴリズム実装コード-ツリーソート
/**
* JAVAソートアルゴリズムはコード-ツリーソートを実現する.
*
* @author 老紫竹 JAVA世紀ネット(java 2000.net)
*
*/
public class Test {
public static int[] a = { 0, 10, 32, 1, 9, 5, 7, 12, 2, 4, 3 }; // プリセットデータ配列
public static int[] Data = new int[20]; // プリセットデータ配列
public static void main(String args[]) {
int i; // ループカウント変数
int Index = 1; // データ索引変数
BNTreeArray BNTree = new BNTreeArray(); // ツリー配列の宣言
Data[Index] = a[Index];
BNTreeArray.TreeData[0] = Data[Index];
Index++;
for (i = 2; i < a.length; i++) {
Data[Index] = a[Index];
BNTree.Create(Data[Index]); // ツリーの作成
Index++;
}
// ソート前のデータの内容
System.out.print(「ソート前」 : ");
for (i = 1; i < Index; i++)
System.out.print(" " + Data[i] + " ");
System.out.println("");
// ソート後の結果
System.out.print(「ソート後」 : ");
BNTreeArray.InOrder(0); // ちゅうかんじゅんかん遍歴
}
}
class BNTreeArray {
public static int MaxSize = 20;
public static int[] TreeData = new int[MaxSize];
public static int[] RightNode = new int[MaxSize];
public static int[] LeftNode = new int[MaxSize];
public BNTreeArray() {
int i; // ループカウント変数
for (i = 0; i < MaxSize; i++) {
TreeData[i] = 0;
RightNode[i] = -1;
LeftNode[i] = -1;
}
}
// ----------------------------------------------------
// ツリーの作成
// ----------------------------------------------------
public void Create(int Data) {
int i; // ループカウント変数
int Level = 0; // 木の階層数
int Position = 0;
for (i = 0; TreeData[i] != 0; i++)
;
TreeData[i] = Data;
while (true) // ノードの場所を探す
{
// 左サブツリーか右サブツリーかを判断
if (Data > TreeData[Level]) {
// 右の木に次の階層があるかどうか
if (RightNode[Level] != -1)
Level = RightNode[Level];
else {
Position = -1; // 右ツリーに設定
break;
}
} else {
// 左の木に次の階層があるかどうか
if (LeftNode[Level] != -1)
Level = LeftNode[Level];
else {
Position = 1; // 左ツリーに設定
break;
}
}
}
if (Position == 1) // ノードの左右連結を確立する
LeftNode[Level] = i; // 左サブツリーを連結
else
RightNode[Level] = i; // 右サブツリーを連結
}
// ---------------------------------------------------------
// ツリーの順序
// ---------------------------------------------------------
public static void InOrder(int Pointer) {
if (Pointer != -1) // 遍歴の終了条件
{
InOrder(LeftNode[Pointer]); // 左サブツリーの操作
// 印刷ノードの内容の処理
System.out.print(" " + TreeData[Pointer] + " ");
InOrder(RightNode[Pointer]); // 右サブツリーの操作
}
}
}
/**
* JAVA - 。
*
* @author JAVA (java2000.net)
*
*/
public class Test {
public static int[] a = { 0, 10, 32, 1, 9, 5, 7, 12, 2, 4, 3 }; //
public static int[] Data = new int[20]; //
public static void main(String args[]) {
int i; //
int Index = 1; //
BNTreeArray BNTree = new BNTreeArray(); //
Data[Index] = a[Index];
BNTreeArray.TreeData[0] = Data[Index];
Index++;
for (i = 2; i < a.length; i++) {
Data[Index] = a[Index];
BNTree.Create(Data[Index]); //
Index++;
}
//
System.out.print(" : ");
for (i = 1; i < Index; i++)
System.out.print(" " + Data[i] + " ");
System.out.println("");
//
System.out.print(" : ");
BNTreeArray.InOrder(0); //
}
}
class BNTreeArray {
public static int MaxSize = 20;
public static int[] TreeData = new int[MaxSize];
public static int[] RightNode = new int[MaxSize];
public static int[] LeftNode = new int[MaxSize];
public BNTreeArray() {
int i; //
for (i = 0; i < MaxSize; i++) {
TreeData[i] = 0;
RightNode[i] = -1;
LeftNode[i] = -1;
}
}
// ----------------------------------------------------
//
// ----------------------------------------------------
public void Create(int Data) {
int i; //
int Level = 0; //
int Position = 0;
for (i = 0; TreeData[i] != 0; i++)
;
TreeData[i] = Data;
while (true) //
{
//
if (Data > TreeData[Level]) {
//
if (RightNode[Level] != -1)
Level = RightNode[Level];
else {
Position = -1; //
break;
}
} else {
//
if (LeftNode[Level] != -1)
Level = LeftNode[Level];
else {
Position = 1; //
break;
}
}
}
if (Position == 1) //
LeftNode[Level] = i; //
else
RightNode[Level] = i; //
}
// ---------------------------------------------------------
//
// ---------------------------------------------------------
public static void InOrder(int Pointer) {
if (Pointer != -1) //
{
InOrder(LeftNode[Pointer]); //
//
System.out.print(" " + TreeData[Pointer] + " ");
InOrder(RightNode[Pointer]); //
}
}
}
実行結果
ソート前:10 32 1 9 5 7 12 4 3
ソート後:1 2 3 4 5 7 9 10 12 32