I've been programming for about 5 years now, mostly in C++ and Java, while continuing my college education in Computer Science. As an adult student, I'm always looking for opinions and ways to improve from professionals in the field. Aside from reading from numerous text books, I've read these forums for tips and good programming practices to improve.
I'm posting a binary search tree in java to hear any and all feedback to continue with this trend of learning; any positive and negative criticism would be helpful to improve. Thanks in advance.
public class NewBinaryTree {
Node root; // top most node of tree
public void addNode(int key, String name) { // adds node to tree
Node newNode = new Node(key, name);
if(root == null) {
root = newNode;
}
else {
Node focusNode = root;
Node parent;
while(true) {
parent = focusNode;
if(key < focusNode.key){ // checks if the key is less than, then inserts left
focusNode = focusNode.leftChild;
if(focusNode == null) {
parent.leftChild = newNode;
return;
}
}
else { // key is greater than, so inserts on right
focusNode = focusNode.rightChild;
if(focusNode == null) {
parent.rightChild = newNode;
return;
}
}
}
}
}
public void inOrderTraverseTree(Node focusNode) { // in order tree traversal using recursion
if(focusNode != null) { // checks if something is in there
inOrderTraverseTree(focusNode.leftChild);
System.out.println(focusNode);
inOrderTraverseTree(focusNode.rightChild);
}
} // end in order tree traversal
public void preOrderTraverseTree(Node focusNode) { // preorder tree traversal using recursion
if(focusNode != null) { // checks if something is in there
System.out.println(focusNode);
preOrderTraverseTree(focusNode.leftChild);
preOrderTraverseTree(focusNode.rightChild);
}
} // end preorder tree traversal
public void postOrderTraverseTree(Node focusNode) { // post order tree traversal using recursion
if(focusNode != null) { // checks if something is in there
postOrderTraverseTree(focusNode.leftChild);
postOrderTraverseTree(focusNode.rightChild);
System.out.println(focusNode);
}
} // end post order tree traversal
public Node findNode(int key) { // searches tree by key value
Node focusNode = root;
while(focusNode.key != key) {
if(key < focusNode.key) {
focusNode = focusNode.leftChild;
}
else {
focusNode = focusNode.rightChild;
}
if(focusNode == null)
return null;
}
return focusNode;
} // end tree search by key
public boolean remove(int key) {
Node focusNode = root;
Node parent = root;
boolean isItAleftChild = true;
while(focusNode.key != key) {
parent = focusNode;
if(key < focusNode.key) {
isItAleftChild = true;
focusNode = focusNode.leftChild;
} else {
isItAleftChild = false;
focusNode = focusNode.rightChild;
}
if(focusNode == null)
return false;
}
if(focusNode.leftChild == null && focusNode.rightChild == null) {
if(focusNode == root) {
root = null;
} else if(isItAleftChild) {
parent.leftChild = null;
} else {
parent.rightChild = null;
}
}
else if(focusNode.rightChild == null) {
if(focusNode == root)
root = focusNode.leftChild;
else if(isItAleftChild)
parent.leftChild = focusNode.leftChild;
else parent.rightChild = focusNode.leftChild;
}
else if(focusNode.leftChild == null) {
if(focusNode == root)
root = focusNode.rightChild;
else if(isItAleftChild)
parent.leftChild = focusNode.rightChild;
else
parent.rightChild = focusNode.leftChild;
}
else {
Node replacement = getRePlacementNode(focusNode);
if(focusNode == root)
root = replacement;
else if(isItAleftChild)
parent.leftChild = replacement;
else parent.rightChild = replacement;
replacement.leftChild = focusNode.leftChild;
}
return true;
}
public Node getRePlacementNode(Node replacedNode) {
Node replacementParent = replacedNode;
Node replacement = replacedNode;
Node focusNode = replacedNode.rightChild;
while(focusNode != null) {
replacementParent = replacement;
replacement = focusNode;
focusNode = focusNode.leftChild;
}
if(replacement != replacedNode.rightChild) {
replacementParent.leftChild = replacement.rightChild;
replacement.rightChild = replacedNode.rightChild;
}
return replacement;
}
public static void main(String[] args) {
NewBinaryTree theTree = new NewBinaryTree();
// insert testing code
}
}
class Node { // Node class
int key;
String name;
Node leftChild;
Node rightChild;
Node(int key, String name) { // node constructor
this.key = key;
this.name = name;
}
public String toString() { // prints out contents of Nodes
return name + " has a key " + key;
}
} // end node class