I'm facing a problem with my binary search tree. After add in all the String value, I am able to search what I have searched before. But, it cant retrieve the value stored in tree.right(or tree.left). Even if my tree.right have the value, but when I search it, it will return "Record Not Found". Output and Code shown below:
public void add(String value) {
if(value.toLowerCase().equals("red") || value.toLowerCase().equals("green") || value.toLowerCase().equals("blue") ||
value.toLowerCase().equals("yellow") || value.toLowerCase().equals("black")) {
if (root == null) {
root = new StringTreeNode(value.toLowerCase());
size ++;
}
else
addleaf(root, value.toLowerCase());
}
}
public void addleaf(StringTreeNode branch, String value) {
if(value.compareTo(branch.data) < 0) {
if (branch.left == null) {
branch.left = new StringTreeNode(value);
size ++;
} else
addleaf(branch.left, value);
} else if (value.compareTo(branch.data) > 0) {
if (branch.right == null) {
branch.right = new StringTreeNode(value);
size ++;
} else
addleaf(branch.right, value);
} else {
}
}
public void searchNode (String value) {
found = false;
if(root == null)
System.out.println("Nothing here");
else
searchBranch(root, value.toLowerCase());
if (!found)
System.out.println("Records not found.");
}
public void searchBranch (StringTreeNode tmp, String value) {
if(value.equals(tmp.data)) {
System.out.println("Records found, " + value + " exist in search history!");
found = true;
} else if (tmp.left != null)
searchBranch(tmp.left, value);
else if (tmp.right != null)
searchBranch(tmp.right, value);
}
Output
What do you want to search?(0 to exit) : red
What do you want to search?(0 to exit) : green
What do you want to search?(0 to exit) : blue
What do you want to search?(0 to exit) : yellow
What do you want to search?(0 to exit) : black
What do you want to search?(0 to exit) : 0
Tree Value : red green blue black yellow
Stack Value : 1. black 2. yellow 3. blue 4. green 5. red
What do you want to search for the existence history?(0 to exit) : red
Records found, red exist in search history!
What do you want to search for the existence history?(0 to exit) : green
Records found, green exist in search history!
What do you want to search for the existence history?(0 to exit) : blue
Records found, blue exist in search history!
**What do you want to search for the existence history?(0 to exit) : yellow
Records not found.**
What do you want to search for the existence history?(0 to exit) : black
Records found, black exist in search history!
I am curious although "yellow" is added into tree, but "yellow" is not in my search result.
Your searchBranch
method is using the wrong criteria to decide which child branch to search. It should be comparing tmp.data
to value
to decide between the left and right branches, not just going down whichever branch happens to lead somewhere:
public void searchBranch (StringTreeNode tmp, String value) {
if(value.equals(tmp.data)) {
System.out.println("Records found, " + value + " exist in search history!");
found = true;
} else if (value.compareTo(tmp.data) < 0)
searchBranch(tmp.left, value);
else
searchBranch(tmp.right, value);
}
Notice that this is basically the same logic that addLeaf
uses when deciding whether a given value
belongs in the left or right branch of a node.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.