-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pb
110 lines (105 loc) · 4.1 KB
/
main.pb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
;**********************************************************************************************************************
;* PROJECT : AVL_TREE
;* PROCESS : manage a avl tree
;* FILE : main.pbi
;* CONTENT : main program
;* AUTHOR : microdedWeb
;* DATE : 2019/12/18
;**********************************************************************************************************************
;* IMPORT
;**********************************************************************************************************************
EnableExplicit
XIncludeFile "lib/avl_tree.pbi"
;**********************************************************************************************************************
;* STRUCTURES / DEFINE / ENUM
;**********************************************************************************************************************
Structure people
fname.s
sname.s
id.l
EndStructure
#DATA_FILE = "import/data.csv"
;**********************************************************************************************************************
;* PROTOTYPE OF FUNCTION
;**********************************************************************************************************************
Declare compare(*a.people,*b.people)
Declare load_data()
Declare ask()
Declare free(*d.people)
;**********************************************************************************************************************
;* GLOBAL VARIABLES
;**********************************************************************************************************************
Global myTree.AVL_TREE::tree = AVL_TREE::new_tree(@compare())
;**********************************************************************************************************************
;* RUN PROGRAM
;**********************************************************************************************************************
OpenConsole("AVL Tree")
EnableGraphicalConsole(1)
PrintN("Please wait we load above 450.000 records, that can be take some time")
PrintN("For maximize performs please disable the debugger :)")
load_data()
ask()
;**********************************************************************************************************************
;* FUNCTIONS
;**********************************************************************************************************************
Procedure compare(*a.people,*b.people)
If *a\id > *b\id : ProcedureReturn 1 : EndIf
If *a\id < *b\id : ProcedureReturn -1 : EndIf
ProcedureReturn 0
EndProcedure
Procedure load_data()
Protected d.s,*p.people,i
Protected t = ElapsedMilliseconds(),te.f,txt.s
; read data file
OpenFile(0,#DATA_FILE)
While Eof(0) = 0
d = ReadString(0)
If i > 0
*p = AllocateStructure(people)
*p\id = Val(StringField(d,1,";"))
*p\fname = StringField(d,3,";")
*p\sname = StringField(d,4,";")
myTree\add(*p)
EndIf
i + 1
Wend
te = (ElapsedMilliseconds() - t) / 1000
txt = Str(i-1)+" data are loaded in "+StrF(te,2)+" seconds (press any keys for next step)"
PrintN(txt)
Input()
EndProcedure
Procedure ask()
Protected choice.s
Protected.people *p,*f
Protected t.f,te.f
Repeat
ClearConsole()
Print("Enter a id of we look for (betwenn 100008 and 2345813) or empty for exit : ")
choice = Input()
If Len(choice)
; make find structure
*p = AllocateStructure(people)
*p\id = Val(choice)
t = ElapsedMilliseconds() ; for calculate the elapsed time
*f = myTree\search(*p)
te = ElapsedMilliseconds() - t
PrintN("Request finished in "+StrF(te,2)+" ms")
If *f
Print("First name : "+*f\fname + " Surname : "+*f\sname + " id : "+Str(*f\id))
Else
Print("This id : "+Str(*p\id)+" is not found ")
EndIf
FreeStructure(*p)
Input()
EndIf
Until Not Len(choice)
myTree\free(@free())
EndProcedure
Procedure free(*d.people)
FreeStructure(*d)
EndProcedure
; IDE Options = PureBasic 5.71 LTS (Windows - x64)
; CursorPosition = 38
; FirstLine = 33
; Folding = -
; EnableXP