MCPcopy Create free account
hub / github.com/algorithmzuo/algorithm-journey / add

Method add

src/class151/FollowUp1.java:59–81  ·  view source on GitHub ↗
(int i, int num)

Source from the content-addressed store, hash-verified

57 }
58
59 public static int add(int i, int num) {
60 if (i == 0) {
61 key[++cnt] = num;
62 count[cnt] = size[cnt] = 1;
63 priority[cnt] = Math.random();
64 return cnt;
65 }
66 if (key[i] == num) {
67 count[i]++;
68 } else if (key[i] > num) {
69 left[i] = add(left[i], num);
70 } else {
71 right[i] = add(right[i], num);
72 }
73 up(i);
74 if (left[i] != 0 && priority[left[i]] > priority[i]) {
75 return rightRotate(i);
76 }
77 if (right[i] != 0 && priority[right[i]] > priority[i]) {
78 return leftRotate(i);
79 }
80 return i;
81 }
82
83 public static void add(int num) {
84 head = add(head, num);

Callers 1

mainMethod · 0.95

Calls 4

upMethod · 0.95
rightRotateMethod · 0.95
leftRotateMethod · 0.95
randomMethod · 0.45

Tested by

no test coverage detected