ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Recursive Classes - Design Pattern needed (https://www.excelbanter.com/excel-programming/387401-recursive-classes-design-pattern-needed.html)

syswizard

Recursive Classes - Design Pattern needed
 
I've got a class defined that instantiates itself internally. (It's a
simple binary tree implementation). Are there any OO gurus here can
help me to design the proper class structure so that I can track the
item count (# of nodes in the b-tree) and other statistics that would
apply to all instantiations ?
Do I just need to create a separate "parent" class that is
instantiated on the first instance of the bintree class ? I would
guess the parent class would house a Collection to track each instance
of the bintree, as well as the total count of the nodes.....correct ?
Am I on the right track ?


Tushar Mehta[_3_]

Recursive Classes - Design Pattern needed
 
In article om,
says...
I've got a class defined that instantiates itself internally. (It's a
simple binary tree implementation). Are there any OO gurus here can
help me to design the proper class structure so that I can track the
item count (# of nodes in the b-tree) and other statistics that would
apply to all instantiations ?
Do I just need to create a separate "parent" class that is
instantiated on the first instance of the bintree class ? I would
guess the parent class would house a Collection to track each instance
of the bintree, as well as the total count of the nodes.....correct ?
Am I on the right track ?


You could do what you propose. But for (almost) the same amount of
programming, I would add the information to each node. Now, you will
know how many elements each and every node contains. Something along
the lines of the pseudocode:

class BinTreeNode:

Dim Left as BinTreeNode, Right as BinTreeNode, _
Val, ChildCount as Integer

private function checkAndAdd(CurrNode as BinTreeNode, NewVal) _
as Boolean
if CurrNode is nothing then
set currnode=new BinTreeNode
with CurrNode
.val = NewVal
.ChildCount=0
end with
checkAndAdd=true
else
checkAndAdd=CurrNode.NewNodeAdded(NewVal)
end if
public function NewNodeAdded(NewVal) as Boolean
if me.val NewVal then NewNodeAdded=checkAndAdd(me.left,NewVal) _
else NewNodeAdded=checkAndAdd(me.right,NewVal)
if NewNodeAdded then me.ChildCount += 1
end function

Similarly, any delete function would have to subtract one from the
ChildCount property.


All times are GMT +1. The time now is 05:33 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com