How to create an assignFirst method with Nodes in a BinarySearchTree? - java

I have a binarySearch Tree and I want to create a method assignFirst.
This method should find the node in the tree with the smallest value and
update the tree's "first" attribute accordingly.
I have a lot of methods, but I don't want to include all of them in here, since I want to keep it short and simple.
Therefore, I will include the class and some features inside that class.
public class BinarySearchTree<E extends Comparable<E>>
{
private BSTNode<E> root; // root of overall tree
private int numElements;
private BSTNode<E> first;
// post: constructs an empty search tree
public BinarySearchTree()
{
this.root = null;
this.numElements = 0;
}
private void assignFirst()
{
if (root.left == null)
{
first.data = root.data;
}
else
{
first.data = root.left.data;
}
}
public class Iterator
{
private BSTNode<E> currentNode;
public Iterator()
{
currentNode = first;
}
public boolean hasNext()
{
return currentNode != null;
}
public E next()
{
E value = currentNode.data;
currentNode = currentNode.next;
return value;
}
}
private static class BSTNode<E>
{
public E data;
public BSTNode<E> left;
public BSTNode<E> right;
public BSTNode<E> parent;
public BSTNode<E> next;
public BSTNode(E data)
{
this(data, null, null, null, null);
}
public BSTNode(E data, BSTNode<E> left, BSTNode<E> right, BSTNode<E> parent, BSTNode<E> next)
{
this.data = data;
this.left = left;
this.right = right;
this.parent = parent;
this.next = next;
}
}
}
I updated my method look like this. I'm still uncertain if this is the correct way of doing it.
private void assignFirst()
{
if (first.left != null)
{
first = first.left;
}
else
{
first = root;
}
}

I figured it out. I wrote it in like this.
private void assignFirst()
{
BSTNode<E> node = root;
while(node.left != null)
{
node = node.left;
}
first = node;
}

Related

How to make linked stack generics?

I am doing lab, I viewed a lot of Java Generics example, but I cannot understand it. My goal is achieving Linked Stack. It have 2 files: IntStack.java and IntNode.java. The part of these code is:
public class IntNode {
private int element = 0;
private IntNode next = null;
public IntNode(final int data, final IntNode next) {
this.element = data;
this.next = next;
}
public class IntStack {
private IntNode top = null;
public boolean isEmpty() {
return this.top == null;
}
How to convert them to generics type? I know it should use <T>,and I write these code, it is correct or not?
public class Node<T> {
private T element;
private Node<T> next = null;
public Node(final T data,final Node next) {
this.element = data;
this.next = next;
}
}
You are close. The Node parameter of the Node constructor should also be parameterized:
public class Node<T> {
private T element;
private Node<T> next = null;
public Node(final T data,final Node<T> next) {
this.element = data;
this.next = next;
}
}

Method that inserts a node at the end of a list, using the tail reference Java

I am currently trying to write a method insertEnd that inserts a node at the end of a list, using the tail reference. As I am still learning about it, I do not know how I can approach this. If you have any suggestions or solutions, please could you let me know as it will help me greatly.
package lib;
public class LinkedList {
private Node head;
private Node tail;
public LinkedList(Node h){
head = h;
}
public Node getHead(){
return head;
}
public Node getTail(){
return tail;
}
public void setHead(Node n){
head = n;
}
public void insertEnd(Node newNode){
}
public class ListApp {
public static void main(String[] args){
Node n4 = new Node("green", null);
Node n3 = new Node("orange", n4);
Node n2 = new Node("blue", n3);
Node n1 = new Node("red", n2);
package lib;
public class Node {
private String item;
private Node nextItem;
public Node(String str, Node n){
item = str;
nextItem = n;
}
public String getItem(){
return item;
}
public void setItem(String str){
item = str;
}
public Node next(){
return nextItem;
}
public void setNext(Node n){
nextItem = n;
}
public String getHead(){
return item;
}
}
Here's one possible solution:
public void insertEnd(Node newNode){
newNode.setNext(null);
if (tail == null) {
tail = newNode;
head = newNode;
} else {
tail.setNext(newNode);
tail = newNode;
}
}
Assuming your Node, holds a reference to next,
public class Node<E>{
private E ele;
private Node<E> next;
P.S: Java already does have the LinkedList implemented for our convenience.

Null pointer working with nodes

I have a problem comparing nodes (of binary tree) to null
Say node is a new root of the tree, with key=5.
In the constructor I set node's left, right and parent to be null
(fields: private Node left=null,right=null,parent=null;
constructor: this.key=key)
When I print the node's left child (same as for the right and the parent) I get null as expected.
However, node.left==null (or node.getLeft()==null) gets me false. why?
Here's the code of node class:
public class Node{
private Node parent = null, left = null, right = null;
private int key;
public Node(int key) {
this.key = key;
}
public Node getParent() {
return parent;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
public void setParent(Node parent) {
this.parent = parent;
}
public void setLeft(Node child) {
this.left = child;
}
public void setRight(Node child) {
this.right = child;
}
public void setKey(int key) {
this.key = key;
}
}

Method getInfo() not found?

The problem comes in the toString method of the following code:
import java.util.*;
public class LinkedDeque<T> // implements Deque<T>
{
private Node head;
private Node tail;
private int size;
private class Node // Node class
{
T info;
Node next;
Node prev;
private Node (T info, Node prev, Node next)
{
this.info = info;
this.prev = prev;
this.next = next;
}
private T getInfo()
{
return this.info;
}
private Node getNext()
{
return this.next;
}
private Node getPrev()
{
return this.prev;
}
}
public LinkedDeque ()
{
this.head = null;
this.tail = null;
this.size = 0;
}
public static void main()
{
}
public int size ()
{
Node count = head;
while(count.getNext() != null)
{
size++;
count = count.getNext();
}
return size;
}
public String toString()
{
return this.getInfo();
}
public boolean isEmpty()
{
return size() == 0;
}
}
My compiler keeps giving me an error saying that the getInfo method is missing. Any help would be appreciated! Initially, I thought this was due to the fact that the Node class was private, but the Node getNext() method works fine in the method size().
The toString method is a member of LinkedDeque not Node. LinkedDeque does not have a getInfo method.
Not sure what it is you were trying to achieve, but you may consider moving that method into the Node class...

Raw type Binary Search Tree issue in Java SE 1.7

I'm trying to build an raw type BST, with Comparable<T>. The thing is somehow my declarations do something wrong, because I use in Node class type Comparable<type> and in BST class it errors with
The method setParent(Node<Comparable<Comparable<type>>>) in the type
Node<Comparable<type>> is not applicable for the arguments (Node<Comparable<type>>)
BinarySearchTree.java /lab2/src line 22 Java Problem
Node.java:
public class Node <type> {
private Comparable<type> key;
private Node <Comparable<type>> parent;
private Node <Comparable<type>> leftChild;
private Node <Comparable<type>> rightChild;
public Node(Comparable<type> key, Node <Comparable<type>> leftChild, Node <Comparable<type>> rightChild) {
this.setKey(key);
this.setLeftChild(leftChild);
this.setRightChild(rightChild);
}
public void setKey(Comparable<type> key) {
this.key = key;
}
public Comparable<type> getKey() {
return key;
}
public void setParent(Node<Comparable<type>> y) {
this.parent = y;
}
public Node <Comparable<type>> getParent() {
return parent;
}
public void setLeftChild(Node <Comparable<type>> leftChild) {
this.leftChild = leftChild;
}
public Node <Comparable<type>> getLeftChild() {
return leftChild;
}
public void setRightChild(Node <Comparable<type>> rightChild) {
this.rightChild = rightChild;
}
public Node <Comparable<type>> getRightChild() {
return rightChild;
}
}
BinarySearchTree.java:
import java.util.Iterator;
public class BinarySearchTree<type> implements SortedSet<type> {
private Node <Comparable<type>> root;
public void insert(Node <Comparable<type>> z) {
Node <Comparable<type>> y = null;
Node <Comparable<type>> x = root;
while (x != null) {
y = x;
if (z.getKey() < x.getKey()) { // ERROR '<' is undefined for type...
x = x.getLeftChild(); // PARAM TYPE ERROR
} else {
x = x.getRightChild(); // PARAM TYPE ERROR
}
}
z.setParent(y);
if (y == null) {
root = z;
} else if (z.getKey() < y.getKey()) {
y.setLeftChild(z);
} else {
y.setRightChild(z);
}
}
Consider to refactoring to the following code
import java.util.SortedSet;
public abstract class BinarySearchTree<T extends Comparable<T>> implements SortedSet<T> {
private Node<T> root;
class Node<T extends Comparable<T>> {
private T key;
private Node<T> parent;
private Node<T> leftChild;
private Node<T> rightChild;
public Node(T key, Node<T> leftChild, Node<T> rightChild) {
this.setKey(key);
this.setLeftChild(leftChild);
this.setRightChild(rightChild);
}
public void setKey(T key) {
this.key = key;
}
public T getKey() {
return key;
}
public void setParent(Node<T> y) {
this.parent = y;
}
public Node <T> getParent() {
return parent;
}
public void setLeftChild(Node <T> leftChild) {
this.leftChild = leftChild;
}
public Node <T> getLeftChild() {
return leftChild;
}
public void setRightChild(Node <T> rightChild) {
this.rightChild = rightChild;
}
public Node <T> getRightChild() {
return rightChild;
}
}
public void insert(Node<T> z) {
Node<T> y = null;
Node<T> x = root;
while (x != null) {
y = x;
if (z.getKey().compareTo(x.getKey()) < 0) {
x = x.getLeftChild();
} else {
x = x.getRightChild();
}
}
z.setParent(y);
if (y == null) {
root = z;
} else if (z.getKey().compareTo((T) y.getKey()) <0) {
y.setLeftChild(z);
} else {
y.setRightChild(z);
}
}
}

Categories