• Post Reply Bookmark Topic Watch Topic
  • New Topic

Tree Stucture  RSS feed

 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, i am having difficulties programming a tree structure.I would like to specify an max depth of an tree and let the program randomly generate nodes for me until max depth is reach. how would i go accross this? I am creating a mathematical expression onto a tree
I have made a node class that contains the node itself and the leftchild as well as the rightchild. I do not know what is the next step.
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so you have tree, then ?
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:so you have tree, then ?

I only defines my simple tree class
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
so you want max depth ? to particular point/node or from node? if from root then tree height is answer.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terence hiu wrote:Hi guys, i am having difficulties programming a tree structure.I would like to specify an max depth of an tree and let the program randomly generate nodes for me until max depth is reach. how would i go accross this? I am creating a mathematical expression onto a tree
I have made a node class that contains the node itself and the leftchild as well as the rightchild. I do not know what is the next step.


You need to define what "randomly generate nodes" mean. The "until max depth is reach" part just specify when you stop randomly generating, and makes no reference on how to randomly generate.

Henry
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:so you want max depth ? to particular point/node or from node? if from root then tree height is answer.

from root
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terence hiu wrote:
Seetharaman Venkatasamy wrote:so you want max depth ? to particular point/node or from node? if from root then tree height is answer.

from root

I mean by randomly generating a arithmetic expression for a randomly generated tree
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Terence hiu wrote:
from root

* to know the height --> there is recursive algorithm

* for clear understanding I would suggest you to know what is called level order traversal of tree...

Note: in your tree class, parent reference is missing , which play crucial role in most circumstance .. for instance, print a specific path of a tree
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seetharaman Venkatasamy wrote:
Terence hiu wrote:
from root

* to know the height --> there is recursive algorithm

* for clear understanding I would suggest you to know what is called level order traversal of tree...

Note: in your tree class, parent reference is missing , which play crucial role in most circumstance .. for instance, print a specific path of a tree


I know what is tree traveral, it is post order i need. It's jsut i do not know how it is implemented in java
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you mean you want the value of the nodes to be randomly generated? Assuming that you know how to generate a random string. Just write a recursive function that creates 2 nodes with random string, and puts hem as children of current node. If depth is less than target, call the function recursively
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:Do you mean you want the value of the nodes to be randomly generated? Assuming that you know how to generate a random string. Just write a recursive function that creates 2 nodes with random string, and puts hem as children of current node. If depth is less than target, call the function recursively

Thankyou. solves part of my problem. im just wondering how i can work out the depth of my tree now
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean? Isn't depth of tree one of your inputs?
 
Terence hiu
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:What do you mean? Isn't depth of tree one of your inputs?

it wasn't but know i changed it and its working. Thank you
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!