树形结构的反馈/批判

时间:2013-05-23 19:01:33

标签: c# design-patterns data-structures recursion tree

所以我已经写了几天这个数据结构,现在我真的好奇它实际上是什么,并对我的逻辑进行一些批评。

当HEAD节点包含至少一个引用的相同类型的节点时,存在基于此用法的分支。

结构的目的是具有按类型排列的分支。分支上的每个节点都引用了分支上的下一个节点(始终属于同一类型)和Subdata分支的入口点。在这种情况下,子数据是继承自AchievementNode的对象的实例。当添加子数据并且它是该分支上的第一个子类时,它应用了HEAD标记,此外,它还有一个标记,其中包含所包含数据类型的元数据(绕过调用类型)。

实现:

public abstract class AchievementNode : ScriptableObject
{
public enum NodeTypes
{
    NONE   = 0x0,
    HEAD   = 0x1,
    TAIL   = 0x2,
    TYPE   = 0x4,
    DATA   = 0x8,
    LEVEL  = 0x16,
    GLOBAL = 0x32
}

public NodeTypes nodeType;

public AchievementNode nextOfType; 

public AchievementNode headOfSubnode; 

public void OnEnable ()
{
    hideFlags = HideFlags.HideAndDontSave;
}

public virtual void Init(NodeTypes type, int enumData)
{
    nodeType = type;
}

protected void AddNode(NodeTypes type, AchievementNode originNode, AchievementNode newNode)
{
    //Create SubNode branch notch when types mismatch.
    if((originNode.nodeType & type) != type)
    {
        //If Has subNode Data Run to the end and assign new node
        if(originNode.headOfSubnode!=null)
        {
            newNode.nodeType = type | NodeTypes.TAIL;
            AppendToTail(type,GetEndOfBranch(originNode.headOfSubnode),newNode);
        }//Search for proper SubNodeTypes then add. Wicked Recursion warning here...
        else if((originNode.headOfSubnode.nodeType & type) != type)
        {
            Debug.LogError("Do Gnarly Search To Find!");
            return;
        }//Doesn't have subnode... add new Subnode.
        else
        {
            newNode.nodeType = type | NodeTypes.HEAD | NodeTypes.TAIL;
            originNode.headOfSubnode = newNode;
        }
    }
    else
    {
        //Add to the current branch
        newNode.nodeType = type | NodeTypes.TAIL;
        AppendToTail(type,GetEndOfBranch(originNode),newNode);

    }
}

private void AppendToTail(NodeTypes type,AchievementNode tailNode, AchievementNode newNode)
{
    if((tailNode.nodeType & NodeTypes.HEAD) == NodeTypes.HEAD)
    {
        tailNode.nodeType = tailNode.nodeType | type;
    }
    else
    {
        tailNode.nodeType = type;
    }
    tailNode.nextOfType = newNode;
}

protected AchievementNode GetEndOfBranch(AchievementNode currentNode)
{
    //Special Case where Node is HEAD and TAIL.
    if((currentNode.nextOfType.nodeType & NodeTypes.TAIL) != NodeTypes.TAIL)
    {
        return GetEndOfBranch(currentNode.nextOfType);  
    }
    else
    {
        return currentNode; 
    }
}

protected void SetType(NodeTypes type)
{
    nodeType = type;
}

protected virtual AchievementNode FindInHierarchy(NodeTypes nodeCheck, AchievementNode currentNode)
{
    if(currentNode == null)
    {
        return null;
    }
    else if((currentNode.nodeType & nodeCheck) == nodeCheck)
    {
        return currentNode; 
    }
    else
    {
        return FindInHierarchy(nodeCheck,currentNode.nextOfType);
    }
}

}

感谢您花时间检查一下。那意义重大。

0 个答案:

没有答案