Changed find(int key) method to return null when node is not found, and updated docs accordingly. Issue #104.

This commit is contained in:
mpokryva 2017-09-29 11:21:47 -04:00
parent 0aee19d427
commit e5381585a5

View File

@ -1,20 +1,20 @@
/** /**
* This entire class is used to build a Binary Tree data structure. * This entire class is used to build a Binary Tree data structure.
* There is the Node Class and the Tree Class, both explained below. * There is the Node Class and the Tree Class, both explained below.
* *
* @author Unknown * @author Unknown
* *
*/ */
/** /**
* This class implements the nodes that will go on the Binary Tree. * 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 * 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. * to the right, and the parent from which they came from.
* *
* @author Unknown * @author Unknown
* *
*/ */
class Node{ class Node{
/** Data for the node */ /** Data for the node */
public int data; public int data;
@ -26,10 +26,10 @@ class Node{
public Node parent; public Node parent;
/** /**
* Constructor of Node * Constructor of Node
* *
* @param value Value to put in the node * @param value Value to put in the node
*/ */
public Node(int value){ public Node(int value){
data = value; data = value;
left = null; left = null;
@ -40,56 +40,54 @@ class Node{
/** /**
* A binary tree is a data structure in which an element * A binary tree is a data structure in which an element
* has two successors(children). The left child is usually * has two successors(children). The left child is usually
* smaller than the parent, and the right child is usually * smaller than the parent, and the right child is usually
* bigger. * bigger.
* *
* @author Unknown * @author Unknown
* *
*/ */
class Tree{ class Tree{
/** The root of the Binary Tree */ /** The root of the Binary Tree */
private Node root; private Node root;
/** /**
* Constructor * Constructor
*/ */
public Tree(){ public Tree(){
root = null; root = null;
} }
/** /**
* Method to find a Node with a certain value * Method to find a Node with a certain value
* *
* @param key Value being looked for * @param key Value being looked for
* @return The node if it finds it, otherwise returns the parent * @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 current = root;
Node last = root; while (current != null) {
while(current != null){ if(key < current.data) {
last = current;
if(key < current.data)
current = current.left; current = current.left;
else if(key > current.data) } else if(key > current.data) {
current = current.right; current = current.right;
//If you find the value return it } else { // If you find the value return it
else
return current; return current;
}
} }
return last; return null;
} }
/** /**
* Inserts certain value into the Binary Tree * Inserts certain value into the Binary Tree
* *
* @param value Value to be inserted * @param value Value to be inserted
*/ */
public void put(int value){ public void put(int value){
Node newNode = new Node(value); Node newNode = new Node(value);
if(root == null) if(root == null)
root = newNode; root = newNode;
else{ else{
//This will return the soon to be parent of the value you're inserting //This will return the soon to be parent of the value you're inserting
Node parent = find(value); Node parent = find(value);
@ -109,29 +107,29 @@ class Tree{
} }
/** /**
* Deletes a given value from the Binary Tree * Deletes a given value from the Binary Tree
* *
* @param value Value to be deleted * @param value Value to be deleted
* @return If the value was deleted * @return If the value was deleted
*/ */
public boolean remove(int value){ public boolean remove(int value){
//temp is the node to be deleted //temp is the node to be deleted
Node temp = find(value); Node temp = find(value);
//If the value doesn't exist //If the value doesn't exist
if(temp.data != value) if(temp.data != value)
return false; return false;
//No children //No children
if(temp.right == null && temp.left == null){ if(temp.right == null && temp.left == null){
if(temp == root) if(temp == root)
root = null; root = null;
//This if/else assigns the new node to be either the left or right child of the parent //This if/else assigns the new node to be either the left or right child of the parent
else if(temp.parent.data < temp.data) else if(temp.parent.data < temp.data)
temp.parent.right = null; temp.parent.right = null;
else else
temp.parent.left = null; temp.parent.left = null;
return true; return true;
} }
@ -162,9 +160,9 @@ class Tree{
//This if/else assigns the new node to be either the left or right child of the parent //This if/else assigns the new node to be either the left or right child of the parent
if(temp.parent.data < temp.data) if(temp.parent.data < temp.data)
temp.parent.right = successor; temp.parent.right = successor;
else else
temp.parent.left = successor; temp.parent.left = successor;
return true; return true;
} }
} }
@ -175,96 +173,96 @@ class Tree{
if(temp == root){ if(temp == root){
root = temp.right; return true;} root = temp.right; return true;}
temp.right.parent = temp.parent; temp.right.parent = temp.parent;
//Assigns temp to left or right child //Assigns temp to left or right child
if(temp.data < temp.parent.data) if(temp.data < temp.parent.data)
temp.parent.left = temp.right; temp.parent.left = temp.right;
else else
temp.parent.right = temp.right; temp.parent.right = temp.right;
return true; return true;
}
//If it has a left child
else{
if(temp == root){
root = temp.left; return true;}
temp.left.parent = temp.parent;
//Assigns temp to left or right side
if(temp.data < temp.parent.data)
temp.parent.left = temp.left;
else
temp.parent.right = temp.left;
return true;
}
}
} }
//If it has a left child
else{
if(temp == root){
root = temp.left; return true;}
temp.left.parent = temp.parent; /**
* This method finds the Successor to the Node given.
* Move right once and go left down the tree as far as you can
*
* @param n Node that you want to find the Successor of
* @return The Successor of the node
*/
public Node findSuccessor(Node n){
if(n.right == null)
return n;
Node current = n.right;
Node parent = n.right;
while(current != null){
parent = current;
current = current.left;
}
return parent;
}
//Assigns temp to left or right side /**
if(temp.data < temp.parent.data) * Returns the root of the Binary Tree
temp.parent.left = temp.left; *
else * @return the root of the Binary Tree
temp.parent.right = temp.left; */
return true; public Node getRoot(){
return root;
}
/**
* Prints leftChild - root - rightChild
*
* @param localRoot The local root of the binary tree
*/
public void inOrder(Node localRoot){
if(localRoot != null){
inOrder(localRoot.left);
System.out.print(localRoot.data + " ");
inOrder(localRoot.right);
}
}
/**
* Prints root - leftChild - rightChild
*
* @param localRoot The local root of the binary tree
*/
public void preOrder(Node localRoot){
if(localRoot != null){
System.out.print(localRoot.data + " ");
preOrder(localRoot.left);
preOrder(localRoot.right);
}
}
/**
* Prints rightChild - leftChild - root
*
* @param localRoot The local root of the binary tree
*/
public void postOrder(Node localRoot){
if(localRoot != null){
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.data + " ");
}
} }
} }
}
/**
* This method finds the Successor to the Node given.
* Move right once and go left down the tree as far as you can
*
* @param n Node that you want to find the Successor of
* @return The Successor of the node
*/
public Node findSuccessor(Node n){
if(n.right == null)
return n;
Node current = n.right;
Node parent = n.right;
while(current != null){
parent = current;
current = current.left;
}
return parent;
}
/**
* Returns the root of the Binary Tree
*
* @return the root of the Binary Tree
*/
public Node getRoot(){
return root;
}
/**
* Prints leftChild - root - rightChild
*
* @param localRoot The local root of the binary tree
*/
public void inOrder(Node localRoot){
if(localRoot != null){
inOrder(localRoot.left);
System.out.print(localRoot.data + " ");
inOrder(localRoot.right);
}
}
/**
* Prints root - leftChild - rightChild
*
* @param localRoot The local root of the binary tree
*/
public void preOrder(Node localRoot){
if(localRoot != null){
System.out.print(localRoot.data + " ");
preOrder(localRoot.left);
preOrder(localRoot.right);
}
}
/**
* Prints rightChild - leftChild - root
*
* @param localRoot The local root of the binary tree
*/
public void postOrder(Node localRoot){
if(localRoot != null){
postOrder(localRoot.left);
postOrder(localRoot.right);
System.out.print(localRoot.data + " ");
}
}
}