private void preOrderTraversalRec(BinaryNode subTree) { if (subTree != null) { Console.Out.Write(subTree.getData()); Console.Out.Write(" "); preOrderTraversalRec(subTree.getLeft()); preOrderTraversalRec(subTree.getRight()); } }
private bool hasValueRec(BinaryNode subTree, int value) { if (subTree == null) { return false; } else if (subTree.getData() == value) { return true; } else if (subTree.getData() > value) { return hasValueRec(subTree.getLeft(), value); } else { return hasValueRec(subTree.getRight(), value); } }
private int getHeightRec(BinaryNode subTree) { if (subTree == null) { return 0; } else { return Math.Max(getHeightRec(subTree.getLeft()), getHeightRec(subTree.getRight())) + 1; } }
//======Private Methods====== private void deleteValueRec(BinaryNode prevNode, BinaryNode subTree, int value) { BinaryNode nextNode = null; if (subTree != null) { if (subTree.getData() == value) { deleteNode(prevNode, subTree, value); } else if (subTree.getData() > value) { nextNode = subTree.getLeft(); deleteValueRec(subTree, nextNode, value); } else { nextNode = subTree.getRight(); deleteValueRec(subTree, nextNode, value); } } }
private void deleteNode(BinaryNode prev, BinaryNode curr, int value) { BinaryNode temp = null; if (prev == null) { if (root.getLeft() == null && curr.getRight() == null) { root = null; } else if (root.getLeft() != null && curr.getRight() == null) { root = root.getLeft(); } else if (root.getLeft() == null && curr.getRight() != null) { root = root.getRight(); } else { temp = root; root = getNDelNextInOrderNode(null, root); root.setLeft(temp.getLeft()); root.setRight(temp.getRight()); } } else { if (curr.getLeft() != null && curr.getRight() == null) { if (prev.getData() > value) { prev.setLeft(curr.getLeft()); } else { prev.setRight(curr.getRight()); } } else if (curr.getLeft() == null && curr.getRight() != null) { if (prev.getData() > value) { prev.setLeft(curr.getRight()); } else { prev.setRight(curr.getRight()); } } else { if (prev.getData() > value) { temp = curr; prev.setLeft(getNDelNextInOrderNode(prev, curr)); prev.getLeft().setLeft(curr.getLeft()); prev.getLeft().setRight(curr.getRight()); } else { temp = curr; prev.setRight(getNDelNextInOrderNode(prev, curr)); prev.getRight().setLeft(curr.getLeft()); prev.getRight().setRight(curr.getRight()); } } } }
private void addRec(BinaryNode subTree, BinaryNode toAdd) { if (subTree.getData() > toAdd.getData()) { if(subTree.getLeft() == null){ subTree.setLeft(toAdd); }else{ addRec(subTree.getLeft(), toAdd); } }else{ if(subTree.getRight() == null){ subTree.setRight(toAdd); }else{ addRec(subTree.getRight(), toAdd); } } }