/***********************************************/ /*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()); } } }
/*Allows the user to add a vertex to to the bst*/ public void add(int data) { if (root == null) { root = new BinaryNode(data); root.setPositionX(1000); root.setPositionY(80); mainRoot = root; graphNode(root.getPositionX(), root.getPositionY(), data, 0); } else { if (search(data) != null) { txtTitle.Text = ("No duplicate vertex allowed!"); } else { add(data, root); } } }