【数据结构】——排序算法——1.3、二叉树排序

前端之家收集整理的这篇文章主要介绍了【数据结构】——排序算法——1.3、二叉树排序前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
【数据结构】——排序算法——1.2、二叉树排序

一、先上维基的图:二叉树排序wiki
图一、二叉树排序
二、描述

二叉查找树英语Binary Search Tree),也称二叉搜索树、有序二叉树(英语ordered binary tree),排序二叉树(英语sorted binary tree),是指一棵空树或者具有下列性质的二叉树

  1. 若任意节点的左子树不空,则左子树上所有结点的值均小于或等于它的根结点的值;
  2. 若任意节点的右子树不空,则右子树上所有结点的值均大于它的根结点的值;
  3. 任意节点的左、右子树也分别为二叉查找树。
  4. 没有键值相等的节点(英语no duplicate nodes)。二叉查找树相比于其他数据结构的优势在于查找、插入的时间复杂度较低。为O(log n)。二叉查找树是基础性数据结构,用于构建更为抽象的数据结构,如集合multiset关联数组等。

在二叉查找树b中查找x的过程为:

  1. 若b是空树,则搜索失败,否则:
  2. 若x等于b的根节点的数据域之值,则查找成功;否则:
  3. 若x小于b的根节点的数据域之值,则搜索左子树;否则:
  4. 查找右子树。

三、Java程序

  1. public class OrderTree {
  2. public static void main(String[] args) {
  3. /**
  4. * ***4***
  5. * *2***6*
  6. * 1*3*5*7
  7. *
  8. */
  9. int[] a = {4,2,1,3,6,5,7,8,9};
  10. BTree root = new BTree();
  11. createOrder(a,root);
  12. System.out.println("\npre order:");
  13. preOrder(root);
  14. System.out.println("\nmid order:");
  15. midOrder(root);
  16. System.out.println("\nlast order:");
  17. lastOrder(root);
  18. List<Point> points = new ArrayList<Point>();
  19. // 将tree的节点转化为坐标
  20. int floors = totalfloor(root);
  21. System.out.println("\nfloors:"+floors);
  22. printTree(root,points,-1,floors);
  23. // 按照 row,col 对 坐标进行排序方便打印
  24. Collections.sort(points);
  25. // 按每点 5个字符宽进行打印(需要进行坐标系转换), 当data==-1时 显示 *
  26. int row = 0;
  27. StringBuilder sb = new StringBuilder();
  28. int totalLength = 1*(squat(2,floors)-1);
  29. for(Point p : points){
  30. if(row == p.row){
  31. sb.append(printTimes("*",1*p.col-sb.length()));
  32. sb.append(printWidth(""+p.data,1));
  33. }else{
  34. if(sb.length()< totalLength){
  35. sb.append(printTimes("*",totalLength-sb.length()));
  36. }
  37. System.out.println(sb.toString());
  38. sb = new StringBuilder();
  39. row++;
  40. sb.append(printTimes("*",1));
  41. }
  42. }
  43. if(sb.length()< totalLength){
  44. sb.append(printTimes("*",totalLength-sb.length()));
  45. }
  46. System.out.println(sb.toString());
  47. }
  48. static int totalfloor(BTree root){
  49. if(root == null)return 0;
  50. return internalLoop(root,0);
  51. }
  52. static int internalLoop(BTree root,int floor){
  53. if(root != null){
  54. floor++;
  55. int left = internalLoop(root.left,floor);
  56. int right = internalLoop(root.right,floor);
  57. return Math.max(left,right);
  58. }
  59. else return floor;
  60. }
  61. static String printTimes(String s,int time){
  62. StringBuilder sb = new StringBuilder();
  63. for(int i=0;i<time;i++){
  64. sb.append(s);
  65. }
  66. return sb.toString();
  67. }
  68. static String printWidth(String s,int width){
  69. if(s == null ){
  70. return printTimes(" ",width);
  71. }else{
  72. if(s.length() < width){
  73. return new StringBuilder(s).append(printTimes("*",width-s.length())).toString();
  74. }else{
  75. return s.substring(0,width);
  76. }
  77. }
  78. }
  79. static class Point implements Comparable<Point>{
  80. public Point(int row,int col,int data){
  81. this.row = row;
  82. this.col = col;
  83. this.data = data;
  84. }
  85. int row = 0;
  86. int col = 0;
  87. int data = -1;
  88. @Override
  89. public int compareTo(Point o) {
  90. if(this.row == o.row){
  91. if(col == o.col)
  92. return 0;
  93. else if(col < o.col){
  94. return -1;
  95. }else{
  96. return 1;
  97. }
  98. }else if(row < o.row){
  99. return -1;
  100. }else{
  101. return 1;
  102. }
  103. }
  104. }
  105. static void printTree(BTree root,List<Point> points,int row,int floors){
  106. if(root != null){
  107. int inter = squat(2,floors-1)-1;
  108. if(col == -1){
  109. col = inter;
  110. }
  111. /**
  112. * ***0***
  113. * *0***0*
  114. * 0*0*0*0
  115. *
  116. */
  117. points.add(new Point(row,col,root.data));
  118. printTree(root.left,row+1,col - ((inter-1)/2)-1,floors -1);
  119. printTree(root.right,col + (inter-1)/2+1,floors -1);
  120. }
  121. }
  122. static int squat(int s,int b){
  123. if(b == 0) return 1;
  124. int result = 1;
  125. for(int i=0;i<b ;i++){
  126. result *=s;
  127. }
  128. return result;
  129. }
  130. static void preOrder(BTree root){
  131. if(root == null) {
  132. return ;
  133. }
  134. System.out.print("-"+root.data);
  135. preOrder(root.left);
  136. preOrder(root.right);
  137. }
  138. static void midOrder(BTree root){
  139. if(root == null) return ;
  140. midOrder(root.left);
  141. System.out.print("-"+root.data);
  142. midOrder(root.right);
  143. }
  144. static void lastOrder(BTree root){
  145. if(root == null) return ;
  146. lastOrder(root.left);
  147. lastOrder(root.right);
  148. System.out.print("-"+root.data);
  149. }
  150. // 左边小,右边大,相等时放左边
  151. static void createOrder(int[] a,BTree root){
  152. if(a == null || a.length == 0) return ;
  153. for(int i=0;i<a.length;i++){
  154. if(i==0){
  155. root.data = a[i];
  156. }else{
  157. BTree leaf = new BTree();
  158. leaf.data = a[i];
  159. insert(root,leaf);
  160. }
  161. }
  162. }
  163. static void insert(BTree root,BTree leaf){
  164. if(root.data == leaf.data){ // == 放左边
  165. if(root.left == null){
  166. root.left = leaf;
  167. }else{
  168. insert(root.left,leaf);
  169. }
  170. }else if(root.data > leaf.data){ // > 放左边
  171. if(root.left == null){
  172. root.left = leaf;
  173. }else{
  174. insert(root.left,leaf);
  175. }
  176. }else{ // < 放右边
  177. if(root.right == null){
  178. root.right = leaf;
  179. }else{
  180. insert(root.right,leaf);
  181. }
  182. }
  183. }
  184. static class BTree{
  185. BTree left;
  186. BTree right;
  187. int data;
  188. }
  189. }


收藏:java 二叉树的实现(这个简单点比较容易看懂);
Java基础复习笔记10数据结构-排序二叉树

猜你在找的数据结构相关文章