[英]Sharing an AVL Tree Between Several Classes
I have an AVL tree implemented. 我实现了AVL树。
So, let's say I have two classes, one that parses through an XML of different types of Apples and another the parses through different types of Oranges. 因此,假设我有两个类,一个类通过不同类型的Apple的XML解析,另一个类通过不同类型的Orange进行解析。
If I instantiate the tree in the Apples_Parser.cs
class, how can I use that same tree to load the different types of oranges which would be loaded into the tree in from the Oranges_Parser.cs
class? 如果我在Apples_Parser.cs
类中实例化该树,那么如何使用同一棵树来加载从Oranges_Parser.cs
类加载到树中的不同类型的桔子?
This isn't homework, I've just generalized the question to make it easier (no code to provide yet, I'm at a loss on where to begin) 这不是家庭作业,我只是概括了这个问题以使其变得更容易(尚无代码可提供,我对从何处开始感到困惑)
You can use a helper class, that contains an instance of the tree, then use that instance in both Apple_parser
and Oranges_Parser
classes. 您可以使用一个包含树实例的帮助程序类,然后在Apple_parser
和Oranges_Parser
类中使用该实例。
public static class AvlFactory
{
private static AvlTree _avlTree;
public static AvlTree AvlTree{ get {return _avlTree; }}
public static void InitializeTree()
{
_avlTree = new AvlTree();
}
}
Then in Apples_Praser
or Oranges_Praser
class: 然后在Apples_Praser
或Oranges_Praser
类中:
if(AvlFactory.AvlTree == null)
{
AvlFactory.InitializeTree();
}
//Codes to load data to the tree
In this way whichever parser that used first will instantiate the tree and the other just adds data to it. 通过这种方式,首先使用的解析器将实例化该树,而其他解析器仅向其添加数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.