/** * Time Complexity::O(log n),n=number of elements present.<br> * Memory::O(log n),for traversing stack is formed<br> * You can optimize it using user_defined stack. */ void insertTree(UraInstantiator uraI, BaseNode p, BaseNode root) { if ((tmp2.nodeIndex < p.nodeIndex)) { if (p.left != null) { insertTree(uraI, p.left, p); } else { p.left = tmp2; tmp2 = null; } if ((insertFlag == true) && (vMFlag == true)) { p.nodeIndex += 1l; p.vMFactor += 1l; } } else { long tmpnode = tmp2.nodeIndex; tmpnode -= p.vMFactor; if (tmp2.nodeIndex == p.nodeIndex) { insertFlag = true; ClientData temp = p.data; p.data = tmp2.data; tmp2.data = temp; p.vMFactor += 1l; } tmp2.nodeIndex = tmpnode; if (p.right != null) { insertTree(uraI, p.right, p); } else { p.right = tmp2; tmp2 = null; } vMFlag = true; } uraI.getBaseStructure().checkForUnbalancing(uraI, p, root); }
void insert1(UraInstantiator uraI, long node, ClientData data, BaseNode p) { if (node >= 0l) { insertFlag = false; vMFlag = false; tmp2 = new BaseNode(); tmp2.nodeIndex = node; tmp2.data = data; if (rootTREE == null) { rootTREE = new BaseNode(); rootTREE.left = tmp2; } else { insertTree(uraI, rootTREE.left, rootTREE); } } else System.out.println(" NEGATIVE INDEX ARE NOT ACCEPTED"); }
/** * Rectify the indexes beyond currently inserted .<br> * <br> * Time:: O(log n) */ void findAndCorrect(long node, BaseNode tmp) { if (tmp != null) { if (node < tmp.nodeIndex) { findAndCorrect(node, tmp.left); if (maxFlag == true) { tmp.nodeIndex -= 1l; tmp.vMFactor -= 1l; } } else if (node == tmp.nodeIndex) { if (tmp.right != null) { tmp.vMFactor -= 1l; } else { tmp.vMFactor = 0l; } maxFlag = true; } else { findAndCorrect(node - tmp.vMFactor, tmp.right); } } else { // It's a bug.' } }