Changed find(int key) method to return null when node is not found, and updated docs accordingly. Issue #104.
This commit is contained in:
parent
0aee19d427
commit
e5381585a5
@ -1,20 +1,20 @@
|
||||
/**
|
||||
* This entire class is used to build a Binary Tree data structure.
|
||||
* There is the Node Class and the Tree Class, both explained below.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
* This entire class is used to build a Binary Tree data structure.
|
||||
* There is the Node Class and the Tree Class, both explained below.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* This class implements the nodes that will go on the Binary Tree.
|
||||
* They consist of the data in them, the node to the left, the node
|
||||
* to the right, and the parent from which they came from.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
* This class implements the nodes that will go on the Binary Tree.
|
||||
* They consist of the data in them, the node to the left, the node
|
||||
* to the right, and the parent from which they came from.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Node{
|
||||
/** Data for the node */
|
||||
public int data;
|
||||
@ -40,14 +40,14 @@ class Node{
|
||||
|
||||
|
||||
/**
|
||||
* A binary tree is a data structure in which an element
|
||||
* has two successors(children). The left child is usually
|
||||
* smaller than the parent, and the right child is usually
|
||||
* bigger.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
* A binary tree is a data structure in which an element
|
||||
* has two successors(children). The left child is usually
|
||||
* smaller than the parent, and the right child is usually
|
||||
* bigger.
|
||||
*
|
||||
* @author Unknown
|
||||
*
|
||||
*/
|
||||
class Tree{
|
||||
/** The root of the Binary Tree */
|
||||
private Node root;
|
||||
@ -65,20 +65,18 @@ class Tree{
|
||||
* @param key Value being looked for
|
||||
* @return The node if it finds it, otherwise returns the parent
|
||||
*/
|
||||
public Node find(int key){
|
||||
public Node find(int key) {
|
||||
Node current = root;
|
||||
Node last = root;
|
||||
while(current != null){
|
||||
last = current;
|
||||
if(key < current.data)
|
||||
while (current != null) {
|
||||
if(key < current.data) {
|
||||
current = current.left;
|
||||
else if(key > current.data)
|
||||
} else if(key > current.data) {
|
||||
current = current.right;
|
||||
//If you find the value return it
|
||||
else
|
||||
} else { // If you find the value return it
|
||||
return current;
|
||||
}
|
||||
return last;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -267,4 +265,4 @@ class Tree{
|
||||
System.out.print(localRoot.data + " ");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user