result = currentEntry; if (entry.compareTo(currentEntry) < 0) currentNode = currentNode.getLeftChild(); if(entry.compareTo(currentEntry) >= 0) currentNode = currentNode.getRightChild(); array.add(currentEntry); if (searchVal.compareTo(currentEntry) < 0) currentNode = currentNode.getLeftChild(); if(searchVal.compareTo(currentEntry) >= 0) currentNode = currentNode.getRightChild();
import java.util.ArrayList; import java.util.Queue; public ArrayList<T> T getallenteries(T entry) { ArrayList<T> result = new ArrayList<T>(); Queue<T> q=(Queue<T>) new Queue<T>(); boolean found = false; BinaryNodeInterface<T> currentNode = getRootNode(); while ((!found||!q.isEmpty())&¤tNode!=null) { if(!found)T currentEntry = currentNode.getData(); if(found)T currentEntry = q.poll(); if (entry.equals(currentEntry)) { result.add(currentEntry); found=true; if(currentNode.getLeftChild()!=null)q.add(currentNode.getLeftChild()); if(currentNode.getRightChild()!=null)q.add(currentNode.getRightChild()); } else if (entry.compareTo(currentEntry) < 0) if(!found) currentNode = currentNode.getLeftChild(); else{ if(!found) currentNode = currentNode.getRightChild(); } } return result;}
public BinarySearchTreeNode<T> getParent(BinarySearchTreeNode<T> e) { if (e == null) { return null; } BinarySearchTreeNode<T> current = this.root; T eValue = e.getValue(); while (current != null) { if (howManyChildren(current) == 0) { return null; } else if ((current.getLeft()!=null && eValue.equals(current.getLeft().getValue())) || (current.getRight()!=null) && eValue.equals(current.getRight().getValue())) { return current; } else if (eValue.compareTo(current.getValue()) < 0) { current = current.getLeft(); } else { current = current.getRight(); } } return null; }
private int binarySearchRecursive(int first, int last, T desiredItem) { int position = 0; int mid = (first + last) / 2; if (first > last) position = -(last + 1); else if (desiredItem.equals(list[mid])) position = mid; else if (desiredItem.compareTo(list[mid]) < 0) position = binarySearchRecursive(first, mid -1, desiredItem); else position = binarySearchRecursive(mid + 1, last, desiredItem); return position; }