我正在尝试在二叉树中搜索用户输入的值,我遇到了搜索问题。我想返回导致搜索词的树的分支,或者返回搜索词的添加路径。路径将是节点值,加上指向左或右的箭头表示所采用的分支。 即搜索:6; 20< -10>> 6 OR 20< -10>>进入会到这里
我认为,我的问题在于搜索方法。代码编译,但是当运行搜索时,我得到返回的默认值null。
驱动程序类:
public class Driver {
/*****
* Main
*
* @param args Command Line Arguments
* @throws NullPointerException
*/
public static void main(String[] args) throws NullPointerException {
//Locale Objects and Variables
TreeNode root = new TreeNode();
Scanner scan = new Scanner(System.in);
boolean loop;
//Continuation Loop
do {
//Fill the Tree
System.out.println("Adding Initial Ints");
for (int i = 0; i < 15; i++) {
System.out.print("\nEnter an Int: ");
root.add(scan.nextInt());
}
//Search Prompt
System.out.print("\nSearch For: ");
System.out.print(root.search(scan.nextInt()));
//Continuation Statement
System.out.println("Continue? Yes to cont: ");
String input = scan.next();
if (input.equalsIgnoreCase("YES") || input.equalsIgnoreCase("Y")) {
loop = true;
} else {
loop = false;
} //IF-ELSE Tree
} while (loop); //DO-WHILE LOOP
} //MAIN
} //DRIVER
树节点类:
public class TreeNode {
//Instance Variables
private int data;
private TreeNode left;
private TreeNode right;
/*****
* Constructor
* Constructs a new TreeNode Object with the 3 instance variables passed
*
* @param initialData int value held by the object
* @param initialLeft left link
* @param initialRight right link
*/
public TreeNode(int initialData, TreeNode initialLeft, TreeNode initialRight) {
data = initialData;
left = initialLeft;
right = initialRight;
} //TreeNode
/*****
* Constructor
* Constructs the Object with only the data passed
*
* @param initialData Data to give the node
*/
public TreeNode(int initialData) {
data = initialData;
left = null;
right = null;
} //TreeNode
/*****
* Constructor
* Default Constructor
*/
public TreeNode() {
} //TreeNode
/*****
* Add
* Add a new item to the tree, sorting as it adds. Left if the new info is less than the current node, Right if greater than
*
* @param info Data for the new Node
*/
public void add(int info) {
/*Determines where to add the new information*/
if (this == null) {
setData(info);
} else if (this.isLeaf()) {
if (info < data) {
left = new TreeNode(info);
} else {
right = new TreeNode(info);
}
} else if (info < data) {
left.add(data);
} else if (info > data) {
right.add(data);
} else if (info == data) {
System.out.print("Invalid Input--Duplicate");
}else {
System.out.print("SOMETHING WENT WRONG---TREENODE_dot_ADD");
} //IF-ELSE Tree
} //ADD
/*****
* Search
* Searches for the entry in the tree
*
* @param entry int to search for in the tree
* @return the location of the int in the tree
* @throws NullPointerException
*/
public String search(int entry) throws NullPointerException {
String result = new String();
if (data == entry) {
return result + data;
} else if (this.isLeaf()) {
return result + " Not Found, Would Go Here";
}else if (entry < data) {
if (left.isLeaf())
return result + " Not Found, Would Go Here";
left.search(entry, result + data + " <- ");
} else if (entry > data) {
if (right.isLeaf())
return result + " Not Found, Would Go Here";
right.search(entry, result + data + " <- ");
} //IF-ELSE Tree
return null;
} //SEARCH
/*****
* Search
* Searches for the Entry in the Tree. Also receives a String to append with the new result information
*
* @param entry
* @param result
* @return The location of the entry in the tree
* @throws NullPointerException
*/
public String search(int entry, String result) throws NullPointerException {
if (data == entry) {
return result + data;
} else if (this.isLeaf()) {
return result + " Not Found, Would Go Here";
}else if (entry < data) {
if (left.isLeaf())
return result + " Not Found, Would Go Here";
left.search(entry, result + data + " <- ");
} else if (entry > data) {
if (right.isLeaf())
return result + " Not Found, Would Go Here";
right.search(entry, result + data + " <- ");
} //IF-ELSE Tree
return null;
} //Search
/*****
* Is Leaf
* Determines if the node has any children
*
* @return true if this node is a leaf
*/
public boolean isLeaf() {
if (right == null && left == null) {
return true;
} else {
return false;
} //IF-ELSE TREE
} //IS LEAF
/*****
* Remove Leftmost
* Deletes the node that follows the tree to the left
*
* @return Used to recursively access the Tree
*/
public TreeNode removeLeftmost() {
if (left == null)
return right;
else {
left = left.removeLeftmost( );
return this;
} //IF-ELSE TREE
} //REMOVE LEFTMOST
/*****
* Remove Rightmost
* Deletes the node that follows the tree to the right
*
* @return Used to recursively access the Tree
*/
public TreeNode removeRightmost() {
if (right == null)
return left;
else {
right = right.removeRightmost( );
return this;
} //IF-ELSE TREE
} //REMOVE RIGHTMOST
/*****
* Size
* Determines the size of the tree
*
* @return The number of nodes in the tree
*/
public int size() {
if (this == null) {
return 0;
} else {
return 1 + left.size() + right.size();
} //IF-ELSE Tree
} //SIZE
/*****
* Tree Copy
* Makes a copy of the Tree
*
* @param source root to start the copy
* @return a Tree rooted at source
* @throws OutOfMemoryError
*/
public static TreeNode treeCopy(TreeNode source) throws OutOfMemoryError {
TreeNode leftCopy, rightCopy;
if (source == null) {
return null;
} else {
leftCopy = treeCopy(source.left);
rightCopy = treeCopy(source.right);
return new TreeNode(source.data, leftCopy, rightCopy);
} //IF-ELSE Tree
} //TREE COPY
/*****
* Get Right
* Retrieve the child on the right
*
* @return the right child node
*/
public TreeNode getRight() {
return right;
} //GET RIGHT
/*****
* Get Left
* Retrieve the child on the left
*
* @return The left child node
*/
public TreeNode getLeft() {
return left;
} //GET LEFT
/*****
* Get Data
* Retrieve the data of the current node
*
* @return the data of the current node
*/
public Integer getData() {
return data;
} //GET DATA
/*****
* Get Leftmost Data
* Follows the tree to the left, and gets the data
*
* @return the data all the way on the left
*/
public Integer getLeftmostData() {
if (left.isLeaf()) {
return data;
} else {
return left.getLeftmostData();
} //IF-ELSE TREE
} //GET LEFTMOST DATA
/*****
* Get Rightmost Data
* Follows the tree to the right, and gets the data
*
* @return the data all the way on the right
*/
public Integer getRightmostData() {
if (right.isLeaf()) {
return data;
} else {
return right.getRightmostData();
} //IF-ELSE TREE
} //GET RIGHTMOST DATA
/*****
* Set Data
* sets the current data to data
*
* @param data new data for node
*/
public void setData(Integer data) {
this.data = data;
} //SET DATA
/*****
* Set Left
* points current left to left
*
* @param left node to point left branch at
*/
public void setLeft(TreeNode left) {
this.left = left;
} //SET LEFT
/*****
* Set Right
* Points current right to right
*
* @param right node to point right branch at
*/
public void setRight(TreeNode right) {
this.right = right;
} //SET RIGHT
} //TREE NODE