• 广义表(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;
    }

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

  • 相关阅读:
    【20171227】json
    【20171224】文件操作
    【20171225】编解码
    【20171226】urllib
    【20171226】requests
    【错误集】编解码
    Python 基础概念——单例设计模式
    Python 继承
    python面向对象概述
    Python基础_函数的递归
  • 原文地址:https://www.cnblogs.com/Clingingboy/p/1942009.html
Copyright © 2020-2023  润新知