# Expression to Binary Tree

I need help for finish my binary tree

``````-- Expr To String ( Three Manipulation )

function setup()
--expr1="(sin(xt)-4.67)^Ter+tan(60/5)+7.98"
--expr1="la voiture de mon père est verte"
expr1="3+5*7-8*6"
stop=false
nb_parentheses_ouvertes=0
end

function draw()
background(40, 40, 50)
strokeWidth(5)
if stop==false then
print(expr1)
print("StrToLst()") ; StrToLst(expr1) ; AffLst()
print("LstToThree()") ; LstToThree(expr1) ; AffThree() ; AffOperandAttente()
stop=true
end
end

function StrToLst(expr)
Lst1={} ; Lst2={} ; i=1
::debut::
if TablePos({"["},string.sub(expr,i,i))~=0 then
table.insert(Lst1,"[") ; table.insert(Lst2,"array") end
if TablePos({"]"},string.sub(expr,i,i))~=0 then
table.insert(Lst1,"]") ; table.insert(Lst2,"array")end
if TablePos({"("},string.sub(expr,i,i))~=0 then
table.insert(Lst1,"(") ; table.insert(Lst2,"parenthese") end
if TablePos({")"},string.sub(expr,i,i))~=0 then
table.insert(Lst1,")") ; table.insert(Lst2,"parenthese") end
if TablePos({"sin("},string.sub(expr,i,i+4))~=0 then
table.insert(Lst1,"sin") ; table.insert(Lst2,"function") ; i=i+3 end
if TablePos({"cos("},string.sub(expr,i,i+4))~=0 then
table.insert(Lst1,"cos") ; table.insert(Lst2,"function") ; i=i+3 end
if TablePos({"tan("},string.sub(expr,i,i+4))~=0 then
table.insert(Lst1,"tan") ; table.insert(Lst2,"function") ; i=i+3 end
if TablePos({"^"},string.sub(expr,i,i))~=0 then
table.insert(Lst1,"^") ; i=i+1 end
if TablePos({"*"},string.sub(expr,i,i))~=0 then
table.insert(Lst1,"*") ; table.insert(Lst2,"operand") end
if TablePos({"/"},string.sub(expr,i,i))~=0 then
table.insert(Lst1,"/") ; table.insert(Lst2,"operand") end
if TablePos({"+"},string.sub(expr,i,i))~=0 then
table.insert(Lst1,"+") ; table.insert(Lst2,"operand") end
if TablePos({"-"},string.sub(expr,i,i))~=0 then
table.insert(Lst1,"-") ; table.insert(Lst2,"operand") end
if string.sub(expr,i,i)>="A" and string.sub(expr,i,i)<="z" then
if Lst1=={} then
table.insert(Lst1,string.sub(expr,i,i))
table.insert(Lst2,"variable")
else
if Lst2[#Lst2]=="variable" then
Lst1[#Lst1]=Lst1[#Lst1]..string.sub(expr,i,i)
else
table.insert(Lst1,string.sub(expr,i,i))
table.insert(Lst2,"variable")
end
end
end
if string.sub(expr,i,i)>="0" and string.sub(expr,i,i)<="9"
or string.sub(expr,i,i)=="." then
if Lst1=={} then
table.insert(Lst1,string.sub(expr,i,i))
table.insert(Lst2,"nombre")
else
if Lst2[#Lst2]=="nombre" then
Lst1[#Lst1]=Lst1[#Lst1]..string.sub(expr,i,i)
else
table.insert(Lst1,string.sub(expr,i,i))
table.insert(Lst2,"nombre")
end
end
end
i=i+1
if i<=#expr then goto debut end
end

function AffLst()
str1="" ; for i=1, #Lst1 do str1=str1..Lst1[i].."   " end ; print(str1)
str2="" ; for i=1, #Lst2 do str2=str2..Lst2[i].."   " end ; print(str2)
end

function LstToThree(expr)
Priorite={}
Priorite["+"]=1 ; Priorite["-"]=1 ; Priorite["*"]=2 ; Priorite["/"]=2 ; Priorite["^"]=3
Priorite["sin"]=4 ; Priorite["cos"]=4 ; Priorite["tan"]=4
Priorite["exp"]=4 ; Priorite["log"]=4 ; Priorite["ln"]=4
Priorite["neg"]=4 ; Priorite["inv"]=4 ; Priorite["sqrt"]=4 ; Priorite["sq"]=4
NbArg={}
NbArg["+"]=2 ; NbArg["-"]=2 ; NbArg["*"]=2 ; NbArg["/"]=2 ; NbArg["^"]=2
NbArg["sin"]=1 ; NbArg["cos"]=1 ; NbArg["tan"]=1
NbArg["exp"]=1 ; NbArg["log"]=1 ; NbArg["ln"]=1
NbArg["neg"]=1 ; NbArg["inv"]=1 ; NbArg["sqrt"]=1 ; Priorite["sq"]=1
Three={}
OperandAttente={}
nb_arg_dans_pile=0
for i=1, #Lst1 do
if Lst2[i]=="nombre" or Lst2[i]=="variable" then
table.insert(Three,Lst1[i]) ; nb_arg_dans_pile=nb_arg_dans_pile+1
end
if Lst2[i]=="operand" then
if nb_arg_dans_pile==NbArg[Lst1[i]]
then table.insert(Three,Lst1[i])
else table.insert(OperandAttente,Lst1[i]) end
end
end
end

function AffThree()
str="" ; for i=1, #Three do str=str..Three[i].."   " end ; print(str)
end

function AffOperandAttente()
str="" ; for i=1, #OperandAttente do str=str..OperandAttente[i].."   " end ; print(str)
end

function TablePos(tab,match)
pos=0
for i=1,#tab do
if tab[i]==match then pos=i end
end
return pos
end

function halt(...)
local n = select('#', ...)
local arg = {...}
fill(0, 53, 255, 255)
rect(820,730-n*30,160,n*30+10)
fill(252, 255, 0, 255)
for ligne=1, n do text(arg[ligne].." = ",840,740-ligne*30) end
end
``````