January 16, 2021, 06:07:59 AM

### Author Topic: [bb] Binary decision tree by xMicky [ 1+ years ago ]  (Read 716 times)

#### BlitzBot

• Jr. Member
• Posts: 1
##### [bb] Binary decision tree by xMicky [ 1+ years ago ]
« on: June 29, 2017, 12:28:43 AM »
Title : Binary decision tree
Author : xMicky
Posted : 1+ years ago

Description : Making decisions depending on the content of an integer is fastest using a binary search method, if the probability for each possible content of the variable
is nearly equal. Because coding such a tree from hand is awful, I wrote a code generator to do the job.

Code :
Code: BlitzBasic
1. ;-----------------------------------------------------------------------------------------------------
2. ; USER SETTINGS :
3.
4. ; where the generated code to store in:
5. FileName\$ ="C:Tmp.bb"
6.
7. ; a decision tree for at least how many decisions you want:
8. maxDecs =32
9.
10. ; what variable should be asked to make the decisions:
11. varName\$ ="z"
12.
13. ; END OF USER SETTINGS
14. ;-----------------------------------------------------------------------------------------------------
15. Global curlevel
16.
17. tmpDecs =maxDecs -1
18. While tmpDecs >1
19.   tmpDecs =tmpDecs /2
20.   count=count +1
21. Wend
22. Dim globCount(count +1)
23.
24. file =WriteFile(FileName\$)
25. writeCodeLines(file, varName\$, 2^count, 2 *2^count, 0)
26.
27. End
28. ;-----------------------------------------------------------------------------------------------------
29. Function writeCodeLines(fileHandle, varName\$, comparedUppper, compareStep, insertionlevel)
30.
31.   curCode\$ =String\$(" ", insertionlevel) +"If " +varName\$ +" <" +Trim\$(Str\$(comparedUppper +globCount(curlevel) *compareStep)) +" Then"
32.   WriteLine fileHandle, curCode\$
33.   If comparestep =2 Then
34.     curCode\$ ="; " +Trim\$(Str\$(comparedUppper +globCount(curlevel) *compareStep -1)) +" -------------------------------------------------------------------------------------------------"
35.     WriteLine fileHandle, curCode\$
36.     curCode\$ =String\$(" ", insertionlevel) +"Else"
37.     WriteLine fileHandle, curCode\$
38.     curCode\$ ="; " +Trim\$(Str\$(comparedUppper +globCount(curlevel) *compareStep)) +" -------------------------------------------------------------------------------------------------"
39.     WriteLine fileHandle, curCode\$
40.     curCode\$ =String\$(" ", insertionlevel) +"End If"
41.     WriteLine fileHandle, curCode\$
42.     globCount(curlevel) =globCount(curlevel) +1
43.   Else
44.     globCount(curlevel) =globCount(curlevel) +1
45.     curlevel =curlevel +1
46.     writeCodeLines(fileHandle, varName\$, comparedUppper /2, compareStep /2, insertionlevel +2)
47.
48.     curCode\$ =String\$(" ", insertionlevel) +"Else"
49.     WriteLine fileHandle, curCode\$
50.
51.     curlevel =curlevel +1
52.     writeCodeLines(fileHandle, varName\$, comparedUppper /2, compareStep /2, insertionlevel +2)
53.
54.     curCode\$ =String\$(" ", insertionlevel) +"End If"
55.     WriteLine fileHandle, curCode\$
56.   End If
57.
58.   curlevel =curlevel -1
59.
60.   Return
61.
62. End Function
63. ;-----------------------------------------------------------------------------------------------------