Welcome to Dream.In.Code
Become a Java Expert!

Join 149,602 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,896 people online right now. Registration is fast and FREE... Join Now!




Finding an optimal decision tree

 
Reply to this topicStart new topic

Finding an optimal decision tree

Wo Hu
11 Oct, 2007 - 03:30 AM
Post #1

New D.I.C Head
*

Joined: 11 Oct, 2007
Posts: 2


My Contributions
I have to write a program to solve the following problem:

A and B are playing. A thinks of a number between 1 and N. B has to find it out with using only this question: "Is it equal to or lesser than x?" where x is any number between 1 and N. To make things more difficult, a given value belongs to each of the numbers between 1 and N. Whenever B poses his question, he must pay as many dollars as the value that belongs to x.
For example, I get this in an input file:
CODE
7
1 3 10 1 5 2 6

Where the first line is N, and the numbers in the second line are the values that belong to 1, 2, 3 etc.

I have to find out how much does it cost at least to find out any number A can think of (between 1 and the given N of course).

There's an example that shows the optimal question sequence (that produces the minimal amount of dollars required to find out any number between 1 and 7). This minimal amount here is 14.
IPB Image

This picture shows the questions (the numbers in circles). The left child is selected when the answer's yes, and the right child when the answer's no. The small numbers outside the circles mean their value.

Now how do I create this optimal question sequence (this binary tree)? I only get the input shown above in the CODE box.

Thanks for your help!
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: Finding An Optimal Decision Tree
11 Oct, 2007 - 04:32 AM
Post #2

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
you will need to post an attempt at this question, known as "good faith" code. We area community to help and encourage programming, but we will not do your homework for you.

You seem to understand the concept quite well, have you tried implementing a binary tree class? Is a specific extension supplied for you? Please show us what you have done so far.
User is offlineProfile CardPM
+Quote Post

Wo Hu
RE: Finding An Optimal Decision Tree
11 Oct, 2007 - 06:47 AM
Post #3

New D.I.C Head
*

Joined: 11 Oct, 2007
Posts: 2


My Contributions
I've made a Binary tree data structure implementation, here it is:

(originally it contains variable names and function names that make sense in Hungarian only, I just made a quick translation of all of those, so this code may or may not compile)
CODE
public class BinTreePoint<E>{
    public E element;
    public E value;
    public BinTreePoint<E> left;
    public BinTreePoint<E> right;
    public BinTreePoint<E> father;
    public BinTreePoint(){}
    public BinTreePoint(E x, E y, BinTreePoint<E> b, BinTreePoint<E> j){
        this.element=x;
        this.value=y;
        this.left=b;
        this.right=j;
        if (b!=null)
            b.father=this;
        if (j!=null)
            j.father=this;
    }
}


'E' will probably be exchanged with Integer.

I have no problem with reading the input (the 'values' will be put in a vector, the N will be put in an int). Once I've got the optimal tree, I will be able to find the most expensive route in it. I didn't start coding that yet, because I'm not 100% sure this concept is the best one. Maybe there is some way to calculate the optimal decision tree without actually constructing it, using a 2D array.
I don't need code, all I need is the algorithm (maybe a pseudo-code) that finds the optimal question sequence.

This post has been edited by Wo Hu: 11 Oct, 2007 - 06:47 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 11:49PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month