AVLTree: Tree_Last
issueid=533 22-08-2017 11:30
Member
Number of reported issues by primo: 7
AVLTree: Tree_Last

Thanks Eros for referring to the AVLTree module in my last post, i find it very good and very speedy, i'm still experimenting with it.
not sure if the following is a bug or not
i have used the loop index as keys, and the result of the following code something like this:
1-- 21
2-- 20
3-- 21
4-- 30
5-- 13
6-- 13
7-- 29
8-- 29
9-- 17
10-- 26

First node Key/data is: 1 21
Last node Key/data is: 9 17

the last node refer to the 9 17 while it should refer to 10 26 unless i miss something
Uses "console"

Dim  i  As Long
String sData

'Creating an new AVL Tree, Keys are case sensitive
DWord pTree = Tree_New 'AVL Tree pointer: pTree

Randomize
For i = 1 To 10
  Tree_Set(pTree, Str$(i), Rnd(10,30))
Next

For i = 1 To 10
  sData = Tree_Get(pTree, Str$(i))
  PrintL Str$(i)& "-- " & sData
Next

DWord pRoot, pLast

pRoot = Tree_First(pTree)
pLast = Tree_Last(pTree)

PrintL
PrintL "First node Key/data is:", Tree_GetKey(pRoot) , Tree_GetData(pRoot) 
PrintL "Last  node Key/data is:", Tree_GetKey(pLast) , Tree_GetData(pLast)

Tree_Free(pTree)

WaitKey
Edit:
it works well if we choose the key from 10 and up. but if we choose the key from 1 and up sequently the Tree_Last stop at 9. so i guess the key should be at least 2 characters
Issue Details
Issue Number 533
Project thinBasic
Category Unknown
Status Not a Bug
Priority Unknown
Affected Version 1.10.2
Fixed Version (none)
Milestone (none)
Users able to reproduce bug 0
Users unable to reproduce bug 0
Assigned Users (none)
Tags (none)




22-08-2017 17:59
Member
now i think the problem is only with Tree_First and Tree_Last
try in the above code
For i = 90 To 110 line 10 and line 14

will output:
First node Key/data is: 100 number
Last node Key/data is: 99 number

08-10-2017 18:24
thinBasic author
Checking

08-10-2017 18:33
thinBasic author
Ciao Primo,

it is not a bug. Keys are strings so key "10" comes second just after "1"
The sequence you are showing is the inserting sequence, not the sequence in which data is stored into the tree.
If you want to have numeric sequence, you need to format keys something like: 00001, 00002, ...

Here an example and its output.
Ciao
Eros

Uses "console"
 
Dim  i  As Long
String sData
 
'Creating an new AVL Tree, Keys are case sensitive
DWord pTree = Tree_New 'AVL Tree pointer: pTree
 
Randomize
For i = 1 To 10
  Tree_Set(pTree, Str$(i), Rnd(10,30))
Next
 
For i = 1 To 10
  sData = Tree_Get(pTree, Str$(i))
  PrintL Str$(i)& "-- " & sData
Next
 
DWord pRoot, pLast, pNode
 
pRoot = Tree_First(pTree)
pLast = Tree_Last(pTree)


PrintL
PrintL "First node Key/data is:", Tree_GetKey(pRoot) , Tree_GetData(pRoot) 
PrintL "Last  node Key/data is:", Tree_GetKey(pLast) , Tree_GetData(pLast)


printl "---Moving sequentialy from FIRST to LAST"
pNode = Tree_First(pTree)
while pNode
  PrintL "Node Key/data is:", Tree_GetKey(pNode) , Tree_GetData(pNode) 
  pNode = Tree_Next(pNode)
Wend


printl "---Moving sequentialy from LAST to FIRST"
pNode = Tree_Last(pTree)
while pNode
  PrintL "Node Key/data is:", Tree_GetKey(pNode) , Tree_GetData(pNode) 
  pNode = Tree_Prev(pNode)
Wend


 
Tree_Free(pTree)
 
WaitKey
1-- 12
2-- 20
3-- 12
4-- 27
5-- 18
6-- 10
7-- 14
8-- 10
9-- 22
10-- 14


First node Key/data is: 1 12
Last node Key/data is: 9 22
---Moving sequentialy from FIRST to LAST
Node Key/data is: 1 12
Node Key/data is: 10 14
Node Key/data is: 2 20
Node Key/data is: 3 12
Node Key/data is: 4 27
Node Key/data is: 5 18
Node Key/data is: 6 10
Node Key/data is: 7 14
Node Key/data is: 8 10
Node Key/data is: 9 22
---Moving sequentialy from LAST to FIRST
Node Key/data is: 9 22
Node Key/data is: 8 10
Node Key/data is: 7 14
Node Key/data is: 6 10
Node Key/data is: 5 18
Node Key/data is: 4 27
Node Key/data is: 3 12
Node Key/data is: 2 20
Node Key/data is: 10 14
Node Key/data is: 1 12

08-10-2017 18:34
thinBasic author
I close as "not a bug" but feel free to discuss further.
Ciao
Eros

08-10-2017 19:18
thinBasic author
Maybe I can add the feature to have numeric keys.
Will think about how to do without doubling the keywords

+ Reply