Recursive Method For A Tree

May 9, 2015

As one of the methods of my IntTree tree I have to implement a method that multiplies the level number with the sum of the nodes on the current level. So far I have this, and it doesn't work. What I am wondering is am I on the right track at all with the second return statement?

public int depthSum(){
return depthSum(overallRoot);
}
private int depthSum(IntTreeNode root) {
if(root==null)
return 0;
int level = 0;

[code]....

View Replies


ADVERTISEMENT

Recursive Method Using Technique Calling Tree

May 9, 2014

a)Write a method that recursively displays any given character the specified number of times on one line.For example, the call: displayRowOf Characters(,5);

Produce a line: *****Write another method that uses a for-loop to perform the same process.

B is something like this ?
for (i=1; i<=n; i++)
i= '*' * n;
System.out.print(i);

View Replies View Related

Recursive Method - Calculate Greatest Common Divisor Using Euclidean Method

Apr 29, 2014

Consider the following recursive method that calculates the greatest common divisor using Euclidean method.

PHP Code:

public static int GCD ( int x , int y )
{
    if ( y == 0 )                        
        return x;
    else if ( x >= y && y > 0)
        return GCD ( y , x % y );
    else return GCD ( y , x );  


Trace the above method for x=32 and y=46

View Replies View Related

Tracing Recursive Method That Calculates GCD

May 3, 2014

I have this code and i want to trace it

public static int GCD ( int x , int y ) {
if ( y == 0 )
return x;
else if ( x >= y && y > 0)
return GCD ( y , x % y );
else return GCD ( y , x );
}

(it is a recursive method that calculates the greatest common divisor using Euclidean method )

while x = 32 and y = 46

I want here to understand how the code work ? Precisely , composition and decomposition operations.

View Replies View Related

Why Recursive Method Continue To Return 0

Nov 13, 2014

I was told to write a method that adds up the sequence of the formula (n/2n+1) eg. 1/3 + 2/5 + 3/7 etc. simple enough i suppose. my method is below

public static double Series(int n){
if (n==0)return 0;
else return (n/(n*2+1)) + Series(n - 1);
}

However for some reason or another it returns 0 for any number that is put in. I've written it dozens of different ways with no change and i feel like something fairly obvious is being missed on my part. I am honestly intrigued and interested as to why this is happening. i assume it has something to do with the way i put the actual formula in cause if i put anything else in like simply n the recursion would work as expected.

View Replies View Related

Recursive Method And Printing Out Stars?

Nov 23, 2014

KtMok1t.jpg

Below is what I go so far, but how to do star C and E.

public class PrintTriangle
{
public static void printStars (int star)
{
for (int number = 0; number < star;number ++)
{
System.out.print("*");

[Code] ....

View Replies View Related

Recursive Method Called RangeSum

Oct 3, 2014

I'm trying to understand the concept behind this recursive method called rangeSum. This method sums a range of array elements with recursion. I tried summing the elements of 2 through 5, and I tried writing down what actually happens when java executes the program, but what I get when I try to figure it out by paper is different from what netbeans gives me. Here is a snapshot of my scratch work as well as my source code. Netbeans gives me "The sum of elements 2 through 5 is 18" when I try running it but it I get 12 when I do the recursion on paper. I know that the source code is correct because it's out of the book but what am I doing wrong when I try figuring it out by hand?

XML Code:

package recursivecall;
import java.util.Scanner;
/**
* Author: <<Conrado Sanchez>> Date: Task:
*/
public class RecursiveCall {

public static void main(String[] args) {

[code]....

View Replies View Related

Recursive Method To Remove Vowels In A String

Oct 22, 2014

Write down a recursive method to remove vowels in string ....

View Replies View Related

Making Selection Sort Into A Recursive Method

Oct 21, 2014

How would I modify this version of a selection sort into a recursive method?

public static void selectionSortRecursive(Comparable [] list, int n)
{
int min;
Comparable temp;
for(int index =0; index < n-1; index++){
min = index;

[code]....

View Replies View Related

How To Resolve StackOverFlowError When Using Recursive Method Call

Mar 24, 2014

I have 3 Xml documents that look like this:

model1:
Java Code: <?xml version="1.0" encoding="UTF-8"?>
<model>
<id>1</id>
<nodes>
<id>2</id>
<stencil>TASK</stencil>
</nodes>
<nodes>
<id>3</id>
<stencil>MODEL</stencil>

[Code] ....

After unmarshalling/marshalling in the main class I have created this 3 methods:

Java Code:

public List<Task> extractTasks(Model model) {
List<Task> tasks = new ArrayList<Task>();
for (ModelNodes modelNodes : model.getNodes()) {
if (modelNodes.getStencil().equals("TASK")) {
tasks.add(new Task(modelNodes.getId()));

[Code] ....

When the recursive call to the extractSubModels method is made, I get a **java.lang.StackOverFlowError** ...

View Replies View Related

Binary Tree Insert Method

Mar 24, 2014

So here is the code that I have. The problem is my insert method. It compiles, but it doesn't do so well in the test class I have set up for it.

package SystemandDesign.BinaryTree;
 mport java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.io.ObjectInputStream;
import java.util.Scanner;

[code]....

View Replies View Related

Find Node Method Within Binary Tree

Mar 29, 2015

I have some work where I have to create a binary tree. Anyway I am stuck on one of the classes which has several methods I must fill in! The code is below

The headers for each method are: public Node find (member p1)The method find(Member p1) is used to return the node which contains a particular member. It is a private method because the interface to CabinetTree should not provide direct access to the internal nodes. It makes use of the method Node findNode (Member p1, Node current)

and the next method is : Node findNode(Member p1, Node current) which searches the tree from current node to find a node whose value is equal to the Member p1, i.e. a node in the tree such that p1.equals(current.getMember()) is true. If there is no such node then the method should return null.

public class CabinetTree {
private Node head;
CabinetTree(Member p1) {
head = new Node(p1, null);

[Code] ....

View Replies View Related

Binary Tree Delete Method - NullPointerException

Oct 4, 2014

Iva tried flipping the entire code upside down (expression) and No matter what I do the delete function returns a null pointer exception after my while loops takes the pointer right to where it should be.

package bp;
import java.time.LocalDate;
public class BinaryTree implements IBinaryTree {
private Node root = null;
private int sizeOfTree = 0;

[Code] ....

View Replies View Related

Binary Search Tree - Calling FindSmallest Method In Main Class

Nov 3, 2014

How should I call my findSmallest method in the main class.. Here is the code:

public class Tester {
public static void main(String[] args){
try {
BinaryTree<Integer> myTree2 = new BinaryTree<Integer>();
 
myTree2.insert(5);
myTree2.insert(2);
myTree2.insert(7);

[Code] ....

So the question is what kind of parameter I should pass in myTree2.findSmallest()??? Here is my findSmallest method..

public E findSmallest(Node<E> parent){
if(parent.left.left == null){
E returnValue = parent.left.data;
parent.left = parent.left.right;
return returnValue;
} else {
return findSmallest(parent.left);
}
}

View Replies View Related

Java Tree Structure - Build Tree Based On Traversal Results

Jun 17, 2014

How to do draw the original binary tree based on traversal results?

A binary tree has this pre-order traversal result: A,B,D,H,I,E,F,C,G,K,J (TreeNodes) And the same tree gives the following in-order traversal: B,H,I,D,A,C,F,E,K,G,J. Can you draw the tree structure?

View Replies View Related

Creating Search Method For Binary Search Tree

Apr 22, 2014

I want to create a search method that returns the frequency of a word in the search method.

public class IndexTree {
private class TreeNode {
TreeNode left;
String word;
int frequency;
TreeNode right;

[Code] .....

View Replies View Related

Non-recursive BST Insertion?

Apr 6, 2014

I'm trying to implement a non-recursive version of the insertion method, but I'm having a bit of trouble. From what I can tell, it's only returning the last two node..

public void insert(Key key, Value val) {
root = insert(key, val, root);
} private Node insert(Key key, Value val, Node x) {
if(x == null) {
x = new Node(key, val, 1);

[code].....

View Replies View Related

Recursive Selection Sort

Oct 21, 2014

How would I modify this version of a selection sort into a recursive method?

public static void selectionSortRecursive(Comparable [] list, int n)
{
int min;
Comparable temp;

[code]....

View Replies View Related

Recursive Implantation For Setting Objects

Apr 8, 2015

I have a requirement where I have a class as Page which itself contains ArrayList<Page>.Here ArrayList<Page> is nothing but the pages which are accessible from the base Page.I know the depth level ( reading from file) which means how many level I need to go to identify more pages.BUT the problem is how to set the base Page class. I need to set the base Page class but for that I need the objects for the subsequent pages and hence an iterative type of implementation.

View Replies View Related

Recursive Function To Test All Combinations?

May 17, 2014

I am not sure how to add all the possibilities of elements in an array and find the greatest sum. I want to do this recursively. I don't need any code. How I would do it.

View Replies View Related

Recursive Program - Choose K Objects Out Of N

Apr 20, 2015

I recently wrote a simple recursive program that chooses K objects out of N (I was asked to use the variables N choose the R, however) total objects. Here is the code:

int n = 0;
int r = 0;
//the total number of objects defaults to 0
String nChoice = JOptionPane.showInputDialog(null, "How many objects are there to choose from?");
String rChoice = JOptionPane.showInputDialog(null, "How many object are to be chosen from this group?");
try {
n = Integer.parseInt(nChoice);

[Code] ....

It works fine, however in my class we were given two different formula to implement into our code. I used the one above, obviously. However, the second formula we were given was:

C(n,R) = n!
-------(R!(n-R)!)

I had to get the spacing right.

How do I read this formula? How could it be implemented? What are the benefits (if there are any) from using one method over the other? Which method of calculating N choose K (or, in my case, N choose R) would be more widely accepted?

View Replies View Related

Fibonacci Number - Recursive Function

Jul 8, 2014

I wrote this tail recursive function that mirrors the iterative version, except that the loop in the iterative version is replaced by an if statement here and then a recursive call. Is this truly recursive? I have seen the fibo(n-1) + fibo(n - 2) version, but is this also an acceptable recursive solution? Why is it never solved this way?

public class FiboRecursive {
public static int fibo (int n) {
int sum = 0;
int n1 = 1;
int n2 = 1;
if (n == 1 || n == 2) {
sum = 1;

[Code] ...

View Replies View Related

Calculating Arithmetic Expression - Recursive Function

Oct 26, 2014

I have a given arithmetic expression, which form is (A+B) or (A-B), were A and B are either numbers from 0-9, or another ARITHMETIC Expression.

The first thing that comes to my mind is a recursive function, but how to start...

View Replies View Related

Recursive Program To Change Two Four Letter Words Into Each Other?

Oct 28, 2014

SO for my project, we have to create a program where we input two four letter words, and using a list of words our teacher provided us and only changing one letter at a time, make the words match.For example, you input BALD and CALL and it would output BALD BALL CALLWe have to use recursion to do this, and I'm totally lost as to where to even begin.

View Replies View Related

Writing A Recursive Merge Sort Algorithm

Mar 27, 2015

I have this assignment to write a Merge Sort algorithm using recursion. To start I have a very tough time picturing what is happening when it comes to recursion, but I do understand how merge sorting works. At the moment I feel as though a very good portion of my code is correct, but I am having trouble with the recursion in the main method [ mergeSort(Queue<T> queue) ].

I have another 4 or so hours to pass in my assignment finished or not, and at this point I can honestly say I have no clue how to make my code work. I tried working through the problem on paper with a simple queue of size 3, but even that is a struggle. On paper my code works perfectly fine, so there is definitely something I am missing.

Below is what I have along with my JUnit test.

Java Code:

private Queue<T> output = new Queue<T>();
private Queue<T> output1 = new Queue<T>();
private Queue<T> output2 = new Queue<T>();
public Queue<T> mergeSort(Queue<T> queue) {
// TODO 1
if(queue.size() <= 1) {
return queue;

[Code] .....

View Replies View Related

Recursive Backtracking Solution - How To Cache Answers Into Appropriate Array

Jan 23, 2014

I can often write a recursive backtracking solution, but don't know how to cache the answers into an appropriate array.

For example:

Java Code:

public static int max(int[] costs, int index, int total, int shares) {
if(index >= costs.length) {
return total;
}
int buy = max(costs, index + 1, total - costs[index], shares + 1); // buy one stock
int sell = max(costs, index + 1, total + shares * costs[index], 0); // sell all stocks
return Math.max(total, Math.max(buy, sell)); // compares between buy, sell, and doing nothing
} mh_sh_highlight_all('java');

This is a dynamic programming exercise, but I have no idea what dimensions the dp array should be (I was thinking maybe dp[index][total][shares], but that seemed like overkill). Is this just because my understanding of recursion isn't solid enough or am I missing something else?

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved