private int minKeyInTree(TwoThreeNode toSearch) { int toReturn = toSearch.getFirstKey(); if( !toSearch.isLeafNode() ){ toReturn = minKeyInTree(toSearch.getLeft()); } return toReturn; }
private void printTreeRec(TwoThreeNode curNode) { if (curNode.isLeafNode()) { Console.Out.Write(curNode.getFirstKey().ToString() + " "); } else { //Console.Out.Write("Getting Left "); printTreeRec(curNode.getLeft()); //Console.Out.Write("Getting Mid "); printTreeRec(curNode.getMid()); if (curNode.hasTwoNodes() == false) { //Console.Out.Write("Getting Right "); printTreeRec(curNode.getRight()); } } }
private TwoThreeNode addRec(TwoThreeNode curNode, int key) { TwoThreeNode toReturn = null; TwoThreeNode returned = null; TwoThreeNode newLeft = null; TwoThreeNode newRight = null; TwoThreeNode newParent = null; if(!curNode.isLeafNode()){ //A key has been pushed up, which needs to be added to the curNode node if (curNode.hasTwoNodes() == true) { if(key < curNode.getFirstKey()){ returned = addRec(curNode.getLeft(), key); }else{ returned = addRec(curNode.getMid(), key); } if(returned != null){ if (returned.getFirstKey() < curNode.getFirstKey()) { curNode.toggleNodeNum(); curNode.setRight(curNode.getMid()); curNode.setMid(returned.getMid()); curNode.setLeft(returned.getLeft()); curNode.setSecondKey(curNode.getFirstKey()); curNode.setFirstKey(minKeyInTree(curNode.getMid())); }else if(returned.getFirstKey() >= curNode.getFirstKey()){ curNode.toggleNodeNum(); curNode.setRight(returned.getMid()); curNode.setMid(returned.getLeft()); curNode.setFirstKey(minKeyInTree(returned.getLeft())); curNode.setSecondKey(minKeyInTree(returned.getMid())); } } } else { if(key < curNode.getFirstKey()){ returned = addRec(curNode.getLeft(), key); }else if(key < curNode.getSecondKey()){ returned = addRec(curNode.getMid(), key); }else{ returned = addRec(curNode.getRight(), key); } if(returned != null){ if (returned.getFirstKey() < curNode.getFirstKey()) { newLeft = new TwoThreeNode(returned.getLeft(), returned.getMid(), minKeyInTree(returned.getMid())); newRight = new TwoThreeNode(curNode.getMid(), curNode.getRight(), minKeyInTree(curNode.getRight())); } else if (returned.getFirstKey() >= curNode.getFirstKey() && returned.getFirstKey() < curNode.getSecondKey()) { newLeft = new TwoThreeNode(curNode.getLeft(), returned.getLeft(), minKeyInTree(returned.getLeft())); newRight = new TwoThreeNode(returned.getMid(), curNode.getRight(), minKeyInTree(curNode.getRight())); }else{ if (returned.getFirstKey() >= curNode.getSecondKey()) { newLeft = new TwoThreeNode(curNode.getLeft(), curNode.getMid(), minKeyInTree(curNode.getMid())); newRight = new TwoThreeNode(returned.getLeft(), returned.getMid(), minKeyInTree(returned.getMid())); } else { newLeft = new TwoThreeNode(curNode.getLeft(), curNode.getMid(), minKeyInTree(curNode.getMid())); newRight = new TwoThreeNode(returned.getLeft(), returned.getMid(), minKeyInTree(returned.getMid())); } } newParent = new TwoThreeNode(newLeft, newRight, minKeyInTree(newRight)); toReturn = newParent; } } }else{ //Base Case: Return a the parent node consisting of two child nodes, the leaf node if(key < curNode.getFirstKey()){ toReturn = new TwoThreeNode(new TwoThreeNode(key), curNode, curNode.getFirstKey()); }else if(key > curNode.getFirstKey()){ toReturn = new TwoThreeNode(curNode, new TwoThreeNode(key), key); } } return toReturn; }