python数据结构学习笔记-2016-10-28-03-用链表实现多项式ADT

        6.6 多项式

        6.6.1 多项式操作

        6.6.2 多项式ADT

  • Polynomial():构造多项式实例,每一项系数初始化为零;
  • Polynomial(degree, coefficient):构造多项式实例,次数为degree和系数为coefficient 的单项式;
  • degree():返回多项式的最高次数;
  • getitem(degree):返回多项式中次数为degree的项的系数;
  • evaluate(scalar):代入scalar求值;
  • add(other):多项式加法;
  • substract(other):多项式减法;
  • multiply(other):多项式乘法。
        6.6.3 实现

        使用排序链表,从大到小排列,每一个结点储存多项式对应项的次数和项。在链表中额外加入了尾指针,使得之后追插项变得较为便利。

#-*-coding: utf-8-*-

# 使用排序链表实现多项式ADT

class Polynomial(object):
    def __init__(self, degree = None, coefficient = None):
        if degree is None:
            self._polyHead = None
        else:
            self._polyHead = _PolyTermNode(degree, coefficient)
        self._polyTail = self._polyHead

    # 返回多项式的次数
    def degree(self):
        if self._polyHead is None:
            return -1
        else:
            return self._polyHead.degree

    # 返回多项式中指定次数项的系数
    def __getitem__(self, degree):
        assert self.degree() >= 0, "Operation not permitted on an empty polynomial."
        curNode = self._polyHead
        while curNode is not None and curNode.degree >= degree:
            curNode = curNode.next
        
        if curNode is None or curNode.degree != degree:
            return 0.0
        else:
            return curNode.coefficient

    # 求值
    def evaluate(self, scalar):
        assert self.degree() >= 0, "Only non-empty polynomials can be evaluated."
        result = 0.0
        curNode = self._polyHead
        while curNode is not None:
            result += curNode.coefficient * (scalar ** curNode.degree)
            curNode = curNode.next
        return result

    # 多项式加法,类似于两个排序列表的合并
    def __add__(self, other):
        assert self.degree() >= 0 and other.degree() >= 0, "Addition only allowed on non-empty polynomials."
        newPoly = Polynomial()
        nodeA = self._polyHead
        nodeB = other._polyHead

        while nodeA is not None and nodeB is not None:
            if nodeA.degree > nodeB.degree:
                degree = nodeA.degree
                coefficient = nodeA.coefficient
                nodeA = nodeA.next
            elif nodeA.degree < nodeB.degree:
                degree = nodeB.degree
                coefficient = nodeB.coefficient
                nodeB = nodeB.next
            else:
                degree = nodeA.degree
                coefficient = nodeA.coefficient + nodeB.coefficient
                nodeA = nodeA.next
                nodeB = nodeB.next
            self._appendTerm(degree, coefficient)

        while nodeA is not None:
            self._appendTerm(nodeA.degree, nodeA.coefficient)
            nodeA = nodeA.next

        while nodeB is not None:
            self._appendTerm(nodeB.degree, nodeB.coefficient)
            nodeB = nodeB.next

        return newPoly
        
    # 多项式减法
    def __sub__(self, other):
        pass

    # 辅助方法, 多项式与单项式相乘
    def _termMultiply(self, termNode):
        newPoly = Polynomial()
        
        curr = self._polyHead
        while curr is not None:
            newDegree = curr.degree + termNode.degree
            newCoeff = curr.coefficient + termNode.coefficient
            
            newPoly._appendTerm(newDegree, newCoeff)
            curr = curr.next
        return newPoly

    # 多项式乘法,实质就是一个多项式分别于另一个多项式的每一项相乘,再将结果相加,但并不是十分有效
    def __mul__(self, other):
        assert self.degree() >= 0 and other.degree() >= 0, "Multiplication only allowed on non-empty polynomials."
        
        node = self._polyHead
        newPoly = Polynomial()
        while node is not None:
            newPoly += other._termMultiply(node)
            node = node.next
        return newPoly
        

    # 辅助方法,后插,排序体现在什么地方?
    def _appendTerm(self, degree, coefficient):
        if coefficient != 0.0:
            newTerm = _PolyTermNode(degree, coefficient)
            if self._polyHead is None:
                self._polyHead = newTerm
            else:
                self._polyTail.next = newTerm
            self._polyTail = newTerm

class _PolyTermNode(object):
    def __init__(self, degree, coefficient):
        self.degree = degree
        self.coefficient = coefficient
        self.next = None

        degree()是返回多项式的最高次数,即返回链表的首元结点中储存的次数。

        evaluate()需对链表进行遍历,其时间复杂度是O(n)。

        多项式的加法可以通过__getitem__()来获取相应次数的系数,再相加的方法,但这样效率不高,时间复杂度是O(n²)。书中采用了更为有效的方法,即将两个排序链表合并的方法,其时间复杂度是O(n)。

python数据结构学习笔记-2016-10-28-03-用链表实现多项式ADT_第1张图片

         多项式的乘法,可以看成是一个多项式与另一个多项式的每一项相乘,然后再将结果相加,所以程序中先定义了多项式与单项式相乘的辅助方法,在此基础上实现多项式乘法。

       

你可能感兴趣的:(python数据结构)