Python Learning 3--python Complex data types

Source: Internet
Author: User


1 stacks

A heap is a binary tree in which the value of each parent node is less than or equal to the value of all its child nodes, and the smallest element is always in the root node of the binary tree.

Creation of Heaps

Import HEAPQ Import  = range ( in scrambled order heap = for in data:      Print heapheapq.heappushpop (Heap,# new data into heap # popup smallest element, heap rebuild

List into a heap

Myheap = [100,2,3,4,22,7,10,5# converts list to heap # instead of stack element value, heap rebuild #  Returns the maximum of 3 values # 3 Minimum

2 queues

The features of the queue are the first in, the last in, the out, the LIFO, the

Import=# element enqueued q.put (1) q.put (2print# deque ([0, 1, 2]) Print # element 0 First out team Print # deque ([1, 2])


3 stacks

The feature of the stack is the last in Out,first-in-first out, LIFO, advanced-out

List can implement the basic operation of the Stack, append () equivalent to the stack, pop () equivalent to the stack, but when the list is empty, the pop () operation will have an exception, can not limit the size of the stack.

Import= stack.stack () X.push (1) x. push (2) x.show () X.pop () x.show ()
classStack:def __init__(Self, size=10): Self._content=[] self._size=sizedefEmpty (self): self._content= []     defIsEmpty (self):if  notself._content:returnTrueElse:             returnFalsedefsetSize (self,size): Self._size=sizedefIsfull (self):ifLen (self._content) = =self._size:returnTrueElse:             returnFalsedefpush (SELF,V):ifLen (self._content) <Self._size:self._content.append (v)Else:             Print 'Stack Full'     defpop (self):ifself._content:returnSelf._content.pop ()Else:             Print 'Stack is empty!'     defShow (self):Printself._contentdefShowremainderspace (self):Print 'Stack can still PUSH', Self.size-len (self._content),'elements.'     if __name__=='__main__':         Print 'Please use me as a module'

4 Linked List

List and its basic operations can be used directly to realize the function of the linked list

linktable = []linktable.append (3) linktable.append (5) Linktable.insert (1,4) Linktable.remove (linktable[1])


52 Fork Tree

...

6 direction diagram

...






Python Learning 3--python Complex data types

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.