Not printing 1st Node entered in stack(toString) - java

First off, the code is long, but I am only concerned about the toString method. I pasted everything in case its relevant.
The code is supposed to check whether or not the element entered is less than the element at the top of the stack. I have that part figured out. When I call my toString method to print the saved input, it doesn't print the first node added to the stack. However, it prints all remaining inputs. A example would be if a user enters the following:
testing
test
te
t
What its currently outputing:
t te test
What it needs to output:
t te test testing
Main:
import java.util.*;
public class Stack {
public static void main(String args[]) {
int loopInt = 1;
PyramidStack<String> stringStack = new PyramidStack<String>();
PyramidStack<Integer> intStack = new PyramidStack<Integer>();
System.out
.println("This program will save some of the strings you enter. ");
System.out
.println("Can you predict which ones will be saved? (Enter nothing to quit.)");
Scanner sc = new Scanner(System.in); // Opens Scanner for keyboard input
try {
do {
System.out.print("Enter a String: ");
String input = sc.nextLine();
if (input.length() > 0) {
if (stringStack.size() == 0) {
intStack.push(input.length());
stringStack.push(input);
System.out.println("String Saved");
}
else if (input.length() < intStack.peek()) {
stringStack.push(input);
intStack.push(input.length());
System.out.println("String Saved");
}
else {
System.out.println("String NOT saved. Already saved "
+ intStack.countBefore(input.length())
+ " strings that should come before this one.");
}
} else {
System.out.println();
System.out.println(stringStack.toString());
System.out.println(intStack.toString());
loopInt--;
sc.close();
}
} while (loopInt > 0);
} catch (NullPointerException e) {
System.out.println("No strings have been entered. Ending the program.");
}
}
}
PyramidStack Method:
import java.util.*;
import java.lang.Comparable;
public class PyramidStack<E extends Comparable<E>> extends Stack<E> {
#Override
public void push(E item) throws IllegalArgumentException {
if (super.size == 0) {
super.push(item);
}
else if(item.compareTo(super.peek()) <= 0) {
super.push(item);
}
else {
System.out.println("String NOT saved. " + countBefore(item) + " strings that should come before this one.");
}
}
#Override
public String toString() {
Node<E> node;
node = this.top;
String s = "";
while(node.getNext() != null){
s += node.getData() + " ";
node = node.getNext();
}
return s;
}
public int countBefore(E item) {
Node<E> node;
node = this.top;
int i = 0;
while(node.getNext() != null){
if(item.compareTo(super.peek()) <= 0) {
node = node.getNext();
}
else{
i++;
node = node.getNext();
}
}
return i;
}
}
Stack method and Node Method below in comments.

while(node.getNext() != null){
s += node.getData() + " ";
node = node.getNext();
}
This is where you are missing to print the last data.. you are checking node.getnext() is null.. so it skips the last object and doesn't print that.. your condition should be
node != null
OR
After the loop end have this
s += node.getData() -- this will not give null pointer exception as node is not null.. as it did check node.getnext()
Hope it helps

Related

Implementing a Circular Single Linked List using a scanner

This is a given activity where we should create a method for inserting a node and it uses a scanner for its input. So far, I can input 3 objects from the list but here comes the problem when I try to add another one:
It goes something like this: 1,2,3 and when I try to add another it goes to this 1,2,4 but want I want is this 1,2,3,4.
I deeply appreciate your help in advance.
Here is the main method:
import java.util.Scanner;
public class MySinglyLinkedCircularListMain {
public static void main(String[] args) throws ListOverflowException {
Scanner keyboard = new Scanner(System.in);
MySinglyLinkedCircularList<Node> singlyLinkedCircularList = new MySinglyLinkedCircularList<>();
while (true) {
System.out.println("+---------------------------------------------------+");
System.out.println("| Select the Number to be executed: |\n" +
"| 1) Insert an element |\n" +
"| 2) Delete an element from the list |\n" +
"| 3) Get an element from the list |\n" +
"| 4) Search an element in the list |\n" +
"| 5) Number of elements in the list |\n" +
"| 6) Show the elements in the list |");
System.out.print("+---------------------------------------------------+ \n");
System.out.print("Input your choice: ");
int intInput = keyboard.nextInt();
if (intInput == 1) {
singlyLinkedCircularList.insert(new Node(singlyLinkedCircularList));
} else if (intInput == 2) {
singlyLinkedCircularList.delete(new Node(singlyLinkedCircularList));
} else if (intInput == 3) {
singlyLinkedCircularList.getElement(new Node(singlyLinkedCircularList));
} else if (intInput == 4) {
singlyLinkedCircularList.search(new Node(singlyLinkedCircularList));
} else if (intInput == 5){
System.out.println("The current capacity of the single circular linked list is "
+ singlyLinkedCircularList.getSize());
} else if (intInput == 6) {
singlyLinkedCircularList.showAllElements();
System.out.println();
}
}
}
}
Here is the node class
public class Node<T> {
T data;
Node<T> next;
public Node(T data) {
this.data = data;
next = null;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
public void setNext(Node<T> node) {
next = node;
}
public Node<T> getNext() {
return next;
}
}
Here's the class for the insert method
import java.util.NoSuchElementException;
import java.util.Scanner;
public class MySinglyLinkedCircularList<E> implements MyList<E> {
Scanner keyboard = new Scanner(System.in);
int size;
Node<E> startNode;
Node<E> endNode;
public MySinglyLinkedCircularList() {
size = 0;
startNode = endNode = null;
}
public int getSize() {
return size;
}
public void insert(E data) throws ListOverflowException {
System.out.print("Input the element you want: ");
data = (E) keyboard.next();
Node<E> newNode = new Node(data);
if (startNode == null) {
startNode = endNode = newNode;
startNode.next = startNode;
size++;
System.out.println("Element " + startNode.getData() + " has been stored in position "
+ getSize() + " and is now referenced itself");
} else {
Node<E> addNode = endNode;
for (int i = 0; i < getSize(); i++) {
addNode = addNode.getNext();
}
addNode.next = newNode;
newNode.next = startNode;
size++;
}
}
Here's the show elements method
public void showAllElements() {
Node<E> showNode = startNode;
int i = 0;
System.out.print("Here are the current elements: ");
while (i<getSize()) {
System.out.print(showNode.getData() + " ");
showNode = showNode.getNext();
i++;
}
System.out.print(showNode.getData());
}
Here's the interface
public interface MyList<E> {
public int getSize();
public void insert(E data) throws ListOverflowException;
public E getElement(E data) throws NoSuchElementException;
public boolean delete(E data); // returns false if the data is not deleted in the list
public boolean search(E data);
public void showAllElements();
}
This is problematic:
public void insert(E data) throws ListOverflowException {
System.out.print("Input the element you want: ");
data = (E) keyboard.next(); // ***** here *****
Your method accepts an E data parameter, insert(E data) and yet promptly discards any result held by the parameter in the second line of the method, data = (E) keyboard.next();, replacing it with Scanner input that shouldn't even be there. The MySinglyLinkedCircularList class should not have a Scanner object, nor should it take user input, but rather should handle the circular list's logic, and that's it. It shouldn't even have println statements, except perhaps temporary println's that you may usind during creation for debugging purposes only. Keep all UI code (user interface code -- Scanner input and println statements) together in the UI class.
Note, that I don't know if this is the cause of your full error, and to check for that will require more intense debugging, debugging that I urge you to do more of yourself, since it is both your responsibility to debug first, and it is a necessary and useful skill that only gets better with use. If you're not sure how to go about doing this, then please check out How to debug small programs. It won't solve your direct problem, but it will give you steps that you can follow that should help you solve it yourself, or even if that is not successful, then at least help you to better isolate your problem so that your question can be more focused and easier to answer.

Tree iterator in Java

I have implemented a code which adds elements in a tree and prints them in increasing order. However my aim is to learn iterators and want to replace the inOrder() function with an iterator function. How can I do this?
import java.util.InputMismatchException;
import java.util.Scanner;
import javax.xml.soap.Node;
class Tree
{
public final int mVal;
public Tree mLeft;
public Tree mRight;
public Node next;
public Tree(int val)
{
mVal = val;
}
public void add(int val)
{
if (val < mVal)
{
if (mLeft == null)
mLeft = new Tree(val);
else
mLeft.add(val);
}
else
{
if (val > mVal)
{
if (mRight == null)
mRight = new Tree(val);
else
mRight.add(val);
}
}
}
public String inOrder()
{
return ((mLeft == null) ? "" : mLeft.inOrder())
+ mVal + " "
+ ((mRight == null) ? "" : mRight.inOrder());
}
public static void main(String[] args)
{
Tree t = new Tree(8);
Scanner scanner = new Scanner(System.in);
boolean continueLoop = true; // determines if more input is needed
for (int i = 1; i < 9; ++i)
{
try // read two numbers and calculate quotient
{
System.out.print("Please enter a random integer : ");
int stackInt = scanner.nextInt();
t.add(Integer.valueOf(stackInt));
} // end try
catch (InputMismatchException inputMismatchException){
System.err.printf("\nException: %s\n", inputMismatchException);
scanner.nextLine(); //discard input so user can try again
System.out.println("You must enter integers. Please try again.\n");
} // end catch
}
System.out.println("Values in order = "+ t.inOrder());
}
}
look at this picture
First Step: if node has a left child, visit left child and do the first step with the child
Second Step: node has no left child (or we visited the left child already), add it to the inorder list
Third Step: first step with right child
i didnt test it
#Override
public String toString() {
return String.valueOf(mVal);
}
public String inOrder(Tree root) {
List<Tree> inOrder = new ArrayList<>();
inOrderRecursively(root, inOrder);
return inOrder.toString();
}
private void inOrderRecursively(Tree Node, List<Tree> inOrder) {
if (Node.mLeft != null) {
inOrderIt(Node.mLeft, inOrder);
}
inOrder.add(Node);
if (Node.mRight != null) {
inOrderIt(Node.mRight, inOrder);
}
}
greetings

Java Object [] sorting

I am trying to sort an array of created objects each time an object is added. I wrote a compareTo method, and it is printing out each line, but throws an exception when I try to sort it. I am initializing an Element[] of 99 elements(maxLen), then using topIndex as a counter to find the "real" length. A scanner is used to get user input to create the Element. EDIT-- I added the full code.
import java.util.Arrays;
import java.util.Scanner;
public class mainmenu {
static Element[] data = new Element[100];
static int maxLen= 99;
static int topIndex= -1;
#SuppressWarnings("rawtypes")
public static void main(String[] args){
menu();
}
public static void menu(){
boolean leave = true;
Scanner in = new Scanner(System.in);
while(leave){
//loop. unless value is 1-7, keeps recycling
System.out.println("Please select a menu option: ");
System.out.println("'1'- enque ");
System.out.println("'2'- deque");
System.out.println("'3'- peek");
System.out.println("'4'- display");
System.out.println("'5' empty queue");
System.out.println("'6'- check if the queue is empty");
System.out.println("'7' to exit the program");
String select = in.next();
if ((select.equals("1") || select.equals("2")|| select.equals("3")|| select.equals("4")||
select.equals("5")|| select.equals("6")|| select.equals("7")))
leave = callMethods(select );
else{
System.out.println("Please enter a valid menu option.");
}
}
}
public static boolean callMethods(String select )
{
boolean leave= true;
int sel = Integer.parseInt(select);
switch(sel){
case 1:
enqueue();
break;
case 2:
dequeue();
break;
case 3:
peek();
break;
case 4:
display();
break;
case 5:
empty();
break;
case 6:
if( isEmpty()){
System.out.println("The structure is empty");
}
else{
System.out.println("The structure is not empty.");
}
break;
case 7:
System.out.println("Bye!");
leave = false;
}
return leave;
}
public static void enqueue(){
boolean isInt = false;
String st = null;
int index = 0;
Scanner in = new Scanner(System.in);
while(!isInt){ //loop continues until input is an integer type
System.out.println("Please enter a priority level for this element");
st = in.next();
if (isInteger(st) == true){// calls method to check if value is integer
index = Integer.parseInt(st);//parses the string into a integer
isInt = true;
}
else //if value isnt integer, try again
System.out.println("Invalid Input.");
}
System.out.println("Please enter the string of information.");
String info = in.next();
Element e = new Element(info, index);
if (topIndex == maxLen){
System.out.println("Data Structure is full.");
}
else if (isEmpty()){
data[0] = e;
topIndex++;
}
else {
topIndex++;
data[topIndex]=e;
System.out.println("Added "+ e.getPriority() + " at "+ e.getInfo());
System.out.println(topIndex);
Arrays.sort(data);
}
}
private static boolean isInteger(String s) {
//checks to see if string can be parsed as an integer.
try{
Integer.parseInt(s);
return true;
}
catch( Exception e ){
return false;
}
}
public static void dequeue(){
if(isEmpty()){
System.out.println("The structure is empty.");
}
else{
Element e = data[topIndex];
System.out.println("Removing Element: " + e.getInfo()+ " Priority Level: " + e.getPriority());
--topIndex;
}
}
public static void peek(){
if (isEmpty()){
System.out.println("The structure is empty.");
}
else {
Element e = (data[0]);
System.out.println("Element: " + e.getInfo()+ " Priority Level: " + e.getPriority());
}
}
public static void display(){
System.out.println("topIndex " + topIndex);
if (topIndex==-1){
System.out.println("The structure is empty.");
}
else {
for(int i = 0; i <= topIndex; i++){
System.out.println("Index: " +i);
Element e = data[i];
System.out.println("Element: " + e.getInfo()+ " Priority Level: " + e.getPriority());
}
}
}
public static void empty(){
System.out.println("Erasing data.");
topIndex=-1;
}
public static boolean isEmpty(){
return (topIndex==-1);
}
}
And the Element class:
public class Element implements Comparable<Element> {
private String info;
private int index;
public Element ( String st, int ind) {
super();
this.info = st;
this.index= ind;
}
public String getInfo() {
return info;
}
public void setInfo(String st) {
this.info = st;
}
public int getPriority(){
return index;
}
public void setPriority(int pr){
this.index = pr;
}
public int compareTo(Element e) {
System.out.println(e.index+ ""+ e.info);
// if (!(e instanceof Element))
// throw new ClassCastException("An Element object expected.");
int ePriority = e.getPriority();
System.out.println(ePriority);
System.out.println(this.index);
int balls= this.index - ePriority;
System.out.println(balls);
return balls;
}
}
Arrays.sort requires all of the array elements to be non-null. You want to sort only the non-null part, so replace Array.sort(data) with Arrays.sort(data, 0, topIndex + 1).
Arrays.sort(Object[], int, int)
Do not modify compareTo to allow a null argument as others have suggested, because the contract of Comparable dictates that your implementation should throw NullPointerException.
Comparable.compareTo(T)
You have given Element[] array data to Array.sort() method which has size 100, but all of the element of data array are not initialized. Hence a call such as e.getPriority(); will result in NullPointerException as e is null. Initialize all of the element of data array first.
for(int i=0 ; i< data.length; i++)
e = new Element(info, index); // replace with relevant info and index of your contest
Use Arrays.sort(Object[] array, int fromIndexInclusive, int toIndexExclusive) to sort parts of an array if needed:
Arrays.sort(data, 0, topIndex+1);
From your error description I would guess the Element object is getting null.
As per your comment
It will throw the Element object expected exception I wrote in. If I comment out the throw declaration in the compareTo method, I get a null pointer exception.
if (!(e instanceof Element))
throw new ClassCastException("An Element object expected.");
The above statement will throw the exception only the object e is NULL
When doing a comparison, you're deciding if one object outranks another by some arbitrary condition.
I notice that you don't check if the object you're comparing against is null - you should do that.
public int compareTo(Element e) {
if(e == null) {
return this.index;
} else {
// rest of your logic goes here
}
}

BinaryTree implementation in java

I have this code for BinaryTree creation and traversal
class Node
{
Integer data;
Node left;
Node right;
Node()
{
data = null;
left = null;
right = null;
}
}
class BinaryTree
{
Node head;
Scanner input = new Scanner(System.in);
BinaryTree()
{
head = null;
}
public void createNode(Node temp, Integer value)
{
Node newnode= new Node();
value = getData();
newnode.data = value;
temp = newnode;
if(head==null)
{
head = temp;
}
System.out.println("If left child exits for ("+value+") enter y else n");
if(input.next().charAt(0)=='y')
{
createNode(temp.left, value);
}
System.out.println("If right child exits for ("+value+") enter y else n");
if(input.next().charAt(0)=='y')
{
createNode(temp.right, value);
}
}
public Integer getData()
{
out.println("Enter the value to insert:");
return (Integer)input.nextInt();
}
public void print()
{
inorder(head);
}
public void inorder(Node node)
{
if(node!=null)
{
inorder(node.left);
System.out.println(node.data);
inorder(node.right);
}
else
return;
}
}
class BinaryTreeWorker
{
static BinaryTree treeObj = null;
static Scanner input = new Scanner(System.in);
public static void displaymenu()
{
int choice;
do{
out.print("\n Basic operations on a tree:");
out.print("\n 1. Create tree \n 2. Insert \n 3. Search value \n 4. print list\n Else. Exit \n Choice:");
choice = input.nextInt();
switch(choice)
{
case 1:
treeObj = createBTree();
break;
case 2:
treeObj.createNode(null, null);
break;
case 3:
//searchnode();
break;
case 4:
treeObj.print();
break;
default:
return;
}
}while(true);
}
public static BinaryTree createBTree()
{
return new BinaryTree();
}
public static void main(String[] args)
{
displaymenu();
}
}
It compiles and runs. But I think there is something wrong with the inorder traversal.
I created the below tree,
2
1 3
But it prints only 2.
I have tried solving the problem your way and I have pasted the solution below.. Though I haven't tested it thoroughly so it might fail in some edge condition.. But I have tested it for one case. Kindly let me know if it fails in some scenario. I would appreciate others help in making this answer better. I agree that this solution is not the most ideal way to code a Binary Tree but it wont hurt this way if some one is just practicing..
import java.util.Scanner;
class Node
{
Integer data;
Node left;
Node right;
Node()
{
data = null;
left = null;
right = null;
}
}
class BinaryTree
{
Node head;
Scanner input = new Scanner(System.in);
BinaryTree()
{
head = null;
}
public void createNode(Node temp,Node newnode)
{
if(head==null)
{
System.out.println("No value exist in tree, the value just entered is set to Root");
head = newnode;
return;
}
if(temp==null)
temp = head;
System.out.println("where you want to insert this value, l for left of ("+temp.data+") ,r for right of ("+temp.data+")");
char inputValue=input.next().charAt(0);
if(inputValue=='l'){
if(temp.left==null)
{
temp.left=newnode;
System.out.println("value got successfully added to left of ("+temp.data+")");
return;
}else {
System.out.println("value left to ("+temp.data+") is occupied 1by ("+temp.left.data+")");
createNode(temp.left,newnode);
}
}
else if(inputValue=='r')
{
if(temp.right==null)
{
temp.right=newnode;
System.out.println("value got successfully added to right of ("+temp.data+")");
return;
}else {
System.out.println("value right to ("+temp.data+") is occupied by ("+temp.right.data+")");
createNode(temp.right,newnode);
}
}else{
System.out.println("incorrect input plz try again , correctly");
return;
}
}
public Node generateTree(){
int [] a = new int[10];
int index = 0;
while(index<a.length){
a[index]=getData();
index++;
}
if(a.length==0 ){
return null;
}
Node newnode= new Node();
/*newnode.left=null;
newnode.right=null;*/
return generateTreeWithArray(newnode,a,0);
}
public Node generateTreeWithArray(Node head,int [] a,int index){
if(index >= a.length)
return null;
System.out.println("at index "+index+" value is "+a[index]);
if(head==null)
head= new Node();
head.data = a[index];
head.left=generateTreeWithArray(head.left,a,index*2+1);
head.right=generateTreeWithArray(head.right,a,index*2+2);
return head;
}
public Integer getData()
{
System.out.println("Enter the value to insert:");
return (Integer)input.nextInt();
}
public void print()
{
inorder(head);
}
public void inorder(Node node)
{
if(node!=null)
{
inorder(node.left);
System.out.println(node.data);
inorder(node.right);
}
else
return;
}
}
public class BinaryTreeWorker
{
static BinaryTree treeObj = null;
static Scanner input = new Scanner(System.in);
public static void displaymenu()
{
int choice;
do{
System.out.print("\n Basic operations on a tree:");
System.out.print("\n 1. Create tree \n 2. Insert \n 3. Search value \n 4. print list\n 5. generate a tree \n Else. Exit \n Choice:");
choice = input.nextInt();
switch(choice)
{
case 1:
treeObj = createBTree();
break;
case 2:
Node newnode= new Node();
newnode.data = getData();
newnode.left=null;
newnode.right=null;
treeObj.createNode(treeObj.head,newnode);
break;
case 3:
//searchnode();
break;
case 4:
System.out.println("inorder traversal of list gives follows");
treeObj.print();
break;
case 5:
Node tempHead = treeObj.generateTree();
System.out.println("inorder traversal of list with head = ("+tempHead.data+")gives follows");
treeObj.inorder(tempHead);
break;
default:
return;
}
}while(true);
}
public static Integer getData()
{
System.out.println("Enter the value to insert:");
return (Integer)input.nextInt();
}
public static BinaryTree createBTree()
{
return new BinaryTree();
}
public static void main(String[] args)
{
displaymenu();
}
}
[Update] : Updated the code to generate a binary tree using an array. This will involve less user interaction.
Best way to implement Binary Tree in Java with all the traverse types and test cases as below
package com.nitin.tree;
public class Tree
{
private Node parent;
private int data;
private int size = 0;
public Tree() {
parent = new Node(data);
}
public void add(int data) {
if (size == 0) {
parent.data = data;
size++;
} else {
add(parent, new Node(data));
}
}
private void add(Node root, Node newNode) {
if (root == null) {
return;
}
if (newNode.data < root.data) {
if (root.left == null) {
root.left = newNode;
size++;
} else {
add(root.left, newNode);
}
} else {
if (root.right == null) {
root.right = newNode;
size++;
} else {
add(root.right, newNode);
}
}
}
public int getLow() {
Node current = parent;
while (current.left != null) {
current = current.left;
}
return current.data;
}
public int getHigh() {
Node current = parent;
while (current.right != null) {
current = current.right;
}
return current.data;
}
private void in(Node node) {
if (node != null) {
in(node.left);
System.out.print(node.data + " ");
in(node.right);
}
}
private void pre(Node node) {
if (node != null) {
System.out.print(node.data + " ");
pre(node.left);
pre(node.right);
}
}
private void post(Node node) {
if (node != null) {
post(node.left);
post(node.right);
System.out.print(node.data + " ");
}
}
public void preorder() {
System.out.print("Preorder Traversal->");
pre(parent);
System.out.println();
}
public void postorder() {
System.out.print("Postorder Traversal->");
post(parent);
System.out.println();
}
public void inorder() {
System.out.print("Inorder Traversal->");
in(parent);
System.out.println();
}
private class Node {
Node left;
Node right;
int data;
public Node(int data) {
this.data = data;
}
}
public String toString() {
Node current = parent;
System.out.print("Traverse From Left ");
while (current.left != null && current.right != null) {
System.out.print(current.data + "->[" + current.left.data + " " + current.right.data + "] ");
current = current.left;
}
System.out.println();
System.out.print("Traverse From Right ");
current = parent;
while (current.left != null && current.right != null) {
System.out.print(current.data + "->[" + current.left.data + " " + current.right.data + "] ");
current = current.right;
}
return "";
}
public static void main(String af[]) {
Tree t = new Tree();
t.add(40);
t.add(25);
t.add(78);
t.add(10);
t.add(32);
t.add(50);
t.add(93);
t.add(3);
t.add(17);
t.add(30);
t.add(38);
System.out.println(t.getLow());
System.out.println(t.getHigh());
System.out.println("Size-" + t.size);
System.out.println(t);
t.inorder();
t.preorder();
t.postorder();
}
}
Your problem is in public void createNodes(Node temp, T data) function. You pass in a parameter the same name as the class variable temp. First of all I don't think you need the class variable by itself. Second of all assigning to temp in this method has only local effect - you loose the information in the temp parameter, but setting temp, will not infuence its value in the called method. I suggest you rewrite the method so that it returns the pointer to the newly created node and assign this pointer to the left and right of the local temp. That way the changes will propagate out.
another type of outputting the tree:
public void inorder()
{
inorder(root);
}
protected void visit(BSTNode<T> p)
{
System.out.println("Node: " + p.el + "Left Side:" + (p.left!=null?p.left.el:"null") +
"Right Side:" + (p.right!=null?p.right.el:"null"));
}
I've changed the BinaryTree Class as below. See the change on the the createNode method in particular.
The problem, as mentioned in the post before this, is that your reference doesn't persist when it is passed as an argument to the createNode method. That change is only local. You need to return an explicit Node reference in the method itself as you're creating the node.
public Node createNode()
{
Integer value = getData();
Node temp = new Node(value);
if(head==null)
{
head = temp;
}
System.out.println("Do you want to add left branch on node("+value+")? Enter y/n");
if(input.next().charAt(0)=='y')
{
temp.left=createNode();
}
System.out.println("Do you want to add right branch on node("+value+")? Enter y/n");
if(input.next().charAt(0)=='y')
{
temp.right=createNode();
}
return temp;
}
Here is the resulting output:
Basic operations on a tree:
1. Create tree
2. Insert
3. Search value
4. print list
Else. Exit
Choice:1
Basic operations on a tree:
1. Create tree
2. Insert
3. Search value
4. print list
Else. Exit
Choice:2
Enter the value to insert:
10
Do you want to add left branch on node(10)? Enter y/n
y
Enter the value to insert:
20
Do you want to add left branch on node(20)? Enter y/n
n
Do you want to add right branch on node(20)? Enter y/n
n
Do you want to add right branch on node(10)? Enter y/n
y
Enter the value to insert:
30
Do you want to add left branch on node(30)? Enter y/n
n
Do you want to add right branch on node(30)? Enter y/n
n
Basic operations on a tree:
1. Create tree
2. Insert
3. Search value
4. print list
Else. Exit
Choice:4
20
10
30
I hope this will be of some help to someone later (even if this is 3 years late..). I just started learning about Binary Trees today myself. I'm actually planning on using this as a base to doing more involved tasks!
I changed the createNode method so that it works:
public Node createNode(Node temp, Integer value)
{
Node newnode = new Node();
value = getData();
newnode.data = value;
temp = newnode;
if(head == null)
{
head = temp;
}
System.out.println("If left child exits for ("+value+") enter y else n");
if(input.next().charAt(0) == 'y')
{
newnode.left = createNode(newnode.left, value);
}
System.out.println("If right child exits for ("+value+") enter y else n");
if(input.next().charAt(0) == 'y')
{
newnode.right = createNode(newnode.right, value);
}
return newnode;
}

AVL Tree Traversal, Search problems

I am having a few problems in my AVL tree implementation.. The code for all the rotations and the adding all seem to be correct and I dry-run the program to thoroughly check that it is running logically correct. I seem to be having a problem in my tree traversal (in-order) because it only outputs a few integers from the supposed 100. Also the search is always failing, regardless of what I enter. I cannot seem to grasp what is going on but I suspect that it has something to do with a few null pointers. Below is the code for the AVL tree, I am wondering if there's any incorrect code in the AddNode method or the rotation methods but they seem to be fine.. The classes are Node class, AVL class and AVL tree class which is the main class.
Node class
private int data;
private Node left;
private Node right;
private int height;
public Node(int m) {
data = m;
left = null;
right = null;
height = 0;
}
public void setToleft(Node newleft) {
left = newleft;
}
public Node getleftNode() {
return left;
}
public void setToright(Node newright) {
right = newright;
}
public Node getrightNode() {
return right;
}
public int getData() {
return data;
}
public int getHeight(){
return height;
}
public void setHeight(int height){
this.height = height;
}
AVL class
public Node root;
public AVL(int root) {
this.root = new Node(root); // since root presently has no left or right children, height is currently 0
}
public int Height(Node n) {
if (n == null) { //basis step
return -1;
} else { //add one for every path
if (n.getleftNode() == null && n.getrightNode() == null) {
return 0;
}
return 1 + Math.max(Height(n.getleftNode()), Height(n.getrightNode()));
}
}
public void add(int data) {
addNode(data, root);
root.setHeight(Math.max(Height(root.getleftNode()), Height(root.getrightNode())) + 1);
}
public void addNode(int data, Node n) {
if (data < n.getData()) {
if (n.getleftNode() == null) {
n.setToleft(new Node(data));
} else {
addNode(data, n.getleftNode());
}
n.setHeight(Math.max(Height(n.getleftNode()), Height(n.getrightNode())) + 1);
if ((Height(n.getleftNode()) + 1) - (Height(n.getrightNode()) + 1) == Math.abs(2)) {
if (data < n.getleftNode().getData()) {
n = LLRotation(n);
} else {
n = LRRotation(n);
}
}
} else if (data >= n.getData()) { //>= also caters for duplicates and inserts them infront of same value
if (n.getrightNode() == null) {
n.setToright(new Node(data));
} else {
addNode(data, n.getrightNode());
}
n.setHeight(Math.max(Height(n.getleftNode()), Height(n.getrightNode())) + 1);
if ((Height(n.getrightNode()) + 1) - (Height(n.getleftNode()) + 1) == Math.abs(2)) {
if (data >= n.getrightNode().getData()) {
n = RRRotation(n);
} else {
n = RLRotation(n);
}
}
}
}
public Node LLRotation(Node n) { //single
Node n1 = n.getleftNode();
n.setToleft(n1.getrightNode());
n1.setToright(n);
n.setHeight(Math.max(Height(n.getleftNode()), Height(n.getrightNode())) + 1);
n1.setHeight(Math.max(Height(n1.getleftNode()), Height(n)) + 1);
//compares heights of left and right subtrees and gets max
//the above source code is of course vital since the node height must be resetted after rotations
//adding 1 at the end of the last two code lines is important since
//initially the height is only calculated from subtrees onwards
//same for single right rotation below
return n1;
}
public Node RRRotation(Node n) { //single
Node n1 = n.getrightNode();
n.setToright(n1.getleftNode());
n1.setToleft(n);
n.setHeight(Math.max(Height(n.getleftNode()), Height(n.getrightNode())) + 1);
n1.setHeight(Math.max(Height(n1.getrightNode()), Height(n)) + 1);
return n1;
}
public Node LRRotation(Node n) { //double
n.setToleft(RRRotation(n.getleftNode()));
return LLRotation(n);
}
public Node RLRotation(Node n) { //double
n.setToright(LLRotation(n.getrightNode()));
return RRRotation(n);
}
public void inOrderTraversal(Node n) {
if (n != null) {
inOrderTraversal(n.getleftNode()); //recursive call to the left subtree
System.out.println(n.getData()); //line which makes the actual node to display its data
inOrderTraversal(n.getrightNode()); //recursive call to the right subtree
}
}
public void traverse() {
inOrderTraversal(root); // can be called in main class to automatically traverse tree from its root
}
public int search(int x) {
try {
if (x == root.getData()) { //basis step
System.out.println("Item found!");
return x;
}
if (x < root.getData()) {
root = root.getleftNode();
return search(x);//recursive call
} else {
root = root.getrightNode();
return search(x);//recursive call
}
} catch (NullPointerException e) {
System.out.println ("Search failed!");
return 0;
}
}
Main Class
public static void main(String[] args) throws IOException {
Scanner s = new Scanner(System.in);
AVL tree = null;
int choice = 0;
System.out.println("AVL TREE");
System.out.println("\n Choose an option from the menu: ");
System.out.println("\n\t 1.) Create file of 100 integers");
System.out.println("\n\t 2.) Create the tree");
System.out.println("\n\t 3.) In-Order traverse and show tree");
System.out.println("\n\t 4.) Search for integer");
System.out.println("\n\t 5.) Quit");
while (choice != 5) {
System.out.print("\nChoice: ");
choice = s.nextInt();
switch (choice) {
case 1:
createFile();
break;
case 2:
try {
FileReader readto = new FileReader("Integers.txt");
BufferedReader br = new BufferedReader(readto);
String line = br.readLine(); //reads text at start of file
line = br.readLine(); // skipping empty lines
line = br.readLine();
line = br.readLine();
int root = Integer.parseInt(line); //extracts first integer from the line
System.out.println("Root: " + root);
tree = new AVL(root);
int x = 0;
while (x != 99) {
try {
line = br.readLine();
int next = Integer.parseInt(line);
tree.add(next);
System.out.println(next);
x++;
} catch (NumberFormatException e) {
};
}
System.out.println("Tree successfully populated!");
} catch (FileNotFoundException e) {
System.out.println("ERROR: File not found!");
}
break;
case 3:
System.out.println("In-Order traversel executed. The now balanced tree shall now be printed in");
System.out.println("ascending order and also the left and right children of each node shall be printed.\n");
System.out.println("Traversal: ");
tree.traverse();
break;
case 4:
System.out.print("Please enter the integer to be searched: ");
int x = s.nextInt();
System.out.println(tree.search(x));
break;
case 5:
System.exit(0);
break;
default:
System.out.println("ERROR: Choice out of bounds!");
}
}
}
static void createFile() throws IOException {
Random r = new Random();
File intfile = new File("Integers.txt");
FileWriter writeto = new FileWriter("Integers.txt");
BufferedWriter bw = new BufferedWriter(writeto);
if (!(intfile.exists())) {
System.out.println("ERROR: File not found!");
} else {
bw.write("The following integers are randomly generated");
bw.newLine();
bw.write("and will be used to construct the AVL tree:");
bw.newLine();
bw.newLine();
int x;
System.out.println("The following random numbers shall be used to build the AVL tree: \n");
for (int i = 0; i < 100; i++) {
x = r.nextInt(100) + 1;
bw.write(String.valueOf(x));
bw.newLine();
System.out.println(x);
}
bw.close();
}
}
The output for the traversal is just the following:
Traversal:
44
53
54
54
77
Suppose that there were 100 integers entered and among them were these. But the output for the traversal was only this.
Output for the search is like this:
Choice: 4
Please enter the integer to be searched: 44
Item found!
44
Choice: 4
Please enter the integer to be searched: 100
Search failed!
0
100 and 44 were both integers added to the tree, but 44 was found and 100 wasn't.. I don;t understand..
Anyone can guide me to a solution..?
Thanks in advance :)
Well, first the obvious thing... In your search method, you are abusing the root variable, which holds the root of your tree, setting it to new values as your search proceeds. So, after the first search, root points to the last node traversed in the search and no longer to the root node of the tree. All following searches are unlikely to find anything at all from that point on.
As your search is recursive, try passing on the node-to-be-searched-in as parameter:
int search(Node node, int key) {
if (node == null) {
return 0; // missing from tree
} else if (key < node.getData()) {
return search(node.getLeft(), key);
} else if (key > node.getData()) {
return search(node.getRight(), key);
} else {
return node.getData(); // found it
}
}
(Edited to address the comments) You might have to expose this method like you do with your add/addNode method pair using a publicly available wrapper, and an internal implementation:
public int search(int key) {
return searchNode(root, key);
}
private int searchNode(Node node, int key) {
// Perform the recursive search, as above
}
There are other problems related to your add/addNode methods. Maybe I just overlooked it, but nowhere do you adjust the root node of your tree, if rotation would make it necessary. This, in effect, causes your tree to get out of balance, losing the AVL property over time.

Categories

Resources