| Author |
Topic: Ok question about java... |
| MoNoXBooGie~rb |
General Member Since: Dec 23, 2006 Posts: 3176 Last: Dec 23, 2006 [view latest posts] |
|
|
|
Category: SoFII General Posted: Monday, Aug. 11, 2003 09:39 pm |
 |
I have this thing and I would like people who know a lot about java to try and see who's right. Ok here's the deal.... I had a test today with the course 'datastructures & algebra' uses java as the main language. This one problem was like this: You have to write a class in java that should build a binary tree, with each node having 2 children with height n by only using the constructor and no other methods. The tree should have a String variable saying "left" for the left child en "right" for the right node. Also it should have a reference to it's parent. So calling Tree(4) should construct a decission tree with height 4. Well the only logic way I can think of (the only way they taught us to construct a tree) is using Recursion (letting a method call itself again until the tree is completely constructed). One of my friends who did the same exam said he asked the teacher and that also the teacher agreed that recursion can't be used with only a constructor, but adding a private method would help a lot. I didn't use a private method, and I just made a little test program in 2 minutes to see if recursion works.... this is what it looks like: public class TestRec{ public int x; public TestRec kind; public TestRec(int i){ if (i != 0){ kind = new TestRec(i - 1); x = i; System.out.println(i + ""); } } } public class Test{ public static void main(String[] args){ TestRec test = new TestRec(6); } } and it produces a column with numbers from 1 to 6..... this is recursion, right? With only a constructor?  |
 |
|
|
| MoNoXBooGie~rb |
General Member Since: Dec 23, 2006 Posts: 3176 Last: Dec 23, 2006 [view latest posts] |
|
|
|
|
| ^MOB^ tkekd538~rb |
General Member Since: Dec 23, 2006 Posts: 184 Last: Dec 23, 2006 [view latest posts] |
|
|
|
|
| MoNoXBooGie~rb |
General Member Since: Dec 23, 2006 Posts: 3176 Last: Dec 23, 2006 [view latest posts] |
|
|
|
|
| Fuz@2die4.com~rb |
General Member Since: Dec 23, 2006 Posts: 145 Last: Dec 23, 2006 [view latest posts] |
|
|
|
Category: SoFII General Posted: Wednesday, Aug. 13, 2003 12:01 pm |
 |
Your example doesn't really make the tree... public class Tree { Tree parent, left, right; String where; public Tree(Tree parent, int level, String where) { this.parent = parent; this.where = where; if(level-- == 0) return; left = new Tree(this, level, "L"); right = new Tree(this, level,"R"); } } public static void main(String args[]) { Tree tree = new Tree(null, 4, "Root"); } Now u need a public method to print the tree... As for recursive constructors... They are bad if u fail to terminate the recursion There is also a problem related to Polymorphism, how to pass on the class type you are trying to make a tree of... |
 |
|
|
| sp00nraker~rb |
General Member Since: Dec 23, 2006 Posts: 67 Last: Dec 23, 2006 [view latest posts] |
|
|
|
|
| ^MOB^ tkekd538~rb |
General Member Since: Dec 23, 2006 Posts: 184 Last: Dec 23, 2006 [view latest posts] |
|
|
|
|
| MoNoXBooGie~rb |
General Member Since: Dec 23, 2006 Posts: 3176 Last: Dec 23, 2006 [view latest posts] |
|
|
|
Category: SoFII General Posted: Wednesday, Aug. 13, 2003 10:32 pm |
 |
Fuz, I know it doesn't make a tree in my example. That just makes a linear line of objects. As I said it was to test whether recursion works in a constructor, not to test whether my tree works. Besides that for a tree it's not much different except that you need to make another child for each node.  sp00n you're right too. You just create a tree of which Tree(4) creates a tree of 4 levels, and then with recursion you call Tree(4 - 1) and the next one will call Three(4 - 1 - 1) etc which is correct cuz the left node of a tree is also a tree but with one layer less. About recursive constructors being bad when not ending it, it's also bad when it doesnt end in a void. You'll run out of stack space either way. So that can't be the reason why it shouldn't work. *edit* I just thought about something.... maybe it's a bad idea to use recursive constructors because the object you're passing on hasn't been created yet? |
 |
|
|
| Fuz@2die4.com~rb |
General Member Since: Dec 23, 2006 Posts: 145 Last: Dec 23, 2006 [view latest posts] |
|
|
|
Category: SoFII General Posted: Thursday, Aug. 14, 2003 08:21 am |
 |
| Quote | | I just thought about something.... maybe it's a bad idea to use recursive constructors because the object you're passing on hasn't been created yet? |
No. When you are in the constructor, self exists. The object has been allocated and can be referenced.
The main problem about recursion in the Constructor, is the Polymorphism problem. Lets say you make a descendant class that has additional attributes... you can't call the inherited constructor because that creates nodes of its class and not of your class - unless you can pass the class type as parameter. So, it gets messy. If you use a virtual function to instantiate each node, you get around that problem. |
 |
|
|
| sp00nraker~rb |
General Member Since: Dec 23, 2006 Posts: 67 Last: Dec 23, 2006 [view latest posts] |
|
|
|
Category: SoFII General Posted: Thursday, Aug. 14, 2003 08:24 am |
 |
| Quote (MoNoXBooGie @ Aug. 13 2003, 5:32 pm) | | sp00n you're right too |
Wow, I honestly thought I'de never use anything I learned in Advanced Algebra class  |
 |
|
|
|
|
mp_TempleCall of Duty: Mods: Multiplayer (624.12Kb)
|