Deze knop handler's doel is om een binaire boom te zoeken naar een locatie voor een record in een random access bestand. De methode fillInfoField is er om de GUI te bevolken met de geretourneerde gegevens. Alle hulp wordt zeer gewaardeerd worden!
private class HandlerSSN implements ActionListener {
public void actionPerformed(ActionEvent event) {
String ssnReqStr = tfReqSSN.getText();
String num;
int ssn;
BalanceOnDemand.Node currNode = null;
BalanceOnDemand myTree = new BalanceOnDemand();
if (ssnReqStr.length() == 0) {
tfMsg.setText(Lookup by Name (partial match allowed));
tfReqName.requestFocus();
return;
} else {
try {
raf.seek(0);
myTree.root = (BalanceOnDemand.Node) ois.readObject();
num = ssnReqStr.replaceAll([^0-9], );
ssn = Integer.parseInt(num);
currNode = myTree.find(ssn);
System.out.println(currNode);
if(currNode != null){
raf.seek(currNode.loc - REC_LEN);
fillInfoFields(readCurrRec());
}else{
System.out.println(Test);
tfMsg.setText(SSN \ + tfReqSSN.getText() + \ was not found);
return;
}
} catch (IOException | ClassNotFoundException e) {
System.out.println(currNode.id);
tfMsg.setText(SSN \ + tfReqSSN.getText()
+ \ was not found);
}
}
}
}
Hier is de vondst methode als je zou willen om het te zien.
public Node find(int key)
{
Node current;
current = root;
while(current!=null && current.id!=key)
{
if(key<current.id){
current = current.left;
}else{
current = current.right;
}
}
return current;
}
class Node implements Serializable
{
private static final long serialVersionUID = 1L;
public int id;
public int loc;
public Node left;
public Node right;
public Node(int i,int i2)
{
id = i;
loc = i2;
left = null;
right = null;
}
}













