Python programming data structure and algorithm practice _009

Source: Internet
Author: User

Practice content:

Determine if a tree is a search binary tree.

Body Content Summary:

1. Create a class to implement a doubly linked list and basic stack structure.
2. Create a class to represent a binary tree.
3. Determine whether a tree is a recursive and non-recursive implementation of a search binary tree.
4. Simple test, verify correctness.

1. Create a class to implement a doubly linked list and 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._nextdefpop (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. Create a class to represent a binary tree. The code is as follows:
classNode:def __init__(self, data): Self.__data=data self.__left=None self.__right=None @propertydefdata (self):returnSelf.__data@propertydefLeft (self):returnSelf.__left@left. SetterdefLeft (Self, node): Self.__left=node @propertydefRight (self):returnSelf.__right@right. SetterdefRight (Self, node): Self.__right= Node
3. Determine whether a tree is a recursive and non-recursive implementation of a search binary tree. The code is as follows:
defIs_bst_recur (Head, pre=None):if  notHead:returnTrue cur=Headif  notis_bst_recur (Cur.left, pre):returnFalseifPre andPre.data >Cur.data:returnFalse Pre=curreturnis_bst_recur (cur.right, Pre)defIs_bst_unrecur (head):if  notHead:returnStack=Stack () Pre=None while  notStack.is_empty ()orHead:ifHead:stack.add (head) head=Head.leftElse: Head=Stack.pop ()ifPre andPre.data >Head.data:returnFalse Pre=Head Head=Head.rightElse:        returnTrue
4. Simple test, verify correctness. The code is as follows:
if __name__=="__main__":    #BSTHead1 = Node (9) Head1.left= Node (8) Head1.right= Node (10) Head1.left.left= Node (7) Head1.left.right= Node (9) Head1.right.left= Node (9) Head1.right.right= Node (11)    Print(Is_bst_recur (HEAD1))#True    Print(Is_bst_unrecur (HEAD1))#True    #Not BSTHead2 = Node (9) Head2.left= Node (8) Head2.right= Node (1) Head2.left.left= Node (7) Head2.left.right= Node (9) Head2.right.left= Node (9) Head2.right.right= Node (11)    Print(Is_bst_recur (HEAD2))#False    Print(Is_bst_unrecur (HEAD2))#False

Python programming data structure and algorithm practice _009

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.