private void deleteNode(int data, BinaryNode currentRoot) { BinaryNode tempNode = search(data); if (tempNode.isLeaf()) { if (position) { parent.setLeftChild(null); } else { parent.setRightChild(null); } } else if (tempNode.hasOneChild()) { if (tempNode.getchildPosition()) { parent.setLeftChild(tempNode.getLeftChild()); } else { parent.setRightChild(tempNode.getRightChild()); } } else { BinaryNode min = getMinor(tempNode.getRightChild()); deleteNode(min.getData()); tempNode.setData(min.getData()); } }
private void insertion(int data, BinaryNode root, int whichChild) { if (whichChild == 0) { if (root.getLeftChild() == null) { root.setLeftChild(new BinaryNode(data)); } else { add(data, root.getLeftChild()); } } else if (whichChild == 1) { if (root.getRightChild() == null) { root.setRightChild(new BinaryNode(data)); } else { add(data, root.getRightChild()); } } }
/***********************************************/ /*Finds the position at which the vertex shoud be inserted and inserts it*/ private void insertion(int data, BinaryNode root, int whichChild) { int line = 0; if (whichChild == 0) { if (root.getLeftChild() == null) { root.setLeftChild(new BinaryNode(data)); if (root != mainRoot) { root.getLeftChild().setPositionX(root.getPositionX() - 100); root.getLeftChild().setPositionY(root.getPositionY() + 100); line = 1; } else { root.getLeftChild().setPositionX(root.getPositionX() - 330); root.getLeftChild().setPositionY(root.getPositionY() + 100); line = 3; } graphNode(root.getLeftChild().getPositionX(), root.getLeftChild().getPositionY(), data, line); } else { add(data, root.getLeftChild()); } } else if (whichChild == 1) { if (root.getRightChild() == null) { root.setRightChild(new BinaryNode(data)); if (root != mainRoot) { root.getRightChild().setPositionX(root.getPositionX() + 100); root.getRightChild().setPositionY(root.getPositionY() + 100); line = 2; } else { root.getRightChild().setPositionX(root.getPositionX() + 330); root.getRightChild().setPositionY(root.getPositionY() + 100); line = 4; } graphNode(root.getRightChild().getPositionX(), root.getRightChild().getPositionY(), data, line); } else { add(data, root.getRightChild()); } } }