Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 499
  • Last Modified:

Decision trees in delphi ???

Hi

i've been working on some program and i have to check a lot of things in it so it will result in a huge , really huge list of nested if's.
Now i have read things about decision trees and i think this is a solution for me BUT i have no idea how to implement int in delphi ! i know it is a linked list and i also now you have to use recursion to do it so if someone could point me to a site where they explain it using examples and code for delphi or can explain it to me with some code i will be really happy !
0
joyrider
Asked:
joyrider
1 Solution
 
EpsylonCommented:
I've never heard of a decision tree in Delphi. Only decisioncubes, -grids and -graphs, but that is something completely different.
0
 
joyriderAuthor Commented:
here is a small explaination of it but it couldn't help me any further :(
http://www.gamedev.net/reference/programming/features/trees1

i need a concreet example in delphi so i can use it, cause i'm not going to nest 20 or more if's
0
 
RickHalleCommented:
I think he may be refering to case statements which are usually faster anyway.

This is directly from the Delphi5 online help documentation.

The case statement provides a readable alternative to complex nested if conditionals. A case statement has the form

case selectorExpression of

  caseList1: statement1;
  ...
  caseListn: statementn;
end

where selectorExpression is any expression of an ordinal type (string types are invalid) and each caseList is one of the following:

A numeral, declared constant, or other expression that the compiler can evaluate without executing your program. It must be of an ordinal type compatible with selectorExpression. Thus 7, True, 4 + 5 * 3, 'A', and Integer('A') can all be used as caseLists, but variables and most function calls cannot. (A few built-in functions like Hi and Lo can occur in a caseList. See Constant expressions.)
     A subrange having the form First..Last, where First and Last both satisfy the criterion above and First is less than or equal to Last.

A list having the form item1, ..., itemn, where each item satisfies one of the criteria above.

Each value represented by a caseList must be unique in the case statement; subranges and lists cannot overlap. A case statement can have a final else clause:

case selectorExpression of

  caseList1: statement1;
  ...
  caseListn: statementn;
else
  statement;
end

When a case statement is executed, at most one of its constituent statements is executed. Whichever caseList has a value equal to that of selectorExpression determines the statement to be used. If none of the caseLists has the same value as selectorExpression, then the statement in the else clause (if there is one) is executed.
The case statement

case I of

  1..5: Caption := 'Low';
  6..9: Caption := 'High';
  0, 10..99: Caption := 'Out of range';
else
  Caption := '';
end;

is equivalent to the nested conditional

if I in [1..5] then

  Caption := 'Low'
  else if I in [6..10] then
    Caption := 'High'
    else if (I = 0) or (I in [10..99]) then
      Caption := 'Out of range'
      else
        Caption := '';

Other examples of case statements:

case MyColor of

  Red: X := 1;
  Green: X := 2;
  Blue: X := 3;
  Yellow, Orange, Black: X := 0;
end;

case Selection of

  Done: Form1.Close;
  Compute: CalculateTotal(UnitCost, Quantity);
else
  Beep;
end;
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
EpsylonCommented:
Now I understand what you mean. The problem is that the implementation highly depends on what you want to do. You even may not have to use recursion.

Sorry, I can't give an example without more information...
0
 
joyriderAuthor Commented:
RickHalle, tnx for the big info about cases but i knew what it was :)
but they actually do use trees.

Epsylon, i was trying to implement it to a simple game i was making to learn how these trees worked and should be implemented so i could use it in later projects.

i want to make a tree for the pc player mainly where he had to put his piece on the board.

i was thinking on something like this (i don't know if it is a good way cause i never tested it):
1) check if other play can do a 4 on a row if so put piece there
else
2) check if computer can make 4 on row if so put piece there
else
3) check if computer can make 3 on row if so put piece there
else
4) check if computer can make 2 on row if so put piece there
else
choose random place where to put piece

i know this is prefectly possible just using if's but i wanted to give these "decision trees" as shot
0
 
Mohammed NasmanSoftware DeveloperCommented:
Hello

  This page contain some programs to Solve Decision Trees
but the code in pascal and c

 http://www.public.asu.edu/~kirkwood/DAStuff/code/scode.htm
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now