国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - java二叉查找樹的實(shí)現(xiàn)代碼

java二叉查找樹的實(shí)現(xiàn)代碼

2020-12-14 13:01evasean Java教程

這篇文章主要為大家詳細(xì)介紹了java二叉查找樹的實(shí)現(xiàn)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了java二叉查找樹的具體代碼,供大家參考,具體內(nèi)容如下

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package 查找;
 
import edu.princeton.cs.algs4.Queue;
import edu.princeton.cs.algs4.StdOut;
 
public class BST<Key extends Comparable<Key>, Value> {
  private class Node {
    private Key key; // 鍵
    private Value value;// 值
    private Node left, right; // 指向子樹的鏈接
    private int n; // 以該節(jié)點(diǎn)為根的子樹中的節(jié)點(diǎn)總數(shù)
 
    public Node(Key key, Value val, int n) {
      this.key = key;
      this.value = val;
      this.n = n;
    }
  }
 
  private Node root;
 
  public int size() {
    return size(root);
  }
 
  private int size(Node x) {
    if (x == null)
      return 0;
    else
      return x.n;
  }
 
  /**
   * 如果樹是空的,則查找未命中 如果被查找的鍵小于根節(jié)點(diǎn),則在左子樹中繼續(xù)查找 如果被查找的鍵大于根節(jié)點(diǎn),則在右子樹中繼續(xù)查找
   * 如果被查找的鍵和根節(jié)點(diǎn)的鍵相等,查找命中
   *
   * @param key
   * @return
   */
  public Value get(Key key) {
    return get(root, key);
  }
 
  private Value get(Node x, Key key) {
    if (x == null)
      return null;
    int cmp = key.compareTo(x.key);
    if (cmp < 0)
      return get(x.left, key);
    else if (cmp > 0)
      return get(x.right, key);
    else
      return x.value;
  }
 
  /**
   * 二叉查找樹的一個(gè)很重要的特性就是插入的實(shí)現(xiàn)難度和查找差不多。 當(dāng)查找到一個(gè)不存在與樹中的節(jié)點(diǎn)(null)時(shí),new 新節(jié)點(diǎn),并將上一路徑指向該節(jié)點(diǎn)
   *
   * @param key
   * @param val
   */
  public void put(Key key, Value val) {
    root = put(root, key, val);
  }
 
  private Node put(Node x, Key key, Value val) {
    if (x == null)
      return new Node(key, val, 1);
    int cmp = key.compareTo(x.key);
    if (cmp < 0)
      x.left = put(x.left, key, val);
    else if (cmp > 0)
      x.right = put(x.right, key, val);
    else
      x.value = val;
    x.n = size(x.left) + size(x.right); // 要及時(shí)更新節(jié)點(diǎn)的子樹數(shù)量
    return x;
  }
 
  public Key min() {
    return min(root).key;
  }
 
  private Node min(Node x) {
    if (x.left == null)
      return x;
    return min(x.left);
  }
 
  public Key max() {
    return max(root).key;
  }
 
  private Node max(Node x) {
    if (x.right == null)
      return x;
    return min(x.right);
  }
 
  /**
   * 向下取整:找出小于等于該鍵的最大鍵
   *
   * @param key
   * @return
   */
  public Key floor(Key key) {
    Node x = floor(root, key);
    if (x == null)
      return null;
    else
      return x.key;
  }
 
  /**
   * 如果給定的鍵key小于二叉查找樹的根節(jié)點(diǎn)的鍵,那么小于等于key的最大鍵一定出現(xiàn)在根節(jié)點(diǎn)的左子樹中
   * 如果給定的鍵key大于二叉查找樹的根節(jié)點(diǎn),那么只有當(dāng)根節(jié)點(diǎn)右子樹中存在大于等于key的節(jié)點(diǎn)時(shí),
   * 小于等于key的最大鍵才會(huì)出現(xiàn)在右子樹中,否則根節(jié)點(diǎn)就是小于等于key的最大鍵
   *
   * @param x
   * @param key
   * @return
   */
  private Node floor(Node x, Key key) {
    if (x == null)
      return null;
    int cmp = key.compareTo(x.key);
    if (cmp == 0)
      return x;
    else if (cmp < 0)
      return floor(x.left, key);
    else {
      Node t = floor(x.right, key);
      if (t == null)
        return x;
      else
        return t;
    }
  }
 
  /**
   * 向上取整:找出大于等于該鍵的最小鍵
   *
   * @param key
   * @return
   */
  public Key ceiling(Key key) {
    Node x = ceiling(root, key);
    if (x == null)
      return null;
    else
      return x.key;
  }
 
  /**
   * 如果給定的鍵key大于二叉查找樹的根節(jié)點(diǎn)的鍵,那么大于等于key的最小鍵一定出現(xiàn)在根節(jié)點(diǎn)的右子樹中
   * 如果給定的鍵key小于二叉查找樹的根節(jié)點(diǎn),那么只有當(dāng)根節(jié)點(diǎn)左子樹中存在大于等于key的節(jié)點(diǎn)時(shí),
   * 大于等于key的最小鍵才會(huì)出現(xiàn)在左子樹中,否則根節(jié)點(diǎn)就是大于等于key的最小鍵
   *
   * @param x
   * @param key
   * @return
   */
  private Node ceiling(Node x, Key key) {
    if (x == null)
      return null;
    int cmp = key.compareTo(x.key);
    if (cmp == 0)
      return x;
    else if (cmp > 0) {
      return ceiling(x.right, key);
    } else {
      Node t = floor(x.left, key);
      if (t == null)
        return x;
      else
        return t;
    }
  }
 
  /**
   * 選擇排名為k的節(jié)點(diǎn)
   *
   * @param k
   * @return
   */
  public Key select(int k) {
    return select(root, k).key;
  }
 
  private Node select(Node x, int k) {
    if (x == null)
      return null;
    int t = size(x.left);
    if (t > k)
      return select(x.left, k);
    else if (t < k)
      return select(x.right, k - t - 1);// 根節(jié)點(diǎn)也要排除掉
    else
      return x;
  }
 
  /**
   * 查找給定鍵值的排名
   *
   * @param key
   * @return
   */
  public int rank(Key key) {
    return rank(key, root);
  }
 
  private int rank(Key key, Node x) {
    if (x == null)
      return 0;
    int cmp = key.compareTo(x.key);
    if (cmp < 0)
      return rank(key, x.left);
    else if (cmp > 0)
      return 1 + size(x.left) + rank(key, x.right);
    else
      return size(x.left);
  }
  /**
   * 刪除最小鍵值對(duì)
   */
  public void deleteMin(){
    root = deleteMin(root);
  }
  /**
   * 不斷深入根節(jié)點(diǎn)的左子樹直到遇見一個(gè)空鏈接,然后將指向該節(jié)點(diǎn)的鏈接指向該結(jié)點(diǎn)的右子樹
   * 此時(shí)已經(jīng)沒有任何鏈接指向要被刪除的結(jié)點(diǎn),因此它會(huì)被垃圾收集器清理掉
   * @param x
   * @return
   */
  private Node deleteMin(Node x){
    if(x.left == null) return x.right;
    x.left = deleteMin(x.left);
    x.n = size(x.left)+size(x.right) + 1;
    return x;
  }
  
  public void deleteMax(){
    root = deleteMax(root);
  }
  private Node deleteMax(Node x){
    if(x.right == null ) return x.left;
    x.right = deleteMax(x.right);
    x.n = size(x.left)+size(x.right) + 1;
    return x;
  }
  
  public void delete(Key key){
    root = delete(root,key);
  }
  private Node delete(Node x, Key key){
    if(x == null) return null;
    int cmp = key.compareTo(x.key);
    if(cmp < 0) x.left = delete(x.left,key);
    else if(cmp > 0) x.right = delete(x.right,key);
    else{
      if(x.right == null) return x.left;
      if(x.left == null ) return x.right;
      /**
       * 如果被刪除節(jié)點(diǎn)有兩個(gè)子樹,將被刪除節(jié)點(diǎn)暫記為t
       * 從t的右子樹中選取最小的節(jié)點(diǎn)x,將這個(gè)節(jié)點(diǎn)x的左子樹設(shè)為t的左子樹
       * 這個(gè)節(jié)點(diǎn)x的右子樹設(shè)為t的右子樹中刪除了最小節(jié)點(diǎn)的子樹,這樣就成功替換了t的位置
       */
      Node t = x;
      x = min(t.right);
      x.left = t.left;
      x.right = deleteMin(t.right);
    }
    x.n = size(x.left) + size(x.right) +1;
    return x;
  }
  
  public void print(){
    print(root);
  }
  private void print(Node x){
    if(x == null ) return;
    print(x.left);
    StdOut.println(x.key);
    print(x.right);
  }
  
  public Iterable<Key> keys(){
    return keys(min(),max());
  }
  public Iterable<Key> keys(Key lo, Key hi){
    Queue<Key> queue = new Queue<Key>();
    keys(root, queue, lo, hi);
    return queue;
  }
  private void keys(Node x, Queue<Key> queue, Key lo, Key hi){
    if(x == null) return;
    int cmplo = lo.compareTo(x.key);
    int cmphi = lo.compareTo(x.key);
    if(cmplo < 0 ) keys(x.left,queue,lo,hi);
    if(cmplo <= 0 && cmphi >= 0) queue.enqueue(x.key);
    if(cmphi > 0 ) keys(x.right,queue,lo,hi);
  }
}

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

原文鏈接:http://www.cnblogs.com/evasean/p/7327278.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 亚洲伊人久久综合 | 国产精品美女视频 | 成人精品一区二区三区中文字幕 | 久草视频在线播放 | 精品中出 | 亚洲国产免费av | 蜜臀久久精品99国产精品日本 | 日韩欧美三级 | 国产精品福利91 | 中国一级特黄毛片大片 | 可以免费看黄色的网站 | 亚洲精品永久免费 | 亚洲久久 | av成人在线观看 | 精品久久一二三区 | 国产综合在线视频 | 夜夜爽av福利精品导航 | 精品欧美一区二区三区久久久 | 男女视频网址 | 免费成年人视频在线观看 | 日本在线免费 | 国产精品日本一区二区不卡视频 | 国产视频在线播放 | 国产精品中文字幕在线观看 | 国产日韩欧美在线 | 日韩在线视频在线观看 | 天堂欧美城网站网址 | 国产一级黄 | 日韩成人免费中文字幕 | 视频一区二区三区在线观看 | 亚洲视频一区二区三区在线观看 | 欧美视频在线一区 | 成人免费一区二区三区视频网站 | 香蕉亚洲 | 欧美精品一区二区三区蜜桃视频 | 午夜国产在线 | 依人在线观看 | 亚洲91| 亚洲一区二区高清 | 国产乱码精品1区2区3区 | 黄色影视 |