If given the following tree structure or one similar to it:
I would want the string ZYXWVUT returned. I know how to do this with a binary tree but not one that can have more than child nodes. Any help would be much appreciated.
This is called a post-order traversal of a tree: you print the content of all subtrees of a tree before printing the content of the node itself.
This can be done recursively, like this (pseudocode):
function post_order(Tree node)
foreach n in node.children
post_order(n)
print(node.text)
If you are maintaining an ArrayList (say node_list) to track the number of nodes branching of from the current tree node, you can traverse the tree from the root till you find a node that has an empty node_list. This way you will be able to identify the leaf nodes of the tree. A recursive approach would work for this case. I haven't tested the code but I believe this should work for what you have asked:
If you are maintaining something similar to the class below to build your tree:
class Node {
String data;
ArrayList<Node> node_list;}
The following recursive function might be what you are looking for:
public void traverse_tree(Node n){
if(n.node_list.isEmpty()){
System.out.print(n.data);
}
else{
for(Node current_node:n.node_list){
traverse_tree(current_node);
}
System.out.println(n.data);
}
}
Essentially what you are looking at is the Post-order Depth First traversal of the tree.
something like this should do it
public void traverse(){
for(Child child : this.children){
child.traverse();
}
System.out.print(this.value);
}
Related
This was one of the questions asked in a interview. Where I need to find the root node of the binary tree. There is no parent node like (Node parent) in the below definition of binary tree.
We have access to any node in the binary tree. It can be root or it can be any other node as well.
Binary tree :
1
/ \
2 3
/ \ / \
4 5 6 7
We might have access to say Node 5
Definition of Binary Tree is as below :
class Node
{
int data;
Node left;
Node right;
Node(int data)
{
this.data = data;
this.left = null;
this.right = null;
}
}
Any ideas how I can be achieved this.
public Node getParent(Node current)
{
}
Thanks !
Actually there may be many roots in the graph, that is node without parents or simply not reference by another node.
You could go through all node 1 time, and put the child node you encounter into a Set. The roots (node without parents) is the difference between all nodes in the tree and nodes in the set.
This would work in O(N), N being the number of nodes as long as the equals/hashCode method are fast. If the node are not duplicated in memory you would be able the memory reference for equality/hashCode and be extremely fast.
But this is only one random solution. You may want to change the data structure for example to have on one side all the nodes and on the other side a pointer to the root so then the access become O(1).
You should check a book on algorithmics to see all the possible solutions that make sense with their benefits and drawbacks.
Hopefully for an interview, what the interviewer look for is not necessarily that you know the response but that you are able to understand the problem, ask question to be sure you got it right, find at least one solution even if naive, be able to criticize it and potentially know way of improving it (like reading the book on algorithms).
I'm trying to write a code for the preorder traversal of a Binary Threaded tree in java. I wrote the following code, and it holds for a few examples, but I'm worried that I'm overlooking some edge scenarios.
MORE INFO
A node has two references left and right pointing to the left and right child of the node respectively. A boolean field called successor determines if the right pointer points to a child or the successor according to inorder traversal (if successor==false: right points to child, else points to inorder traversal successor)
It would be most appreciated if someone could point out the flaws in my logic here...
public void threadedPreorder(){
IntThreadedTreeNode prev, p=root; //pointers to binary tree nodes
while(p!=null){
while(p.left!=null){ //traversal to leftmost node
visit(p); //while visiting it
p=p.left;
}
visit(p);
prev=p;
p=p.right; //shift to right or successor
if(p!=null && prev.successor){ //avoid visiting the same node twice
while(p!=null && prev.successor){
prev=p;
p=p.right;
}
}
}
}
Any help would be appreciated...:)
First things first... You should write unit tests to find functional bugs
However you seem to have a bug here... while loop does not execute at all
if(p!=null && prev.successor){
while(p!=null && !prev.successor){
prev=p;
p=p.right;
}
}
You may want to replace it with do-while
I have a binary tree not bst ,I need to find the depth of a node in that binary tree
Is there any other way to achieve other than level order traversal using some dilimeter to main the count of level .
As input I have the root node of the tree and one of the node of the tree for which i need find the depth.
I want to have some recursive way to find this
If you don't want to do a BFS you can do a DFS (and you can also do it recursively).
pseudo-code for DFS function, the first call will be DFS(root).
DFS(node v, integer d)
visited[v] = true
depth[v] = d
for each u such that u is adjacent to v
if visited[u] == false
DFS(u, d+1)
Try passing an additional parameter in your recursive function to indicate depth.
So Currently I have a program that creates a huffman tree. the tree is made up of "Hnodes" with these fields: right (points to right child) left (points to left child) code (string of integers, ideally the 0's and 1's that will be the huffman code of this node) character (the character contained in the node).
I have created the huffman tree by adding nodes from a linked list - i know the tree was created correctly. As i created the tree, i told the node when i gave it a parent node, that if it was the parent's "right", its code string was 1, if left 0. However obviously after the entire tree is created, each node is only going to have either a 0 or 1, but not yet a string like 00100101. My question is, now that I have this tree, can can I traverse it? I understand the thought would be to give each child its parent's code + the child's own code, but I do not understand how to loop through the tree to accomplish this.
Thank you in advance.
Maybe this?
ExpandBinaryPaths(node, prefix)
1. if node is null then return
2. else then
3. node.binary = prefix concat node.binary
4. ExpandBinaryPaths(node.left, node.binary)
5. ExpandBinaryPaths(node.right, node.binary)
6. return
The idea is you would call this on the root with no prefix... ExpandBinaryPaths(root, "").
I have a transverse tree written in JSP which goes through an XML file.
When I get to a certain Text Node, I'd like to be able to search back up the tree to find a certain element associated with that node.
I'm thinking I need to do a For loop and use some kind of 'getLastNode' or 'getParentNode' function. Would this be the correct method? I'm a little unsure of the syntax, so any help would be much appreciated!
I did a bit of search and I can't find anything which demonstrates what I'm trying to do nor can I find a list of the functions I'm after.
You need to keep calling getParentNode until you hit a node matching your criteria. For example:
public Node searchUpFor(String tagToFind, Node aNode) {
Node n = aNode.getParentNode();
while (n != null && !n.getNodeName().equals(tagToFind)) {
n = n.getParentNode();
}
return n;
}