• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Ugh.. Java Help..

SaturnX

Diamond Member
Alrite... well here's the deal.. I need to create a "tree" structure, such that I can easily reference all the objects leading to, and from any object in the "tree" Essientially, it will be constructed randomly, (this is my problem, so I can't build the tree downward)

Therefore, I need to build the references out of order, yet be able to add more references from one object to another, essientially at the end leaving me with the following:

[Item] --> [Item2] --> [Item4]
.....|........................|
[Item3]..........[Item5]
.....|.......................|
[Item6]..........[Item7]

(Ignore the periods.. just spacers)

Basically a tree structure, then I need to trace my way back through the structure... being new to Java.. this poses several problems.. as I need to be able to start with say.. item 5, and then continually add more items.. that may not necessarily be in order.
--Mark
 
Sounds like you need binary tree. Here's the basic code:

public class treeNode{
....public Object data;
....public treeNode leftChild = null;
....public treeNode rightChild = null;

....public treeNode(Object objData){
........data = objData;
....}
}

public class binaryTree{
....treeNode root = null;

....public void addData(Object data){
........if (root == null){
............root = new treeNode(data);
........}
........else{
............addNode(root, data);
........}
....}

....public void addNode(treeNode testNode, Object data){
........if (data < testNode.data){
............if (testNode.leftChild == null){
................testNode.leftChild = new treeNode(data);
............}
............else{
................addNode(testNode.leftChild, data);
............}
........}
........else{
............if (testNode.rightChild == null){
................testNode.rightChild = new treeNode(data);
............}
............else{
................addNode(testNode.rightChild, data);
............}
........}
....}
}

It's fairly easily to build a findNode method (to traverse down the tree). Also, i'm not sure about the syntax and everything, you'll want to double-check. The tree can be fairly off-balanced depending on how the data is entered, which makes traversing it inefficient. A self-balancing (e.g. AVL) tree would be better, but the code is a lot more complex.
 
Hrmm.. alrite.. thanks a lot.. I'm going to try and fiddle around with the binary tree structures...

--Mark
 
Hehe... thanks for the offer notfred... but I think I've found a fairly simple solution... essientially, the problem I need to solve is a path between cities, and the roads link in only certain paths.. I wouldn't be able to interpert Btrees.. and they're really beyond my scope of knowledge at the moment.. but I'll be creating a 2 dimensional boolean array, and work with that.

--Mark *Still Learning Java... But extremely skilled in vb
 
Back
Top