Results 1 to 2 of 2
Thread: Binary Tree Design Algorithm
- 10-29-2012, 06:25 AM #1
Member
- Join Date
- Aug 2012
- Posts
- 11
- Rep Power
- 0
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; }
- 10-29-2012, 01:33 PM #2
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 - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
tree algorithm question
By stuckonjava in forum New To JavaReplies: 8Last Post: 05-06-2012, 09:41 PM -
Binary Tree Help - Find the largest sub-tree
By joshhazel in forum New To JavaReplies: 2Last Post: 01-30-2012, 02:08 AM -
Algorithm Design
By tabchas in forum New To JavaReplies: 17Last Post: 04-18-2011, 12:58 AM -
Algorithm for converting binary/hex to decimal
By addictz04 in forum New To JavaReplies: 2Last Post: 11-29-2010, 06:49 PM -
Data Structures(Binary Search Tree to AVL Tree)ASAP pls
By jfAdik in forum Forum LobbyReplies: 0Last Post: 04-04-2010, 07:40 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks