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

Method add

src/class151/Code02_Treap1.java:74–96  ·  view source on GitHub ↗
(int i, int num)

Source from the content-addressed store, hash-verified

72 }
73
74 public static int add(int i, int num) {
75 if (i == 0) {
76 key[++cnt] = num;
77 count[cnt] = size[cnt] = 1;
78 priority[cnt] = Math.random();
79 return cnt;
80 }
81 if (key[i] == num) {
82 count[i]++;
83 } else if (key[i] > num) {
84 left[i] = add(left[i], num);
85 } else {
86 right[i] = add(right[i], num);
87 }
88 up(i);
89 if (left[i] != 0 && priority[left[i]] > priority[i]) {
90 return rightRotate(i);
91 }
92 if (right[i] != 0 && priority[right[i]] > priority[i]) {
93 return leftRotate(i);
94 }
95 return i;
96 }
97
98 public static void add(int num) {
99 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