Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

46
LINES

< > BotCompany Repo | #1011274 // Array-Based Binary Tree [OK!]

JavaX source code (desktop) [tags: use-pretranspiled] - run with: x30.jar

Download Jar. Libraryless. Click here for Pure Java version (2533L/17K).

1  
!7
2  
3  
sclass ArrayTreeSet<A> {
4  
  A[] array;
5  
  
6  
  *() {}
7  
  *(L<A> l) {
8  
    array = (A[]) new O[roundUpToPowerOfTwo(l(l))];
9  
    l = sorted(l);
10  
    fill(l, 0, l(array), 0);
11  
  }
12  
  
13  
  void fill(L<A> l, int lIdx1, int lIdx2, int aIdx) {
14  
    if (lIdx2 <= lIdx1) ret;
15  
    int middle = (lIdx1+lIdx2)/2;
16  
    array[aIdx] = get(l, middle);
17  
    fill(l, lIdx1, middle, aIdx*2+1);
18  
    fill(l, middle+1, lIdx2, aIdx*2+2);
19  
  }
20  
  
21  
  bool contains(A a) {
22  
    int i = 0;
23  
    while (i < l(array)) {
24  
      int x = array[i] == null ? -1 : cmp(a, array[i]);
25  
      if (x == 0) true;
26  
      i = i*2+(x > 0 ? 2 : 1);
27  
    }
28  
    false;
29  
  }
30  
}
31  
32  
p {
33  
  new L<Int> l;
34  
  for (int n = 0; n <= 64; n++) {
35  
    if (n != 0) l.add(n*10);
36  
    print("n=" + n); assertEquals(n, l(l));
37  
    ArrayTreeSet tree = new ArrayTreeSet(shuffledList(l));
38  
    printStruct(tree);
39  
    for (int i : l) {
40  
      assertTrue(str(i), tree.contains(i));
41  
      assertFalse(tree.contains(i-1));
42  
      assertFalse(tree.contains(i+1));
43  
    }
44  
  }
45  
  print("OK");
46  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 13 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1011274
Snippet name: Array-Based Binary Tree [OK!]
Eternal ID of this version: #1011274/23
Text MD5: fc7e2c79acd8e4e8ee0dcf1141f8fbba
Transpilation MD5: a1b31f67608168d505057540f4735450
Author: stefan
Category: javax
Type: JavaX source code (desktop)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-10-21 23:06:51
Source code size: 1035 bytes / 46 lines
Pitched / IR pitched: No / No
Views / Downloads: 535 / 1242
Version history: 22 change(s)
Referenced in: [show references]