Is it possible to create a tree of objects in Java? -
I am trying to create an object tree in java. I also want to use Java class which makes it easy to add or remove nodes from the tree. What will be the best class for this purpose?
Example: Here is an array of objects. The object is the string "World" located above the array, the leaves are integer here, and I want to add "this is (world, 0, 0) string"! Will Java class be the best for this purpose as a leaf on "(World, 0, 0)?"
"world" / \ 0/0/0 0 0
Create your own It's easy. Super super easy:
public class tree {public node route; } Public class node {public array list & lieutenant; Node & gt; children; Public node parent; Public string value; } Now, something like this will be done by adding a string value with the sequence of an integer:
public square tree {put public string (string value) , Int [] path) {node current = root; For (int i = 0; i & lt; path.length; i ++) {if (present children. I == tap) {current.children.add (i, new node ()); } Present = present. Creed .. (i); } String ret = current.value; Current.value = value; }} Getting value will be the same, except that you will not overwrite the current value with a given value.
What does do in English: - Visit n th child of the current node, where n your path The next value is in
- If the child is not present, then make it.
- Repeat until the path ends.
- Return the current value (optional)
- Set the value to the new value.
Then something like this will appear:
tree myTree = new tree (); MyTree.root = new node (); Int [] path = {0, 0, 0}; MyTree.put ("Hi", Path); Println (myTree.get (path)); And you will find "Hello" in your console.
Comments
Post a Comment