Results 1 to 2 of 2
  1. #1
    Googol is offline Banned
    Join Date
    Aug 2012
    Rep Power

    Default Binary Tree Design Algorithm

    I am currently learning how to use binary trees, but I am having trouble getting started on how to design basic methods. The simplest of the methods I need to write is finding the maximum number in an unsorted binary tree. I think if I could just see a method I could get an idea on how to travel throughout a binary tree.
    I know that sending a value such as 0 for my initial max is a bad idea just in case all of the input is negative, but I am not sure how to grab some leaf from the tree as my starting max value.

    The input is something like this. (((10 16) -2 25) (33 4) 21 59)

    Java Code:
    public static int findMax (Object tree) 
      return findMax2(tree, 0);
    public static int findMax2 (Object tree, int max)
    	if (method returning boolean for tree)
    		int left = findMax2(lhs((Cons)tree), max);
    		int right = findMax2(rhs((Cons)tree), max);
    		max = left > right ? left : right;
    	else if (method returning boolean for leaf)
    		int curr = (int) (Integer) tree;
    		max = curr > max ? curr : max;
    	return max;

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Washington, DC
    Rep Power

    Default Re: Binary Tree Design Algorithm

    You could set your initial value to negative infinity, or just the first value you come across.
    How to Ask Questions the Smart Way
    Static Void Games - GameDev tutorials, free Java and JavaScript hosting!
    Static Void Games forum - Come say hello!

Similar Threads

  1. tree algorithm question
    By stuckonjava in forum New To Java
    Replies: 8
    Last Post: 05-06-2012, 09:41 PM
  2. Binary Tree Help - Find the largest sub-tree
    By joshhazel in forum New To Java
    Replies: 2
    Last Post: 01-30-2012, 02:08 AM
  3. Algorithm Design
    By tabchas in forum New To Java
    Replies: 17
    Last Post: 04-18-2011, 12:58 AM
  4. Algorithm for converting binary/hex to decimal
    By addictz04 in forum New To Java
    Replies: 2
    Last Post: 11-29-2010, 06:49 PM
  5. Replies: 0
    Last Post: 04-04-2010, 07:40 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts