Python数据结构(栈, 队列, 二叉树, 链表, 图)

Python栈

class Stack():
    def __init__(st,size):
        st.stack=[];
        st.size=size;
        st.top=-1;

    def push(st,content):
        if st.Full():
            print "Stack is Full!"
        else:
            st.stack.append(content)
            st.top=st.top+1

    def out(st):
        if st.Empty():
            print "Stack is Empty!"
        else:
            st.top=st.top-1
    def Full(st):
        if  st.top==st.size:
            return True
        else:
            return False
    def Empty(st):
        if st.top==-1:
            return True
        else:
            return False

Python队列

class Queue():
    def __init__(qu,size):
        qu.queue=[];
        qu.size=size;
        qu.head=-1;
        qu.tail=-1;
    def Empty(qu):
        if qu.head==qu.tail:
            return True
        else:
            return False
    def Full(qu):
        if qu.tail-qu.head+1==qu.size:
            return True
        else:
            return False
    def enQueue(qu,content):
        if qu.Full():
            print "Queue is Full!"
        else:
            qu.queue.append(content)
            qu.tail=qu.tail+1
    def outQueue(qu):
        if qu.Empty():
            print "Queue is Empty!"
        else:
            qu.head=qu.head+1

Python二叉树

class TRee():

    def __init__(self, leftjd=0, rightjd=0, data=0):
        self.leftjd = leftjd
        self.rightjd = rightjd
        self.data = data


class Btree():

    def __init__(self, base=0):
        self.base = base

    def empty(self):
        if self.base is 0:
            return True
        else:
            return False

    def qout(self, jd):
        """前序遍历"""
        if jd == 0:
            return
        print jd.data
        self.qout(jd.leftjd)
        self.qout(jd.rightjd)

    def mout(self, jd):
        """中序遍历"""
        if jd == 0:
            return
        self.mout(jd.leftjd)
        print jd.data
        self.mout(jd.rightjd)

    def hout(self, jd):
        """后序遍历"""
        if jd == 0:
            return
        self.hout(jd.leftjd)
        self.hout(jd.rightjd)
        print jd.data

二叉树使用

        7
8              9

>>> jd1 = TRee(data=8)
>>> jd2 = TRee(data=9)
>>> base = TRee(jd1,jd2,7)
>>> x = Btree(base)
#前序遍历
>>> x.qout(x.base)
7
8
9
#中序遍历
>>> x.mout(base)
8
7
9
#后序遍历
>>> x.hout(base)
8
9
7
>>> 

Python链表

class jd():
    def __init__(self,data):
        self.data=data
        self.next=None
    
class Linklist():
    def __init__(self,jd2):
        self.head=jd2
        self.head.next=None
        self.tail=self.head

    def add(self,jd2):
        self.tail.next=jd2
        self.tail=self.tail.next

    def view(self):
        jd2=self.head
        linkstr=""
        while jd2 is not None:
            if jd2.next is not None:
                linkstr=linkstr+str(jd2.data)+"-->"
            else:
                linkstr+=str(jd2.data)
            jd2=jd2.next
        print linkstr
    原文作者:后山顾主
    原文地址: https://www.jianshu.com/p/3d4963407265
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞