java


Infinite loop while inserting nodes to BST


I have a method to insert nodes in a BST according to the alphabetical order, but I have an infinite loop when I compare the 2 strings I think that the value never changes when it passes the comparison so it's comparing again with the same values resulting in an infinite loop. I think that the aux and Tnodes are not updating the values with the recursive method so it's comparing the same values over and over.
class BST {
BSTNode root;
public BST() {
root = null;
}
BSTNode aux = new BSTNode();
BSTNode insertNames(BSTNode T , int data, String name, double salary) {
if (root == null) {
T = new BSTNode();
T.setName(name);
root = T;
} else {
aux = root;
if (name.compareTo(aux.getName()) < 0)
aux.setLeft(insertNames(aux.getLeft(),data, name, salary));
else if (name.compareTo(aux.getName()) >= 0)
aux.setRight(insertNames(aux.getRight(),data, name, salary));
}
return T;
}
}
class Main{
public static void main(String[] args){
BST alpha=new BST();
BSTNode root = new BSTNode();
alpha.insertNames(root, 0, "Roy", 0);
alpha.insertNames(root, 0, "Joseph", 0);
}
}
package com.gati.dsalgo.string;
class BST {
BSTNode root;
public BST() {
root = null;
}
void insertNames(int data, String name, double salary) {
root = insertNames(root, data, name, salary);
}
BSTNode insertNames(BSTNode root, int data, String name, double salary) {
if (root == null) {
root = new BSTNode();
root.setName(name);
return root;
}
if (name.compareTo(root.getName()) < 0)
root.setLeft(insertNames(root.getLeft(), data, name, salary));
else if (name.compareTo(root.getName()) >= 0)
root.setRight(insertNames(root.getRight(), data, name, salary));
return root;
}
}
public class Main1 {
public static void main(String[] args) {
BST alpha = new BST();
alpha.insertNames(0, "Roy", 0);
alpha.insertNames(0, "Joseph", 0);
System.out.println("hello");
}
}
class BSTNode {
private String name;
BSTNode left;
BSTNode right;
public void setName(String name) {
this.name = name;
}
public void setRight(BSTNode right) {
this.right = right;
}
public void setLeft(BSTNode left) {
this.left = left;
}
public BSTNode getRight() {
return right;
}
public BSTNode getLeft() {
return left;
}
public String getName() {
return name;
}
}
Please return the node in recursion end logic.
/* If the tree is empty, return a new node */
if (root == null) {
root = new BSTNode();
root.setName(name);
return root;
}
/* Otherwise, recur down the tree */
if (name.compareTo(root.getName()) < 0)
root.setLeft(insertNames(root.getLeft(), data, name, salary));
else if (name.compareTo(aux.getName()) >= 0)
root.setRight(insertNames(root.getRight(), data, name, salary));
/* return the (unchanged) node pointer */
return root;
or it can be solve iterative way
if (localRoot == null) {
newNode = new Node < V > (value, null);
root = newNode;
size++;
return true;
}
if (comparator != null) {
//Some code
} else {
Comparable << ? super V > v = (Comparable << ? super V > ) value;
while (localRoot != null) {
parent = localRoot;
cmp = v.compareTo(localRoot.getValue());
if (cmp < 0) {
localRoot = localRoot.getLeftChield();
} else if (cmp > 0) {
localRoot = localRoot.getRightChield();
} else {
localRoot.incrementBy(nCopies);
return true;
}
}
newNode = new Node < V > (value, parent);
if (cmp < 0) {
parent.setLeftChield(newNode);
} else if (cmp > 0) {
parent.setRightChield(newNode);
}
size++;
return true;
}
return false;

Related Links

EOFException after readUTF randomly
Pass Arraylist as value from Mapper to reducer in Hadoop
Generic InputCell for gwt Column (Change Cell-type or know Property's Cell-type before creating Column-constructor)
Make sure the Cursor is initialized correctly before accessing data from it.
How to write this loops in Java 8 Stream API
Compile and Run java program in linux with path to the .java file and path to external jars
Why am I getting error in Spring basic application?
Html mail sent with Spring's JavaMailSender is without subject and plain-text
How can I keep only outer table border and remove all other borders inside the table in iText in java
Data is not displayed in the FXML table
Why Android soft keyboard omits (delete) letters when number or symbol tapped?
How to get data from the db in activity a, when activity b is closed in Android with ActiveAndroid
Hibernate NULL values of map not added (xml mapping)
Parsing JSON for consumption in highchart.js
Files.newOutputStream vs FileOutputStream
Chromedriver in Java not executable

Categories

HOME
sendgrid
arduino-uno
image
asp.net-core
wso2
hook
keycloak
azure-data-factory
reserved
homebrew
relative-path
mean-stack
xmpp
rsync
c#-2.0
ojdbc
mouse
contact
virtualization
reveal.js
export-to-csv
zend-framework3
jplayer
quickfix
quartz-scheduler
systemc
django-admin
transformation
http-status-code-504
undefined
contextmenu
sqlcipher
django-storage
selectedindexchanged
internet-explorer-9
linkerd
applozic
tasklet
glew
spring-security-kerberos
executenonquery
filepicker
komodoedit
mu
lowpass-filter
setter
knockout-components
google-perftools
spring-cache
titanium-android
crosswalk-runtime
heidisql
android-textview
multi-level
thrust
mongocsharpdriver
fuzzy-search
qcustomplot
netmq
uid
cannon.js
python-3.2
citrus-pay
security-testing
muse
ios9.1
sailfish-os
sniffer
directoryservices
npapi
angularjs-ng-click
project-planning
google-reader
comexception
braille
android-2.2-froyo
uv-mapping
web2py-modules
dataservice
padarn
limejs
clipper
factory-method
errai
jquery-ui-layout
joyent
xdomainrequest
javax.script
propagation
nhibernate.search
adrotator
avatar
anti-piracy
sustainable-pace
spec#
geneva-server
dbisam
.net-1.0
ntvdm.exe

Resources

Encrypt Message



code
soft
python
ios
c
html
jquery
cloud
mobile