Root node is null after inserting new node into AVLTree [duplicate] - java

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
Closed 7 years ago.
So in simpletons, I am creating my own AVLTree data structure. Now when i add a new node into my tree, it seems to add fine.
EDIT: It doesnt seem to take into account my duplicates (nor add them to the original node's list by key).
But when i print the rootNode to see if it exists it doesn't exist. I can't figure out what the problem is with my add method.
Here is my AVLTree class:
package cw.util;
import java.util.ArrayList;
import java.util.Comparator;
public class AVLTree<K, V>
{
public class Node {
private K key;
private ArrayList<V> valuesList;
private Node left, right;
private int height;
public Node(K key, ArrayList<V> valuesList) {
this.key = key;
this.valuesList = valuesList;
this.height = 0;
}
public Node(V value) {
}
public void addToNode(V value) {
valuesList.add(value);
}
public K getKey() {
return key;
}
public ArrayList<V> getValues() {
return valuesList;
}
public Node getLeftChild() {
return left;
}
public Node getRightChild() {
return right;
}
public int getHeight() {
return height;
}
public Node getChildNodeFromSide(String side) {
switch(side) {
default: return null;
case "left": return left;
case "right": return right;
}
}
}
private Node rootNode;
private Comparator<K> comparator;
//Unused
public AVLTree() {
}
public AVLTree(Comparator<K> comparator) {
this.comparator = comparator;
this.rootNode = null;
}
public V insert(K key, V value) {
Node n = insert(key, value, rootNode);
if(n != null) {
for(V v : n.getValues())
System.out.println(v.toString());
System.out.println();
return value;
} else {
return null;
}
}
public Node insert(K key, V value, Node node) {
ArrayList<V> values = new ArrayList<V>();
values.add(value);
if(node == null)
node = new Node(key, values);
else if(comparator.compare(key, node.key) < 0) {
node.left = insert(key, value, node.left);
if(height(node.left) - height(node.right) == 2) {
if(comparator.compare(key, node.left.key) < 0)
node = rotateWithLeftChild(node);
else
node = doubleRotateWithLeft(node);
}
} else if(comparator.compare(key, node.key) > 0) {
node.right = insert(key, value, node.right);
if(height(node.right) - height(node.left) == 2) {
if(comparator.compare(key, node.right.key) > 0)
node = rotateWithRightChild(node);
else
node = doubleRotateWithRight(node);
}
} else node.getValues().add(value);
node.height = Math.max(height(node.left), height(node.right)) + 1;
return node;
}
public Node search(K key) {
return search(key, rootNode);
}
public Node search(K key, Node node) {
boolean isFound = false;
while((node != null) && !isFound) {
K nodeKey = node.getKey();
if(comparator.compare(key, nodeKey) < 0)
node = node.getLeftChild();
else if(comparator.compare(key, nodeKey) > 0)
node = node.getRightChild();
else {
isFound = true;
}
node = search(key, node);
}
if(isFound) return node;
else return null;
}
//Custom Methods
public boolean isEmpty() {
return rootNode == null;
}
private int height(Node n) {
return n == null ? -1 : n.getHeight();
}
private Node rotateWithLeftChild(Node node2) {
Node node1 = node2.left;
node2.left = node1.right;
node1.right = node2;
node2.height = Math.max(height(node2.left), height(node2.right)) + 1;
node1.height = Math.max(height(node1.left), node2.getHeight()) + 1;
return node1;
}
private Node rotateWithRightChild(Node node1) {
Node node2 = node1.right;
node1.right = node2.left;
node2.left = node1;
node1.height = Math.max(height(node1.left), height(node1.right)) + 1;
node2.height = Math.max(height(node2.left), node1.getHeight()) + 1;
return node2;
}
private Node doubleRotateWithLeft(Node node) {
node.left = rotateWithRightChild(node.left);
return rotateWithLeftChild(node);
}
private Node doubleRotateWithRight(Node node) {
node.right = rotateWithLeftChild(node.right);
return rotateWithRightChild(node);
}
}
Here is how I test the class:
package cw.avl;
import cw.util.AVLTree;
public class AVLTest
{
public static void main(String[] args) {
AVLTree<String, Integer> tree = new AVLTree<String, Integer>(String.CASE_INSENSITIVE_ORDER);
for (int i=1; i <= 10;i++) {
String s = "S" + i;
int x = i;
tree.insert(s, x);
tree.insert(s, x);
}
}
}

Well, you don't seem to ever assign to rootNode, so it starts null and remains so. In fact, your methods create nodes and return them:
if(node == null)
node = new Node(key, values);
...
return node
But you don't use the returned node.
Edit: longer explanation:
When you call from the other function like this: Node n = insert(key, value, rootNode); you are basically saying: Node n = insert(key, value, null);. On the receiving end, here:
public Node insert(K key, V value, Node node) { ,
you are creating a new variable called node with initial value null. Then you replace that value when you do:
node = new Node(key, values);
That value is for the node variable in the insert(K,V,N) method, in no way is rootNode retroactively updated. You could just do so right there:
if(node == null) {
node = new Node(key, values);
rootNode = node;
}

Related

How can i use inner class in outer class in Java?

Im trying to develop Bynary Tree on Java. I have 2 classes BynaryTreeMap and his inner class Elem.
public class Main {
public static void main(String[] args) {
BinaryTreeMap tree = new BinaryTreeMap(22);
System.out.println(tree.MapSearch(22));
tree.insert(10);
System.out.println(tree);
}
}
class BinaryTreeMap {
Elem root;
private static int height;
BinaryTreeMap(int key){
root = new Elem(key);
height = 1;
}
public Elem next(Elem elem){
Elem next;
if (elem.right != null) {
next = this.Minimum(elem.right);
}
else{
next = elem.parent;
while (next != null && elem == next.right){
elem = next;
next = next.parent;
}
}
return next;
}
public void insert(int key){
Elem elem = new Elem(key);
Elem x;
if (root == null)
root = elem;
else {
x = root;
while(true){
if (x.key == key)
throw new RuntimeException("elem is already in tree");
if (key < x.key){
if (x.left == null){
x.setLeft(elem);
break;
}
x = x.left;
}
else
if (x.right == null){
x.setRight(elem);
break;
}
x = x.right;
}
}
}
public Elem descend(int key){
Elem elem = root;
while (elem != null && elem.key != key){
if (key <= elem.key)
elem = elem.left;
else elem = elem.right;
}
return elem;
}
public boolean MapSearch(int key){
return descend(key) != null;
}
public Elem lookUp(int key){
Elem elem = descend(key);
if (elem == null)
throw new RuntimeException("No such elemet");
return elem;
}
public boolean isEmpty(){
return root == null;
}
public Elem Minimum(Elem elem){
Elem min = elem;
while (min.left != null)
min = min.left;
return min;
}
public Elem Maximum(Elem elem){
Elem max = elem;
while (max.right != null)
max = max.right;
return max;
}
#Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder("BinaryTreeMap{");
while (root != null){
stringBuilder.append(root.key);
root = this.next(root);
}
stringBuilder.append("}");
return stringBuilder.toString();
}
public class Elem{
private Elem parent = null;
private Elem left = null;
private Elem right = null;
private final int key;
Elem(int key){
this.key = key;
}
private void setLeft(Elem leftAncestor){
this.left = leftAncestor;
leftAncestor.parent = this;
}
private void setRight(Elem rightAncestor){
this.right = rightAncestor;
rightAncestor.parent = this;
}
}
}
Output
true
BinaryTreeMap{22}
If i run the program in debug mode, my output is worse.
false
BinaryTreeMap{}
Correct output
true
BinaryTreeMap{22 10}
As i can see my root variable became null, but i cant understand why.
Tried to google it, bud didnt succeed. Maybe i didnt uderstand how inner classes work?
Thank you for you help.

Getting a StackOverFlowError whenever I run my AVLTree.java code

At the moment I'm trying to create an AVLTree to store data and and then print the data via in-order traversal. However I'm currently stuck trying to fix a StackOverflowError that seems to be occuring when I call my height() method.
I'm pretty sure the StackOverflowError is resulting from a bad recursion call on my height() method but I don't know why that bad recursion call is happening.
public class AVLTree<K,V> implements AVLTreeI<K,V> {
class Node<K,V> {
K key;
V value;
Node<K,V> leftChild;
Node<K,V> rightChild;
Node<K,V> parent;
public Node(K key, V value) {
this.key = key;
this.value = value;
leftChild = rightChild = parent = null;
}
}
private Node<K,V> root;
private int currentSize;
public AVLTree() {
root = null;
currentSize = 0;
}
public void add(K key, V value) {
Node<K,V> node = new Node<K,V>(key, value);
if (root == null) {
root = node;
currentSize++;
}
add(root, node);
}
private void add(Node<K,V> parent, Node<K,V> newNode) {
if (((Comparable<K>)newNode.key).compareTo(parent.key) > 0) {
if (parent.rightChild == null) {
parent.rightChild = newNode;
newNode.parent = parent;
currentSize++;
}
else
add(parent.rightChild, newNode);
}
else {
if (parent.leftChild == null) {
parent.leftChild = newNode;
newNode.parent = parent;
currentSize++;
}
else
add(parent.leftChild, newNode);
}
checkBalance(newNode);
}
public int height() {
if (root == null)
return 0;
return height(root) - 1;
}
private int height(Node<K,V> node) {
if (node == null)
return 0;
int leftHeight = height(node.leftChild) + 1;
int rightHeight = height(node.rightChild) + 1;
if (leftHeight > rightHeight)
return leftHeight;
return rightHeight;
}
public void checkBalance(Node<K,V> node) {
if ((height(node.leftChild) - height(node.rightChild) > 1) ||
(height(node.leftChild) - height(node.rightChild) < -1)) {
rotate(node);
}
if (node.parent == null)
return;
checkBalance(node.parent);
}
public void rotate (Node<K,V> node) {
if (height(node.leftChild) - height(node.rightChild) > 1) {
if (height(node.leftChild.leftChild) >
height(node.leftChild.rightChild)) {
node = rightRotate(node);
}
else
node = leftRightRotate(node);
}
else {
if (height(node.rightChild.rightChild) >
height(node.rightChild.leftChild)) {
node = leftRotate(node);
}
else
node = rightLeftRotate(node);
}
if (node.parent == null)
root = node;
}
public Node<K,V> leftRotate(Node<K,V> node) {
Node<K,V> tmp = node.rightChild;
node.rightChild = tmp.leftChild;
tmp.leftChild = node;
node.rightChild = tmp.parent;
tmp.parent = node.parent;
tmp.leftChild.parent = tmp;
return tmp;
}
public Node<K,V> rightRotate(Node<K,V> node) {
Node<K,V> tmp = node.leftChild;
node.leftChild = tmp.rightChild;
tmp.rightChild = node;
node.leftChild = tmp.parent;
tmp.parent = node.parent;
tmp.rightChild.parent = tmp;
return tmp;
}
public Node<K,V> rightLeftRotate(Node<K,V> node) {
node.rightChild = rightRotate(node.rightChild);
return leftRotate(node);
}
public Node<K,V> leftRightRotate(Node<K,V> node) {
node.leftChild = leftRotate(node.leftChild);
return rightRotate(node);
}
Your problem is not the height() method (it seems reasonable), but the add() method:
public void add(K key, V value) {
Node<K,V> node = new Node<K,V>(key, value);
if (root == null) {
root = node;
currentSize++;
}
add(root, node);
}
You create a new Node and if the tree is empty you set the node as root node.
But then you go on and add the same node also as a subnode of the root node - which means that the node gets added as left subnode of itself. And it is this that leads to the infinite recursion.
Instead, your add method should add a node either as root node or as a subnode of the root node:
public void add(K key, V value) {
Node<K,V> node = new Node<K,V>(key, value);
if (root == null) {
root = node;
currentSize++;
} else {
add(root, node);
}
}

Creating a binary search tree in java. But output is null

I am trying to create a rudimentary binary search tree in java with an insert and traverse method. The nodes have two local variables, a string and an int, the String value is used to sort the nodes.
Each BST has a local variable pointer to the root node and the nodes are inserted by traversing from the node. There seems to be a problem in creating the root node as my output is consistently producing null instead of.
THE
CAT
HAT
class BST
{
public Node root = null;
private class Node
{
private String key;
private int value;
private Node left;
private Node right;
public Node ()
{
}
public Node (String key, int value)
{
this.key = key;
this.value = value;
}
public String toString ()
{
return ("The key is: "+ this.key +" "+ this.value);
}
}
BST ()
{
}
public void put (String key, int value)
{
put (root, key, value);
}
private void put (Node x, String key, int value)
{
Node newNode = new Node(key, value);
if (x == null)
{
x = newNode;
System.out.println("new node added");
System.out.println(x);
}
int cmp = key.compareTo(x.key);
if (cmp < 0)
put(x.left, key, value);
else if (cmp > 0)
put(x.right, key, value);
else
x.value = value;
}
public void inorder (Node x)
{
if (x != null)
{
inorder (x.left);
System.out.println(x.key);
inorder (x.right);
}
}
public static void main (String [] args)
{
BST bst = new BST();
bst.put(bst.root,"THE", 1);
bst.put(bst.root,"CAT", 2);
bst.put("HAT", 1);
bst.inorder(bst.root);
}
}
Parameters are passed by value. Use the method's return value to alter something:
public void put (String key, int value)
{
root = put (root, key, value);
}
private Node put (Node x, String key, int value)
{
Node newNode = new Node(key, value);
if (x == null)
{
System.out.println("new node added");
System.out.println(x);
return newNode;
}
int cmp = key.compareTo(x.key);
if (cmp < 0)
x.left = put(x.left, key, value);
else if (cmp > 0)
x.right = put(x.right, key, value);
else
x.value = value;
return x;
}
Refer below link , good explanation of BST
http://www.java2novice.com/java-interview-programs/implement-binary-search-tree-bst/
A binary search tree is a node-based data structure, the whole idea of a binary search tree is to keep the data in sorted order so we can search the data in a little faster.There are three kinds of nodes are playing key role in this tree (Parent Node,Left Child Node & Right Child Node).The value of the left child node is always lesser than the value of the parent node, the same as the value of the right child node is always greater than the value of the parent node. Each parent node can have a link to one or two child nodes but not more than two child nodes.
Please find the source code from my tech blog - http://www.algonuts.info/create-a-binary-search-tree-in-java.html
package info.algonuts;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
class BinaryTreeNode {
int nodeValue;
BinaryTreeNode leftChildNode;
BinaryTreeNode rightChildNode;
public BinaryTreeNode(int nodeValue) {
this.nodeValue = nodeValue;
this.leftChildNode = null;
this.rightChildNode = null;
}
public void preorder() {
System.out.print(this.nodeValue+" ");
if(this.leftChildNode != null) {
this.leftChildNode.preorder();
}
if(this.rightChildNode != null) {
this.rightChildNode.preorder();
}
}
public void inorder() {
if(this.leftChildNode != null) {
this.leftChildNode.inorder();
}
System.out.print(this.nodeValue+" ");
if(this.rightChildNode != null) {
this.rightChildNode.inorder();
}
}
public void postorder() {
if(this.leftChildNode != null) {
this.leftChildNode.postorder();
}
if(this.rightChildNode != null) {
this.rightChildNode.postorder();
}
System.out.print(this.nodeValue+" ");
}
}
class BinaryTreeCompute {
private static BinaryTreeNode temp;
private static BinaryTreeNode newNode;
private static BinaryTreeNode headNode;
public static void setNodeValue(int nodeValue) {
newNode = new BinaryTreeNode(nodeValue);
temp = headNode;
if(temp != null)
{ mapping(); }
else
{ headNode = newNode; }
}
private static void mapping() {
if(newNode.nodeValue < temp.nodeValue) { //Check value of new Node is smaller than Parent Node
if(temp.leftChildNode == null)
{ temp.leftChildNode = newNode; } //Assign new Node to leftChildNode of Parent Node
else
{
temp = temp.leftChildNode; //Parent Node is already having leftChildNode,so temp object reference variable is now pointing leftChildNode as Parent Node
mapping();
}
}
else
{
if(temp.rightChildNode == null)
{ temp.rightChildNode = newNode; } //Assign new Node to rightChildNode of Parent Node
else
{
temp = temp.rightChildNode; //Parent Node is already having rightChildNode,so temp object reference variable is now pointing rightChildNode as Parent Node
mapping();
}
}
}
public static void preorder() {
if(headNode != null) {
System.out.println("Preorder Traversal:");
headNode.preorder();
System.out.println("\n");
}
}
public static void inorder() {
if(headNode != null) {
System.out.println("Inorder Traversal:");
headNode.inorder();
System.out.println("\n");
}
}
public static void postorder() {
if(headNode != null) {
System.out.println("Postorder Traversal:");
headNode.postorder();
System.out.println("\n");
}
}
}
public class BinaryTree {
//Entry Point
public static void main(String[] args) {
ArrayList <Integer> intList = new ArrayList <Integer>(Arrays.asList(50,2,5,78,90,20,4,6,98));
Iterator<Integer> ptr = intList.iterator();
while(ptr.hasNext())
{ BinaryTreeCompute.setNodeValue(ptr.next()); }
BinaryTreeCompute.preorder();
BinaryTreeCompute.inorder();
BinaryTreeCompute.postorder();
}
}
Adding to the answer by #Maurice,
Your code has several problems:
You expect JAVA to be pass by reference, when it is pass by value. You should use the code given by Maurice instead.
You are comparing "keys", when you should compare values.
I suggest that you use following modified code :
public class BST
{
public Node root = null;
private class Node
{
private String key;
private int value;
private Node left;
private Node right;
public Node ()
{
}
public Node (String key, int value)
{
this.key = key;
this.value = value;
}
public String toString ()
{
return ("The key is: "+ this.key +" "+ this.value);
}
}
BST ()
{
}
public void put (String key, int value)
{
root = putInTree (root, key, value);
}
private Node putInTree (Node x, String key, int value)
{
Node newNode = new Node(key, value);
if (x == null)
{
x = newNode;
System.out.println("new node added");
System.out.println(x);
return newNode;
}
//int cmp = key.compareTo(x.key);
if (value < x.value)
x.left = putInTree(x.left, key, value);
else /*if (value >= x.value)*/
x.right = putInTree(x.right, key, value);
/*else
x.value = value;*/
return x;
}
public void inorder (Node x)
{
if (x != null)
{
inorder (x.left);
System.out.println(x.key);
inorder (x.right);
}
}
public static void main (String[] args)
{
BST bst = new BST();
bst.put("THE", 1);
bst.put("CAT", 2);
bst.put("HAT", 1);
bst.inorder(bst.root);
}
}

How to remove all evens in linked list java

How can I remove all the evens in a linked list in java? Other similar questions did not help me. I have a possible solution, but it seems too complicated. I'm not sure it even works.
public class Node {
public int value;
public Node next;
public Node (int val) {
value = val;
}
public Node removeNode (Node root) {
if (root == null || (root.next == null && isOdd(root.value))) {
return null;
}
Node deep = root;
while (deep.next != null) {
deep = deep.next;
}
if (isOdd(deep.value)) {
for (Node x = root; x != null; x = x.next) {
if (isOdd(x.next.value)) {
x.next = x.next.next;
}
}
} else {
for (Node x = root; x.next != null; x = x.next) {
if (isOdd(x.next.value)) {
x.next = x.next.next;
}
}
}
if (isOdd(root.value)) {
root = root.next;
}
return root;
}
public boolean isOdd (int val) {
return (val % 2 == 1);
}
How can I improve this solution?
First of all: The remove method should be in your LinkedList class, not in the Node class itself!
After that it's quite easy to just remove the the even ones:
public class LinkedList {
private Node root;
public void removeEvens() {
if (root == null) return;
// removing all even nodes after the root
Node prev = root;
while (prev.next != null) {
if (isEven(prev.next))
prev.next = prev.next.next; // next is even: delete it
else
prev = prev.next; // next is not even: proceed
}
// delete root if it's even
if (isEven(root))
root = root.next;
}
private boolean isEven(Node node) {
return node.value % 2 == 0;
}
}
You can add an element before root node. In that case you don't have to write code for check root node.
public class Node {
public int value;
public Node next;
public Node(){}
public Node (int val) {
value = val;
}
public static Node removeEvens (Node root) {
Node head = new Node();
head.next = root;
for (Node node = head; node.next!=null; node=node.next)
if (isEven(node.next))
node.next = node.next.next;
return head.next;
}
public static boolean isEven(Node node) {
return node.value % 2 == 0;
}
}

Java Generics: compareTo and “capture#-of ?”

I'm trying to write an implementation of a BinaryTree whose object can be of any type that implements Comparable. However, I realize that won't completely work. For example, A String and a Double wouldn't be able to be inserted into the same tree, even though they both implement Comparable.
So, I would like to know if it's possible to write the code such that the BinaryTree can be instantiated with any value whose type implements Comparable, but any ensuing elements added to the tree must all share the same supertype as the root's value.
Here's the code I have so far:
public class BinaryTree {
private Node root;
public BinaryTree() {
this.root = null;
}
public Node lookup(Comparable<Object> value) {
return lookup(this.root, value);
}
private Node lookup(Node node, Comparable<Object> value) {
Node match = null;
if (match != node) {
if (value == node.value) {
match = node;
} else if (value.compareTo(node.value) < 0) {
return lookup(node.left, value);
} else {
return lookup(node.right, value);
}
}
return match;
}
public Node lookupNonRecursively(Comparable<Object> value) {
return lookupNonRecursively(this.root, value);
}
private Node lookupNonRecursively(Node node, Comparable<Object> value) {
Node match = null;
if (match != node) {
if (value == node.value) {
match = node;
} else {
Node root = node;
boolean found = false;
while (!found && root != null) {
if (root.value.compareTo(value) < 0) {
if (root.left == null) {
root.left = match = new Node(value);
found = true;
} else {
root = root.left;
}
} else {
if (root.right == null) {
root.right = match = new Node(value);
found = true;
} else {
root = root.right;
}
}
}
}
}
return match;
}
public Node insert(Comparable<Object> value) {
return insert(this.root, value);
}
private Node insert(Node node, Comparable<Object> value) {
if (node == null) {
node = new Node(value);
} else {
if (node.value.compareTo(value) <= 0) {
insert(node.left, value);
} else {
insert(node.right, value);
}
}
return node;
}
public Node insertNonRecursively(Comparable<Object> value) {
return insertNonRecursively(this.root, value);
}
private Node insertNonRecursively(Node node, Comparable<Object> value) {
if (node == null) {
node = new Node(value);
} else {
Node root = node;
boolean inserted = false;
while (!inserted) {
if (node.value.compareTo(root.value) < 0) {
if (root.left == null) {
root.left = node = new Node(value);
inserted = true;
} else {
root = root.left;
}
} else {
if (root.right == null) {
root.right = node = new Node(value);
inserted = true;
} else {
root = root.right;
}
}
}
}
return node;
}
public static class Node {
private Node left;
private Node right;
private Comparable<Object> value;
public Node(Comparable<Object> value) {
this.left = null;
this.right = null;
this.value = value;
}
}
}
And as a test, this will throw the error, The method insert(Comparable<Object>) in the type BinaryTree is not applicable for the arguments (Integer), if I try to run code like the following:
BinaryTree tree = new BinaryTree();
tree.insert(new Integer(1));
You can see I've implemented some different BinaryTree methods for this class, but the same rules would need to apply: any value passed into lookup() or insert() would also need to share the root's supertype. I have a feeling this is where some variant of <T extends Comparable<? super T>> is going to come into play, but my mind is just not figuring this one out.
Any ideas for how I might accomplish this?
As noted by #jp-jee, here's my solution (also with logic and other bugs fixed from untested first attempt), which works beautifully:
public class BinaryTree<T extends Comparable<T>> {
private Node<T> root;
public BinaryTree() {
this.root = null;
}
public Node<T> lookup(T value) {
return lookup(this.root, value);
}
private Node<T> lookup(Node<T> node, T value) {
Node<T> match = null;
if (match != node) {
if (value.equals(node.value)) {
match = node;
} else if (value.compareTo(node.value) < 0) {
return lookup(node.left, value);
} else {
return lookup(node.right, value);
}
}
return match;
}
public Node<T> lookupNonRecursively(T value) {
return lookupNonRecursively(this.root, value);
}
private Node<T> lookupNonRecursively(Node<T> node, T value) {
Node<T> match = null;
if (match != node && value != null) {
if (value.equals(node.value)) {
match = node;
} else {
Node<T> searchRoot = node;
boolean found = false;
while (!found && searchRoot != null) {
if (value.equals(searchRoot.value)) {
match = searchRoot;
found = true;
} else if (value.compareTo(searchRoot.value) < 0) {
searchRoot = searchRoot.left;
} else {
searchRoot = searchRoot.right;
}
}
}
}
return match;
}
public void insert(T value) {
this.root = insert(this.root, value);
}
private Node<T> insert(Node<T> node, T value) {
if (node == null) {
node = new Node<T>(value);
} else {
if (value.compareTo(node.value) <= 0) {
node.left = insert(node.left, value);
} else {
node.right = insert(node.right, value);
}
}
return node;
}
public void insertNonRecursively(T value) {
this.root = insertNonRecursively(this.root, value);
}
private Node<T> insertNonRecursively(Node<T> node, T value) {
if (node == null) {
node = new Node<T>(value);
} else {
Node<T> runner = node;
boolean inserted = false;
while (!inserted) {
if (value.compareTo(runner.value) < 0) {
if (runner.left == null) {
runner.left = new Node<T>(value);
inserted = true;
} else {
runner = runner.left;
}
} else {
if (runner.right == null) {
runner.right = new Node<T>(value);
inserted = true;
} else {
runner = runner.right;
}
}
}
}
return node;
}
public static class Node<T extends Comparable<T>> {
private Node<T> left;
private Node<T> right;
private T value;
public Node(T value) {
this.left = null;
this.right = null;
this.value = value;
}
public Node<T> getLeft() {
return left;
}
public Node<T> getRight() {
return right;
}
public T getValue() {
return value;
}
}
}
Make your Binary Tree generic like
public class BinaryTree<T extends Comparable<T>>{
...
}
Whenever creating a BinaryTree instance, specify the containied type:
new BinaryTree<MyClass>();
Where MyClass must implement Comparable<MyClass>, i.e. be comparable to Objects of the same class.
Your methods would read as (example):
public Node lookup(T value) { ... }
The same applies for your Node class. Make it generic the same way.

Categories

Resources