Thanks, switching to new/delete fix the issues. I never knew the real purpose of new/delete until now :-)fxm wrote:But on the other hand, you cannot only allocate (and deallocate at end) memory for creating an instance of tree_type, because this instance must also be built (and destroyed at the end), because it contains three descriptors of dynamic arrays:
...
Topic marked as solved. For completeness:
Code: Select all
function countInStr(text as string, char as string) as integer
dim as integer count = 0
for i as integer = 0 to len(text) - 1
if text[i] = asc(char) then count += 1
next
return count
end function
'-------------------------------------------------------------------------------
type tree_fwd as tree_type 'forward declaration
type sub_tree
dim as string path
dim as tree_fwd ptr pChild
end type
type tree_type
dim as string item(any)
dim as sub_tree subTree(any)
declare sub addItem(objectStr as string, sepChar as string)
declare sub show(depth as integer = 0)
declare sub cleanup()
end type
'object = item or tree
sub tree_type.addItem(objectStr as string, sepChar as string)
dim as integer i, j
if objectStr[0] <> asc(sepChar) then
print "Fail: Bad input"
exit sub
end if
if countInStr(objectStr, sepChar) = 1 then
dim as string itemStr = mid(objectStr, 2) 'all after "/"
dim as integer ub = ubound(item)
redim preserve item(ub + 1)
'find insert location
for i = 0 to ub
if itemStr < item(i) then exit for
next
'move other items below
for j = ub to i step -1
item(j + 1) = item(j)
next
'insert new item
item(i) = itemStr
else
dim as integer nextSepPos = instr(2, objectStr, sepChar)
dim as string pathStr = mid(objectStr, 2, nextSepPos - 2)
dim as string remObjStr = mid(objectStr, nextSepPos)
for i = 0 to ubound(subTree)
if subTree(i).path = pathStr then exit for
next
if i <= ubound(subTree) then 'match found
subTree(i).pChild->addItem(remObjStr, sepChar)
else
'add sub tree
dim as integer ub = ubound(subTree)
redim preserve subTree(ub + 1)
'find insert location
for i = 0 to ub
if pathStr < subTree(i).path then exit for
next
'move other items below
for j = ub to i step -1
subTree(j + 1) = subTree(j)
next
'insert new item
subTree(i).path = pathStr
subTree(i).pChild = new tree_type
subTree(i).pChild->addItem(remObjStr, sepChar)
end if
end if
end sub
sub tree_type.show(depth as integer = 0)
dim as string indentStr = string(depth * 2, " ") + "+ "
for i as integer = 0 to ubound(item)
color 14, 0 'item in yellow
print indentStr & item(i)
color 15, 0
next
for i as integer = 0 to ubound(subTree)
color 10, 0 'path in green
print indentStr & subTree(i).path
color 15, 0
subTree(i).pChild->show(depth + 1)
next
end sub
sub tree_type.cleanup()
for i as integer = 0 to ubound(subTree)
subTree(i).pChild->cleanup()
delete(subTree(i).pChild)
next
erase item, subTree
end sub
'-------------------------------------------------------------------------------
dim as string inputStr(...) = { _
"/itemE",_
"/path1/path2/itemA", _
"/path1/path2/itemC", _
"/path1/path2/itemB", _
"/path2/path2/itemX", _
"/path4/path5/path6/path7/itemQ", _
"/path4/path5/path8/path7/itemR", _
"/path4/path5/path6/path7/itemP", _
"/path3/itemC", _
"/path3/path9/itemY", _
"/path3/itemC", _
"/itemD"}
dim as tree_type tree
for i as integer = 0 to ubound(inputStr)
tree.addItem(inputStr(i), "/")
next
tree.show()
tree.cleanup()
tree.show()
print "end"
sleep