Python notes--implementation of several data structures: stacks, queues, and binary trees

Source: Internet
Author: User
Tags data structures

At present, only three kinds of implementation, stacks, queues and two fork tree, which day to continue to fill it ~

Bishi Busy Dizzy ~ ~

1. Stack

#栈 class Stack:def __init__ (Self,size =): Self.stack = [] self.size = Size Self.top =-1 def setSize (self, size): Self . Size = Size Def isempty (self): if self.top = = -1:return True else:return False def isfull (self): if self.top +1 = self . Size:return True Else:return False def top (self): if Self.isempty (): Raise Exception ("Stackisempty") Else:return self. STACK[SELF.TOP] def push (self,obj): If Self.isfull (): Raise Exception ("StackOverflow") else:self.stack.append (obj) Self.top +=1 def pop (self): if Self.isempty (): Raise Exception ("Stackisempty") else:self.top-= 1 return Self.stack.pop () Def show (self): print (self.stack) s = Stack (5) s.push (1) s.push (2) S.push (3) S.push (4) S.push (5) S.show () S.pop () S.show ( ) S.push (6) s.show ()

Perform:

2. Queues

#队列 class Queue:def __init__ (Self,size =): Self.queue = [] self.size = Size Self.front = 0 self.rear = 0 def isempty (s ELF): return self.rear = = 0 def isfull (self): if (self.front-self.rear + 1) = = Self.size:return True else:return False Def (self): if Self.isempty (): Raise Exception ("Queueisempty") Else:return Self.queue[self.front] def last (self): I F Self.isempty (): Raise Exception ("Queueisempty") Else:return Self.queue[self.rear] def add (self,obj): If Self.isfull () : Raise Exception ("Queueoverflow") else:self.queue.append (obj) self.rear + 1 def Delete (self): if Self.isempty (): Raise Exception ("Queueisempty") Else:self.rear-=1 return Self.queue.pop (0) def show (self): print (self.queue) q = Queue (3) Q.ad D (1) q.add (2) q.show () Q.delete () q.show ()

3. Two fork Tree

#二叉树 class Binarytreenode:def __init__ (self,data,left,right): Self.left = left self.data = Data Self.right = Right class Binarytree:def __init__ (self): Self.root = None def maketree (self,data,left,right): Self.root = Binarytreenode (data, Left,right) #left. root = Right.root = None def isempty (self): If Self.root is None:return True else:return False def pre Order (SELF,R): If r.root are not none:print (r.root.data) If r.root.left are not None:self.preOrder (r.root.left) if R.root. Right isn't None:self.preOrder (r.root.right) def inorder (self,r): If R.root is not none:if r.root.left are not None:sel F.inorder (r.root.left) print (r.root.data) if R.root.right is not None:self.inOrder (r.root.right) def postorder (Self,r) : If R.root is not none:if r.root.left are not None:self.preOrder (r.root.left) If r.root.right are not None:self.preOrder (r.root.right) Print (r.root.data) def levelorder (self,a): Q = Queue () R = a while R was not None:print (R.root.data) if R.R Oot.left is not None:q.add (r.roOt.left) If R.root.right is not None:q.add (r.root.right) if Q.isempty (): Print ("empty") r = None Else:r = Q.delete () R = BinaryTree () RA = BinaryTree () ra.maketree (2,none,none) RB = BinaryTree () rb.maketree (3,none,none) r.maketree (1,RA,RB) p Rint ("Pre-Traversal") R.preorder (r) print ("Sequence traversal") R.inorder (r) print ("Sequential traversal") R.postorder (r) Print ("Level traversal") R.levelorder (R)

Run Result:

Follow-up implementation will be slowly fill ~ ~ The old will continue to improve, I hope everyone to give advice ~ ~

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.