Java sorting algorithm code-binary tree sorting
- /**
- * The Java sorting algorithm implements code-binary tree sorting.
- *
- * @ Author old zizhu Java century network (java2000.net)
- *
- */
- Public class test {
- Public static int [] A = {0, 10, 32, 1, 9, 5, 7, 12, 2, 4, 3}; // default data array
- Public static int [] DATA = new int [20]; // default data array
- Public static void main (string ARGs []) {
- Int I; // The variable of the cyclic count.
- Int Index = 1; // data index variable
- Bntreearray bntree = new bntreearray (); // declare a binary tree Array
- 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]); // create a binary search tree
- Index ++;
- }
- // Pre-sorting data content
- System. Out. Print ("Before sorting :");
- For (I = 1; I <index; I ++)
- System. Out. Print ("" + data [I] + "");
- System. Out. println ("");
- // Sorted result
- System. Out. Print ("sorted :");
- Bntreearray. Inorder (0); // center Traversal
- }
- }
- 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; // The variable of the cyclic count.
- For (I = 0; I <maxsize; I ++ ){
- Treedata [I] = 0;
- Rightnode [I] =-1;
- Leftnode [I] =-1;
- }
- }
- //----------------------------------------------------
- // Create a binary tree
- //----------------------------------------------------
- Public void create (INT data ){
- Int I; // The variable of the cyclic count.
- Int level = 0; // Number of classes in the tree
- Int position = 0;
- For (I = 0; treedata [I]! = 0; I ++)
- ;
- Treedata [I] = data;
- While (true) // find the node location
- {
- // Determine the left or right subtree
- If (data> treedata [level]) {
- // Whether the right tree has the next class
- If (rightnode [level]! =-1)
- Level = rightnode [level];
- Else {
- Position =-1; // set it to the right tree
- Break;
- }
- } Else {
- // Whether the left tree has the next class
- If (leftnode [level]! =-1)
- Level = leftnode [level];
- Else {
- Position = 1; // set it to the left Tree
- Break;
- }
- }
- }
- If (position = 1) // create the left and right links of the node
- Leftnode [level] = I; // link the left subtree
- Else
- Rightnode [level] = I; // link to the right subtree
- }
- //---------------------------------------------------------
- // Sequential traversal of Binary Trees
- //---------------------------------------------------------
- Public static void inorder (INT pointer ){
- If (pointer! =-1) // termination condition of Traversal
- {
- Inorder (leftnode [pointer]); // process the left subtree
- // Process the printed node content
- System. Out. Print ("" + treedata [pointer] + "");
- Inorder (rightnode [pointer]); // process the right subtree
- }
- }
- }
/*** Java sorting algorithm implements code-binary tree sorting. ** @ Author old zizhu Java century network (java2000.net) **/public class test {public static int [] A = {0, 10, 32, 1, 9, 5, 7, 12, 2, 4, 3}; // default data array public static int [] DATA = new int [20]; // default data array public static void main (string ARGs []) {int I; // The cyclic count variable int Index = 1; // data index variable bntreearray bntree = new bntreearray (); // declare the binary tree array data [Index] = A [Index]; bntreearray. treedata [0] = data [Index]; index ++; for (I = 2; I <. length; I ++) {data [Index] = A [Index]; bntree. create (data [Index]); // create a binary search tree index ++;} // pre-sorting data content system. out. print ("Before sorting:"); for (I = 1; I <index; I ++) system. out. print ("" + data [I] + ""); system. out. println (""); // sorted result system. out. print ("sorted:"); bntreearray. inorder (0); // ordinal traversal} 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; // The cyclic count variable for (I = 0; I <maxsize; I ++) {treedata [I] = 0; rightnode [I] =-1; leftnode [I] =-1 ;}// ------------------------------------------------------ // create a binary tree // define public void create (INT data) {int I; // The cyclic count variable I NT level = 0; // The number of classes in the tree int position = 0; for (I = 0; treedata [I]! = 0; I ++); treedata [I] = data; while (true) // find the node location {// determine whether the left subtree or right subtree if (data> treedata [level]) {// whether the right tree has the next level if (rightnode [level]! =-1) level = rightnode [level]; else {position =-1; // set it to right tree break ;}} else {// whether the left tree has the next level if (leftnode [level]! =-1) level = leftnode [level]; else {position = 1; // set to left Tree break; }}} if (position = 1) // create the left and right links of the node leftnode [level] = I; // link the left subtree else rightnode [level] = I; // link to the right subtree} // Binary Tree in-order traversal // ------------------------------------------------------------- public static void inorder (INT pointer) {If (pointer! =-1) // terminate condition for traversal {inorder (leftnode [pointer]); // process the left subtree // process the printed node content system. out. print ("" + treedata [pointer] + ""); inorder (rightnode [pointer]); // process the right subtree }}}
Running result
Before sorting: 10 32 1 9 5 7 12 2 4 3
After sorting: 1 2 3 4 5 7 9 10 12 32