• 广义表(2)


    计算广义表原子结点个数

    public int CountAtom()
    {
        return CountAtom(_Root);
    }
    
    public int CountAtom(GLNode<char> node)
    {
        int i = 0;
        if (node != null)
        {
            switch (node.Type)
            {
                case NodeType.Atom:
                    i++;
                    break;
                case NodeType.List:
                    i+=CountAtom(node.Item as GLNode<char>);
                    break;
            }
            
            return i + CountAtom(node.Next);
        }
        return 0;
    }

    替换相同原子

    public void Replace(char oldchar, char newchar)
    {
        Replace(_Root, oldchar, newchar);
    }
    
    public void Replace(GLNode<char> node,char oldchar,char newchar)
    {
        if (node != null)
        {
            switch (node.Type)
            {
                case NodeType.Atom:
                    if ((char)node.Item == oldchar)
                        node.Item = newchar;
                    break;
                case NodeType.List:
                    Replace(node.Item as GLNode<char>, oldchar, newchar);
                    break;
            }
    
            Replace(node.Next,oldchar,newchar);
        }
    }

    删除相同原子

    public GLNode<char> DeleteAtom(char key)
    {
        return DeleteAtom(_Root, key);
    }
    
    public GLNode<char> DeleteAtom(GLNode<char> node, char key)
    {
        GLNode<char> current = node;
        if (current != null)
        {
            switch (current.Type)
            {
                case NodeType.Atom:
                    if ((char) current.Item == key)
                        return DeleteAtom(current.Next, key);
                    break;
                case NodeType.List:
                    DeleteAtom(current.Item as GLNode<char>, key);
                    break;
            }
            current.Next = DeleteAtom(current.Next, key);
            _Root = current;
            return current;
        }
        return null;
    }

    广义表逆置

    将头与尾交换,如a,b,c

    则a与b,c;b与c交换,也是一个递归过程从尾交换开始,

    public GLNode<char> Reverse(GLNode<char> node)
    {
        if (IsEmpty(node)) return null;
    
        switch (node.Type)
        {
            case NodeType.Atom:
                if (node.Next != null)
                    node = Append(Reverse(GetTail(node)), GetHead(node));
                break;
            case NodeType.List:
                node = Append(Reverse(GetTail(node)), new GLNode<char>() { Item = Reverse((GLNode<char>)node.Item), Type = NodeType.List });
                break;
        }
    
        return node;
    }
    
    public GLNode<char> Append(GLNode<char> head, GLNode<char> tail)
    {
        if (IsEmpty(head)) return tail;
    
        var pre = head;
        var cur = head.Next;
    
        while (cur != null)
        {
            pre = cur;
            cur = cur.Next;
        }
    
        pre.Next = tail;
    
        return head;
    }

    判断两个广义表是否完全相等

  • 相关阅读:
    「SHOI2015」脑洞治疗仪
    LOJ 数列分块入门 8
    CF932F Escape Through Leaf
    NOIP2021游记总结
    [HEOI2016/TJOI2016]序列
    【模板】动态树(Link Cut Tree)
    LG P2839 [国家集训队]middle
    JZOJ 7377.欢乐豆
    JZOJ 7392. 【2021.11.17NOIP提高组联考】数 (ds)
    LOJ 数列分块入门 6
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1942009.html
Copyright © 2020-2023  润新知