Reputation:
I have a Binary Tree which has a word on each node.
In another Class I need to access the nodes one by one and then manipulate the words. What is the best way to access the nodes one by one from another class?
In my BinaryTree Class each node has a leftchild, rightchild and a value(String). I have three methods, printinorder, insert and findnode. The find node takes in a string and sees if that string is stored in any of the node values.
public void printInOrder(Node node) {
if (node != null) {
printInOrder(node.left);
System.out.println(node.value);
printInOrder(node.right);
}
I have another class and need to get at the nodes one by one but I am not sure whats the best way of doing it from another class. I am able to traverse the tree from within the class but not from a different class.
Upvotes: 0
Views: 2176
Reputation: 3225
Read templatetypedef's answer, which is a more efficient approach. The advantage of the approach I present is that it does not require any changes to your existing Node class, and uses the same pattern you've already used for PrintInOrder. It only relies upon public properties of the Node class, so should be useable from a separate class.
public List<Node> nodes GetAllNodes(Node root) {
List<Node> nodes = new ArrayList<Node>();
if (root != null) {
nodes.addAll(GetAllNodes(root.left));
nodes.add(root);
nodes.addAll(GetAllNodes(root.right));
}
return nodes;
}
I didn't compile or test this, so there may be errors.
Upvotes: 0
Reputation: 372952
One way to walk over all the nodes in a binary tree is to do the following, which starts the leftmost node and repeatedly migrates to the current node's inorder successor:
Start by walking as far to the left as you possibly can without falling off the tree. This is your first node.
To get from one node to the next one that should be visited, do the following:
This ends up taking only O(n) time across all nodes, so it's a very fast way to migrate between nodes.
In order for this to work, you will either need to have each node store a parent pointer or will have to maintain a stack of the path of nodes from the start node downward to the current node. However, for reasonably balanced trees, this is much more space efficient than populating an entire list of the nodes in the tree and returning it.
Hope this helps!
Upvotes: 1
Reputation: 195169
For searching :
You could sort your tree to a binary search tree. Or when you were building the tree, build a binary search tree. It would be the best way to search. O(logn)
.
The search should be implemented in your findNode(String value)
method
or you have problem to implement the search?
Upvotes: 0
Reputation: 20571
Assuming you have a tree class, write first a method that traverses the tree first (breadth/depth search). At every node in the traversal, assuming your nodes are some type of object that holds a word in one of its variables, access the node and change the stored word. Basically write functions in your Binary Tree class that let you do this, and call them in your other class.
Upvotes: 0