Practice Goals:
(1) Python implements a doubly linked list class.
(2) Using a doubly linked list class to implement the basic stack structure.
(3) Write the class to represent the binary tree, and realize the first order, the middle order, the recursion and the non-recursive version of the two-fork tree
1. First implement the bidirectional linked list class, and then use this class to implement the basic stack structure. The code is as follows:
classdoublelinkedlist:classNode:def __init__(self, data): Self._data=Data Self._next=None Self._pre=Nonedef __init__(self): self.__head= Doublelinkedlist.node ("__head") self.__tail= Doublelinkedlist.node ("__tail") self.__head. _next = self.__tailSelf .__tail. _pre = self.__head defAppend (self, data): node=Doublelinkedlist.node (data) self.__tail. _pre._next =node Node._pre= self.__tail. _pre self.__tail. _pre =node Node._next= self.__tail defRemove (self, data): node= self.__head whileNode! = self.__tail: ifNode._data = =Data:node._pre._next=Node._next Node._next._pre=Node._pre Breaknode=Node._nextdefInsert (self, I, data):Pass #TODO does not implement insert Operation defpop (self): node= self.__tail. _preifNode! = self.__head: Node._pre._next=Node._next Node._next._pre=Node._pre Node._next=None Node._pre=NonereturnNode._datareturnNonedefIs_empty (self)BOOL:returnSelf.__head. _next = = self.__tail defIternodes (self)None:node= self.__head. _next whileNode! = self.__tail: yieldnode._data Node=Node._nextdefAdd_last (self, data:object)None:self.append (data)defPoll_first (Self): node= self.__head. _nextifNode! = self.__tail: Self.__head. _next =Node._next Node._next._pre= self.__headNode._next=None Node._pre=NonereturnNode._datareturnNonedefPoll_last (self):returnSelf.pop ()defPeek_first (Self): node= self.__head. _nextifNode! = self.__tail: returnNode._datareturnNonedefPeek_last (Self): node= self.__tail. _preifNode! = self.__head: returnNode._datareturnNoneclassStack:def __init__(self): self.__dlnklst=doublelinkedlist ()defpop (self):returnSelf.__dlnklst. Pop ()defAdd (self, data): Self.__dlnklst. Append (data)defpush (self, data):returnSelf.__dlnklst. Append (data)defPeek (self):returnSelf.__dlnklst. Peek_last ()defpoll (self):returnSelf.__dlnklst. Poll_last ()defIs_empty (self):returnSelf.__dlnklst. Is_empty ()2. Write a class that represents a binary tree. The code is as follows:
classNode:def __init__(self, value): Self.__value=value self.__left=None self.__right=None @propertydefvalue (self):returnSelf.__value@propertydefLeft (self):returnSelf.__left@left. SetterdefLeft (Self, node): Self.__left=node @propertydefRight (self):returnSelf.__right@right. SetterdefRight (Self, node): Self.__right= Node
3. Write a function that implements the recursive and non-recursive versions of the first order, the middle order, and the sequential traversal of the two-fork tree. The code is as follows:
defPre_order_recur (head):if notHead:return Print(Head.value, sep=" ", end=" ") pre_order_recur (head.left) pre_order_recur (head.right)defIn_order_recur (head):if notHead:returnin_order_recur (head.left)Print(Head.value, sep=" ", end=" ") in_order_recur (head.right)defPost_order_recur (head):if notHead:returnpost_order_recur (head.left) post_order_recur (head.right)Print(Head.value, sep=" ", end=" ")#First Order non-recursivedefPre_order_unrecur (head):if notHead:returnStack=Stack () stack.add (head) while notStack.is_empty (): Head=Stack.pop ()Print(Head.value, sep=" ", end=" ") ifHead.right:stack.add (head.right)ifHead.left:stack.add (head.left)Else: Print()#middle order non-recursivedefIn_order_unrecur (head):if notHead:returnStack=Stack () while notStack.is_empty ()orHead:ifHead:stack.add (head) head=Head.leftElse: Head=Stack.pop ()Print(Head.value, sep=" ", end=" ") Head=Head.rightElse: Print()#Post -Delivery non-recursiondefPost_order_unrecur (head):if notHead:returnStack1=Stack () Stack2=Stack () stack1.add (head) while notStack1.is_empty (): Head=Stack1.pop () stack2.add (head)ifHead.left:stack1.add (head.left)ifHead.right:stack1.add (head.right) while notStack2.is_empty (): Head=Stack2.pop ()Print(Head.value, sep=" ", end=" ") Else: Print()
4. Simple test, verify correctness. The code is as follows:
if __name__=="__main__": Head= Node (1) Head.left= Node (2) Head.right= Node (3) Head.left.left= Node (4) Head.left.right= Node (5) Head.right.right= Node (6) Print("Pre order Travel binary tree.") Pre_order_unrecur (head) pre_order_recur (head)Print("\nin Order Travel binary tree.") In_order_unrecur (head) in_order_recur (head)Print("\npost Order Travel binary tree.") Post_order_unrecur (head) post_order_recur (head)
Python programming data structure and algorithm practice _007