Java sorting algorithm code-binary tree sorting

Source: Internet
Author: User

Java sorting algorithm code-binary tree sorting
  1. /**
  2. * The Java sorting algorithm implements code-binary tree sorting.
  3. *
  4. * @ Author old zizhu Java century network (java2000.net)
  5. *
  6. */
  7. Public class test {
  8. Public static int [] A = {0, 10, 32, 1, 9, 5, 7, 12, 2, 4, 3}; // default data array
  9. Public static int [] DATA = new int [20]; // default data array
  10. Public static void main (string ARGs []) {
  11. Int I; // The variable of the cyclic count.
  12. Int Index = 1; // data index variable
  13. Bntreearray bntree = new bntreearray (); // declare a binary tree Array
  14. Data [Index] = A [Index];
  15. Bntreearray. treedata [0] = data [Index];
  16. Index ++;
  17. For (I = 2; I <A. length; I ++ ){
  18. Data [Index] = A [Index];
  19. Bntree. Create (data [Index]); // create a binary search tree
  20. Index ++;
  21. }
  22. // Pre-sorting data content
  23. System. Out. Print ("Before sorting :");
  24. For (I = 1; I <index; I ++)
  25. System. Out. Print ("" + data [I] + "");
  26. System. Out. println ("");
  27. // Sorted result
  28. System. Out. Print ("sorted :");
  29. Bntreearray. Inorder (0); // center Traversal
  30. }
  31. }
  32. Class bntreearray {
  33. Public static int maxsize = 20;
  34. Public static int [] treedata = new int [maxsize];
  35. Public static int [] rightnode = new int [maxsize];
  36. Public static int [] leftnode = new int [maxsize];
  37. Public bntreearray (){
  38. Int I; // The variable of the cyclic count.
  39. For (I = 0; I <maxsize; I ++ ){
  40. Treedata [I] = 0;
  41. Rightnode [I] =-1;
  42. Leftnode [I] =-1;
  43. }
  44. }
  45. //----------------------------------------------------
  46. // Create a binary tree
  47. //----------------------------------------------------
  48. Public void create (INT data ){
  49. Int I; // The variable of the cyclic count.
  50. Int level = 0; // Number of classes in the tree
  51. Int position = 0;
  52. For (I = 0; treedata [I]! = 0; I ++)
  53. ;
  54. Treedata [I] = data;
  55. While (true) // find the node location
  56. {
  57. // Determine the left or right subtree
  58. If (data> treedata [level]) {
  59. // Whether the right tree has the next class
  60. If (rightnode [level]! =-1)
  61. Level = rightnode [level];
  62. Else {
  63. Position =-1; // set it to the right tree
  64. Break;
  65. }
  66. } Else {
  67. // Whether the left tree has the next class
  68. If (leftnode [level]! =-1)
  69. Level = leftnode [level];
  70. Else {
  71. Position = 1; // set it to the left Tree
  72. Break;
  73. }
  74. }
  75. }
  76. If (position = 1) // create the left and right links of the node
  77. Leftnode [level] = I; // link the left subtree
  78. Else
  79. Rightnode [level] = I; // link to the right subtree
  80. }
  81. //---------------------------------------------------------
  82. // Sequential traversal of Binary Trees
  83. //---------------------------------------------------------
  84. Public static void inorder (INT pointer ){
  85. If (pointer! =-1) // termination condition of Traversal
  86. {
  87. Inorder (leftnode [pointer]); // process the left subtree
  88. // Process the printed node content
  89. System. Out. Print ("" + treedata [pointer] + "");
  90. Inorder (rightnode [pointer]); // process the right subtree
  91. }
  92. }
  93. }
/*** 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.