BinaryTree - Module
Posted: Sat Aug 20, 2016 9:02 pm
Hello everyone,
A BinaryTree Module example based on Guimauve's code here
Best regards
StarBootics
A BinaryTree Module example based on Guimauve's code here
Best regards
StarBootics
Code: Select all
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : BinaryTree - Module
; File Name : BinaryTree - Module.pb
; File version : 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 20-08-2016
; Last Update : 20-08-2016
; PureBasic code : V5.50
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; This code was originally created by Guimauve to create a BinaryTree
; Data Structure.
;
; See : http://www.purebasic.fr/english/viewtopic.php?f=12&t=24578
;
; I deserve credit only to convert the original code into a Module.
;
; This code is free to be use where ever you like but you use it at
; your own risk.
;
; The author can in no way be held responsible for data loss, damage or
; other annoying situations that may occur.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
DeclareModule BinaryTree
Declare Reset()
Declare AddNode(P_Word.s)
CompilerIf #PB_Compiler_Debugger
Declare Debugging()
CompilerEndIf
EndDeclareModule
Module BinaryTree
Structure Node
Word.s
WordCounter.l
Child.i[2]
EndStructure
Structure Instance
NodeCount.l
RootNode.Node
EndStructure
Global Instance.Instance
Procedure Private_ResetNode(*NodeA.Node)
If *NodeA <> #Null
For ChildID = 0 To 1
Private_ResetNode(*NodeA\Child[ChildID])
Next
Debug "ResetNode called for : " + *NodeA\Word
*NodeA\Word = ""
*NodeA\WordCounter = 0
For ChildID = 0 To 1
If *NodeA\Child[ChildID] <> #Null
FreeMemory(*NodeA\Child[ChildID])
*NodeA\Child[ChildID] = 0
EndIf
Next
EndIf
EndProcedure
Procedure Reset()
Instance\NodeCount = 0
Private_ResetNode(Instance\RootNode)
EndProcedure
Procedure Private_AddNode(*NodeA.Node, P_Word.s)
If Instance\NodeCount = 0
Instance\NodeCount = Instance\NodeCount + 1
*NodeA\Word = P_Word
*NodeA\WordCounter = 1
*NodeA\Child[0] = #Null
*NodeA\Child[1] = #Null
Else
If *NodeA = #Null
*NodeA.Node = AllocateMemory(SizeOf(Node))
If *NodeA <> #Null
Instance\NodeCount = Instance\NodeCount + 1
*NodeA\Word = P_Word
*NodeA\WordCounter = 1
*NodeA\Child[0] = #Null
*NodeA\Child[1] = #Null
Else
MessageRequester("BinaryTree fatal error", "Impossible to allocate node !")
End
EndIf
ElseIf UCase(*NodeA\Word) = UCase(P_Word)
*NodeA\WordCounter = *NodeA\WordCounter + 1
ElseIf UCase(P_Word) < UCase(*NodeA\Word)
*NodeA\Child[0] = Private_AddNode(*NodeA\Child[0], P_Word)
ElseIf UCase(P_Word) > UCase(*NodeA\Word)
*NodeA\Child[1] = Private_AddNode(*NodeA\Child[1], P_Word)
EndIf
EndIf
ProcedureReturn *NodeA
EndProcedure
Procedure AddNode(P_Word.s)
Private_AddNode(Instance\RootNode, P_Word)
EndProcedure
CompilerIf #PB_Compiler_Debugger
Procedure Private_DebuggingNode(*NodeA.Node)
If *NodeA <> #Null
Private_DebuggingNode(*NodeA\Child[0])
If *NodeA\WordCounter = 1
Debug *NodeA\Word + " =---> " + Str(*NodeA\WordCounter) + " time"
Else
Debug *NodeA\Word + " =---> " + Str(*NodeA\WordCounter) + " times"
EndIf
Private_DebuggingNode(*NodeA\Child[1])
EndIf
EndProcedure
Procedure Debugging()
Private_DebuggingNode(Instance\RootNode)
EndProcedure
CompilerEndIf
EndModule
CompilerIf #PB_Compiler_IsMainFile
Dim MyHeros.s(14)
MyHeros(00) = "Zoro"
MyHeros(01) = "Fred"
MyHeros(02) = "Yoda"
MyHeros(03) = "Freak"
MyHeros(04) = "Morpheus"
MyHeros(05) = "Fred"
MyHeros(06) = "Freak"
MyHeros(07) = "Zoro"
MyHeros(08) = "Hercule"
MyHeros(09) = "Yoda"
MyHeros(10) = "Luke"
MyHeros(11) = "Anakin"
MyHeros(12) = "Mace Windu"
MyHeros(13) = "Yoda"
MyHeros(14) = "Obiwan"
For Index = 0 To 14
BinaryTree::AddNode(MyHeros(Index))
Next
CompilerIf #PB_Compiler_Debugger
BinaryTree::Debugging()
CompilerEndIf
Debug ""
Debug "; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<"
Debug ""
BinaryTree::Reset()
CompilerEndIf
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<