susancbk
04-19-2005, 10:33 AM
I've recently learned about recursion and created the following methods for a program which displays a binary tree
publi Queue listAscending () {
Queue queue = new Queue();
recursiveListAscending (root, queue);
return queue;
}
private static void recursiveListAscending (Node root, Queue queue) {
if (root != null) {
recursiveListAscending(root.getLeftChild(),queue);
queue.add (root.getData());
recursiveListAscending(root.getRightChild(),queue);
}
}
seeing how handy recursion can be, I wanted to apply the same logic to another program I have which counts occursions in a binary tree.
here's what the code looks like
/**
* Searches the binary tree for a node containing an item
* with the specified value. If found, returns the occurrence
* count for that value. Otherwise, returns 0.
* @return Occurrence count for the specified value.
*/
public int getOccurrences (String value) {
Node curNode = root;
Node prevNode = null;
if (isEmpty()) { // check if tree is empty
return 0;
}
else {
Node preNode = null;
int result = 0;
while (curNode != null){
Item curItem = (Item)curNode.getData(); // get data of item (leaf) we're looking at
String curValue = curItem.getValue(); // get value of item (leaf) we're looking at
result = value.compareTo(curValue); // compare leaf we'return looking at to the value we're entering
if ( result == 0) { // if its already there
return curItem.getOccurrences();
}
if ( result < 0 ) {
preNode = curNode;
curNode = curNode.getLeftChild();
}
else if ( result > 0) {
preNode = curNode;
curNode = curNode.getRightChild();
}
}
}
return 0;
}
How would I go about using the same principles in the first two methods, to break the second method down into two and apply recursion?
publi Queue listAscending () {
Queue queue = new Queue();
recursiveListAscending (root, queue);
return queue;
}
private static void recursiveListAscending (Node root, Queue queue) {
if (root != null) {
recursiveListAscending(root.getLeftChild(),queue);
queue.add (root.getData());
recursiveListAscending(root.getRightChild(),queue);
}
}
seeing how handy recursion can be, I wanted to apply the same logic to another program I have which counts occursions in a binary tree.
here's what the code looks like
/**
* Searches the binary tree for a node containing an item
* with the specified value. If found, returns the occurrence
* count for that value. Otherwise, returns 0.
* @return Occurrence count for the specified value.
*/
public int getOccurrences (String value) {
Node curNode = root;
Node prevNode = null;
if (isEmpty()) { // check if tree is empty
return 0;
}
else {
Node preNode = null;
int result = 0;
while (curNode != null){
Item curItem = (Item)curNode.getData(); // get data of item (leaf) we're looking at
String curValue = curItem.getValue(); // get value of item (leaf) we're looking at
result = value.compareTo(curValue); // compare leaf we'return looking at to the value we're entering
if ( result == 0) { // if its already there
return curItem.getOccurrences();
}
if ( result < 0 ) {
preNode = curNode;
curNode = curNode.getLeftChild();
}
else if ( result > 0) {
preNode = curNode;
curNode = curNode.getRightChild();
}
}
}
return 0;
}
How would I go about using the same principles in the first two methods, to break the second method down into two and apply recursion?