Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Generalizing actions for a binary tree traversal?

  1. #1
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Generalizing actions for a binary tree traversal?

    I'm trying to find a way that I could take a binary tree class and traverse through it's nodes, performing X inline actions on each node without having to rewrite the same traversal code.
    If Java allowed function pointers, this would be much simpler for me to write.

    Basically, what I need is something like the following:
    public class BinaryTreeNode {
    	public String identifier = "";
    	public BinaryTreeNode parent = null;
    	public BinaryTreeNode left = null;
    	public BinaryTreeNode right = null;
     
    	public BinaryTreeNode(BinaryTreeNode parent, String identifier) {
    		this.parent = parent;
    		this.identifier = identifier;
    	}
     
    	public boolean isRoot() {
    		return parent == null;
    	}
     
    	public void inOrderTraversalFrom(BinaryTreeNode node, /* ??? */ actions) {
    		if(node.left != null)
    			inOrderTraversalFrom(node.left);
     
    		/* do something with "actions" here */
     
    		if(node.right != null)
    			inOrderTraversalFrom(node.right);
    	}
    }
    ... where actions could allow for different methods to be performed, taking in a different set of parameters depending on the type of action(s) to be performed on each node.
    How is this possible? What would be the most dynamic way to perform this?


  2. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Generalizing actions for a binary tree traversal?


  3. #3
    Junior Member
    Join Date
    Feb 2013
    Location
    Germany
    Posts
    27
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Generalizing actions for a binary tree traversal?

    Take a look at this:
    	public static interface IFunction<T> {
     
    		public void callFunction(T t);
     
    	}
     
    	private static IFunction<String> functionPointer = new IFunction<String>() {
     
    		@Override
    		public void callFunction(String t) {
    			System.out.println(t);
     
    		}
    	};
     
    	public static final List<IFunction<?>> functions = new ArrayList<>();
     
    	{
    		functions.add(functionPointer);
    	}
     
     
    	public void inOrderTraversalFrom(BinaryTreeNode node, List<IFunction<?>> actions) {
    		if(node.left != null)
    			inOrderTraversalFrom(node.left, null);
     
    		IFunction<String> func = (IFunction<String>) actions.get(0);
     
    		func.callFunction("Hello function pointer");
     
    		if(node.right != null)
    			inOrderTraversalFrom(node.right, null);
    	}

    That's a quick and dirty solution but that's a kind of function pointing in java.

    You can use generic parameter to change the parameters but you must declare parameter classes for each function signature.

Similar Threads

  1. Binary Search Tree inorder tree traversal
    By Maukkv in forum What's Wrong With My Code?
    Replies: 17
    Last Post: January 26th, 2013, 05:28 PM
  2. Help with this binary tree
    By sim18 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: November 29th, 2012, 03:10 AM
  3. Binary Tree Traversal help please
    By sim18 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 27th, 2012, 08:01 AM
  4. need help with binary tree
    By vash0047 in forum Java Theory & Questions
    Replies: 5
    Last Post: July 12th, 2010, 09:23 AM
  5. Data Structures(Binary Search Tree to AVL Tree)ASAP
    By jfAdik in forum Algorithms & Recursion
    Replies: 2
    Last Post: April 5th, 2010, 04:58 AM