java


Looping a statement for different results in java


Today I am trying to loop a print statement so it outputs different results each time. The premise is I have a binary search tree that has been filled with random numbers 0-99.
I then select a random node and get the length from root to this random node.
I need to repeat the above sentence a number of times but all my loops so far have given the same answer or looped infinitely.
while(i!=10){
System.out.println(bst.Pathlength(root, random_node));
i++;
}
for (int i = 0;i< 10; i++){
System.out.println(bst.Pathlength(root, random_node));
}
while (true) {
int i = 0;
int j = 1000;
if (i != j) {
System.out.println(bst.Pathlength(root, random_node));
i++;
}else if (i == j){
break;
}
}
Above is a selection of loops I have been trying(not at the same time).
Pastebin link with full program Included full program for the full picture.
Any pointers on this would be great :)
Currently, you're passing in the same instance at each iteration of the loops hence why it will print the same result each time as you've mentioned, rather you'll need to traverse all the nodes within the tree meaning from the current node, then left(if any), then right(if any).... and so forth down the tree.
you might want to create an inOrder method which traverses down the tree to print the data of the sub-trees.
As for you loops, the first two should work as expected, However, the last loop will always loop infinitely because you're resetting i to 0 at each iteration of the loop, meaning it will never reach j in order to break out of the loop. Solution to this is simply to declare the i and j variables outside the loop.
Different types of Tree Traversals.

Related Links

SymmetricDS how to filter rows during a transformation
How Do I Model My Results Using Java Collections?
Dynamic Image Making
Role of hibernate queries in heap dump
Tree comparison node by node
error while using elastic search while creating a osgi bundle
How can I set path to myException.java in client?, when create client from WSDL
Android Opengl es 1 - asynctask causing strange behaviour on some devices
Spring MVC <form:errors/> tag doesn't find error messages, what do I wrong?
Re-instantiating objects without losing previous
Performance of #XmlSeeAlso
How to change color of background Android Java in Eclipse ADT
Unknown KieSession name in drools 6.0 (while trying to add drools to existing maven/eclipse project)
Determine file based on Coordinates of touch
My code seems to be sound but any time I click the submit button it does nothing
JavaLite Http exception handling

Categories

HOME
ionic-framework
variables
wso2
openmp
keras
heroku
dictionary
objectgears
amazon-ecs
frameworks
twitter-bootstrap-4
dxl
session-variables
imacros
nstableview
collectd
commonmark
jsprit
ghc
http-status-code-504
connection-string
excel-vba-mac
karma-jasmine
jtextfield
claims-based-identity
wpfdatagrid
nhapi
entitlements
minitab
xacml
subdomains
primitive
data-manipulation
siesta-swift
stringtemplate
subset-sum
android-ble
appfabric
lto
twitch
xenforo
azure-ml
graphenedb
midl
mime
dotcover
azure-application-gateway
network-flow
pnotify
domain-model
return-value
fancybox-2
lift-json
angular-strap
wdf
deadbolt-2
gridpane
gstreamer-0.10
actionbardrawertoggle
skype4py
nessus
cyclomatic-complexity
mutation-observers
fouc
coveralls
ionic
microbenchmark
inmobi
rake-task
gadt
relocation
internet-connection
rdl
expected-exception
openlaszlo
mylyn
android-screen-support
comaddin
orchardcms-1.7
mqx
lcs
android-hardware
robotics-studio
wsdl-2.0
angularjs-controller
nsnetservice
viewswitcher
objective-c-2.0
nsmanagedobject
spring-portlet-mvc
stage
spyware
heartbeat
blackberry-playbook
genshi
table-footer
file-comparison
calling-convention
actionview
oncheckedchanged
xfbml
radcombobox
web-architecture
nintendo-ds
port-number

Resources

Encrypt Message