Hello everyone
Sorry to re-open an old post but have re-worked this BinaryTree Module too see if it's possible to do it OOP style. There is the result :
Code:
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : BinaryTree - OOP
; File Name : BinaryTree - OOP.pb
; File version: 1.0.1
; Programming : OK
; Programmed by : StarBootics
; Date : 12-04-2020
; Last Update : 29-10-2020
; PureBasic code : V5.72
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
DeclareModule BinaryTreeNode
Interface BinaryTreeNode
GetWord.s()
GetWordCounter.l()
GetChild.i(ChildID.l)
SetWord(P_Word.s)
SetWordCounter(P_WordCounter.l)
SetChild(ChildID.l, P_Child.i)
Free()
EndInterface
Declare.i New()
EndDeclareModule
Module BinaryTreeNode
Structure Private_Members
VirtualTable.i
Word.s
WordCounter.l
Child.i[2]
EndStructure
Procedure.s GetWord(*This.Private_Members)
ProcedureReturn *This\Word
EndProcedure
Procedure.l GetWordCounter(*This.Private_Members)
ProcedureReturn *This\WordCounter
EndProcedure
Procedure.i GetChild(*This.Private_Members, ChildID.l)
ProcedureReturn *This\Child[ChildID]
EndProcedure
Procedure SetWord(*This.Private_Members, P_Word.s)
*This\Word = P_Word
EndProcedure
Procedure SetWordCounter(*This.Private_Members, P_WordCounter.l)
*This\WordCounter = P_WordCounter
EndProcedure
Procedure SetChild(*This.Private_Members, ChildID.l, P_Child.i)
*This\Child[ChildID] = P_Child
EndProcedure
Procedure Free(*This.Private_Members)
For ChildID = 0 To 1
If *This\Child[ChildID] <> #Null
Free(*This\Child[ChildID])
EndIf
Next
Debug "Free called for : " + *This\Word
FreeStructure(*This)
EndProcedure
Procedure.i New()
*This.Private_Members = AllocateStructure(Private_Members)
*This\VirtualTable = ?START_METHODS
ProcedureReturn *This
EndProcedure
DataSection
START_METHODS:
Data.i @GetWord()
Data.i @GetWordCounter()
Data.i @GetChild()
Data.i @SetWord()
Data.i @SetWordCounter()
Data.i @SetChild()
Data.i @Free()
END_METHODS:
EndDataSection
EndModule
DeclareModule BinaryTree
Interface BinaryTree
AddNode(Word.s)
Debugging()
Free()
EndInterface
Declare.i New()
EndDeclareModule
Module BinaryTree
Structure Private_Members
VirtualTable.i
NodeCount.l
Root.BinaryTreeNode::BinaryTreeNode
EndStructure
Procedure Private_AddNode(*This.Private_Members, *Node.BinaryTreeNode::BinaryTreeNode, Word.s)
If *This\NodeCount = 0
*This\NodeCount = *This\NodeCount + 1
*Node\SetWord(Word)
*Node\SetWordCounter(1)
*Node\SetChild(0, #Null)
*Node\SetChild(1, #Null)
Else
If *Node = #Null
*Node = BinaryTreeNode::New()
*This\NodeCount = *This\NodeCount + 1
*Node\SetWord(Word)
*Node\SetWordCounter(1)
*Node\SetChild(0, #Null)
*Node\SetChild(1, #Null)
ElseIf UCase(*Node\GetWord()) = UCase(Word)
*Node\SetWordCounter(*Node\GetWordCounter() + 1)
ElseIf UCase(Word) < UCase(*Node\GetWord())
*Node\SetChild(0, Private_AddNode(*This, *Node\GetChild(0), Word))
ElseIf UCase(Word) > UCase(*Node\GetWord())
*Node\SetChild(1, Private_AddNode(*This, *Node\GetChild(1), Word))
EndIf
EndIf
ProcedureReturn *Node
EndProcedure
Procedure AddNode(*This.Private_Members, Word.s)
Private_AddNode(*This, *This\Root, Word)
EndProcedure
Procedure Private_DebuggingNode(*Node.BinaryTreeNode::BinaryTreeNode)
If *Node <> #Null
Private_DebuggingNode(*Node\GetChild(0))
If *Node\GetWordCounter() = 1
Debug *Node\GetWord() + " =---> " + Str(*Node\GetWordCounter()) + " time"
Else
Debug *Node\GetWord() + " =---> " + Str(*Node\GetWordCounter()) + " times"
EndIf
Private_DebuggingNode(*Node\GetChild(1))
EndIf
EndProcedure
Procedure Debugging(*This.Private_Members)
Private_DebuggingNode(*This\Root)
EndProcedure
Procedure Free(*This.Private_Members)
If *This\Root <> #Null
*This\Root\Free()
EndIf
FreeStructure(*This)
EndProcedure
Procedure.i New()
*This.Private_Members = AllocateStructure(Private_Members)
*This\VirtualTable = ?START_METHODS
*This\Root = BinaryTreeNode::New()
ProcedureReturn *This
EndProcedure
DataSection
START_METHODS:
Data.i @AddNode()
Data.i @Debugging()
Data.i @Free()
END_METHODS:
EndDataSection
EndModule
CompilerIf #PB_Compiler_IsMainFile
MyTree.BinaryTree::BinaryTree = BinaryTree::New()
MyTree\AddNode("Zoro")
MyTree\AddNode("Fred")
MyTree\AddNode("Yoda")
MyTree\AddNode("Freak")
MyTree\AddNode("Morpheus")
MyTree\AddNode("Fred")
MyTree\AddNode("Freak")
MyTree\AddNode("Zoro")
MyTree\AddNode("Hercule")
MyTree\AddNode("Yoda")
MyTree\AddNode("Luke")
MyTree\AddNode("Anakin")
MyTree\AddNode("Mace Windu")
MyTree\AddNode("Yoda")
MyTree\AddNode("Obiwan")
MyTree\Debugging()
Debug ""
MyTree\Free()
CompilerEndIf
; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
From what I have learned, I can go now with an Octree for space partitioning for my game project.
Best regards.
StarBootics