python-二叉树的创建及遍历(基于链式存储结构)

二叉树的基本操作(创建一棵二叉树,先序中序后序递归遍历,先序中序后序非递归遍历,层次遍历

class Empty(Exception):
    pass
class LinkedQueue:
    class _Node:
        def __init__(self,element,next):
            self._element=element
            self._next=next
    def __init__(self):
        self._head=None
        self._tail=None
        self._size=0
    def __len__(self):
        return self._size
    def is_empty(self):
        return self._size==0
    def first(self):
        if self.is_empty()==True:
            raise Empty('Queue is Empty')
        return self._head._element
    def dequeue(self):
        if self.is_empty():
            raise Empty('Queue is Empty')
        e=self._head._element
        self._head=self._head._next
        self._size-=1
        if self.is_empty():
            self._tail=None
        return e
    def enqueue(self,e):
        node=self._Node(e,None)
        if self.is_empty():
            self._head=node
        else:
            self._tail._next=node
        self._tail=node
        self._size+=1
    def rotate(self):
        node=self._head
        self._head = self._head._next
        if self._size==1:
            self._tail=None
            self._head=node
        else:
            self._tail._next=node
        self._tail=node
class LinkedStack:
  class _Node:
    def __init__(self,element,next):
        self._element=element
        self._next=next
  def __init__(self):
      self._head=None
      self._size=0
  def __len__(self):
      return self._size
  def is_empty(self):
      return self._size==0
  def push(self,e):
      self._head=self._Node(e,self._head)
      self._size+=1
  def top(self):
      if self.is_empty():
          raise Empty('Stack is Empty')
      return self._head._element
  def pop(self):
      if self.is_empty():
          raise Empty('Stack is Empty')
      e=self._head._element
      self._head=self._head._next
      self._size-=1
      return e
  def travel(self):
      cur=self._head
      while cur!=None:
          print(cur._element,end=' ')
          cur=cur._next
      print('\n')
class LinkBinaryTree:
    class _Node:
        def __init__(self,element,left=None,right=None,flag=0):
            self.element=element
            self.left=left
            self.right=right
            self._flag=flag
    def __init__(self):
        self.root=None
        self._size=0
    def is_empty(self):
        return self._size==0
    def node_num(self):
        return self._size
    def _createbinarytree(self,list):#递归的创建一棵二叉树
        while list.__len__()>0:
            e=list.pop(0)
            if e==-1:
               return None
            else:
               root=self._Node(e)
               self._size+=1
               root.left= self._createbinarytree(list)
               root.right=self._createbinarytree(list)
               return root
    def create(self,list):
        self.root=self._createbinarytree(list)
    def preorder(self,node):#递归先序遍历
        if node is not None:
           print(node.element)
           self.preorder(node.left)
           self.preorder(node.right)
    def inorder(self,node):#递归中序遍历
        if node is not None:
            self.inorder(node.left)
            print(node.element)
            self.inorder(node.right)
    def postorder(self,node):#递归中序遍历
        if node is not None:
            self.postorder(node.left)
            self.postorder(node.right)
            print(node.element)
    def preorderNR(self):#非递归线序遍历
        S = LinkedStack()
        p=self.root
        while S.is_empty()!=True or p is not None:
            while p is not None:
                S.push(p)
                print(p.element,end=' ')
                p=p.left
            if S.is_empty()!=True:
                p=S.pop()
                p=p.right
    def inorderNR(self):#非递归中序遍历
        S=LinkedStack()
        S.push(self.root)
        while S.is_empty()!=True:
            e=S.top()
            while e!=None:
                S.push(e.left)
                e=S.top()
            S.pop()
            if S.is_empty()!=True:
                node=S.pop()
                print(node.element,end=' ')
                S.push(node.right)
    def postorderNR(self):#非递归后序遍历
        S = LinkedStack()
        p=self.root
        while S.is_empty()!=True or p!=None:
            while p!=None:
                p._flag = 1
                S.push(p)
                p=p.left
            if S.is_empty()!=True:
                e=S.pop()
                if e._flag==1:
                    e._flag=2
                    S.push(e)
                    p=e.right
                elif e._flag==2:
                    print(e.element,end=' ')
                    p=None
    def levelorder(self):#层次遍历
        Q=LinkedQueue()
        Q.enqueue(self.root)
        while Q.is_empty()!=True:
            p=Q.dequeue()
            print(p.element,end=' ')
            if p.left!=None:
                Q.enqueue(p.left)
            if p.right!=None:
                Q.enqueue(p.right)
list=[1,2,4,-1,-1,5,8,-1,-1,9,10,-1,-1,11,-1,-1,3,6,-1,-1,7,-1,-1]#按树的先序遍历序列构建树其中-1表示空节点
L= LinkBinaryTree()
L.create(list)
#L.preorder(L.root)
#print(L.node_num())
#L.inorder(L.root)
#L.postorder(L.root)
#L.inorderNR()
#L.preorderNR()
#L.postorderNR()
L.levelorder()

你可能感兴趣的:(python学习)