1.双链表

class Node(object):
def __init__(self, value=None):
self._prev = None
self.data = value
self._next = None
def __str__(self):
return "Node(%s)"%self.data
def __init__(self):
def insert(self, value):
element = Node(value)
def search(self, value):
raise ValueError("the linked list is empty")
while temp.data != value:
temp = temp._next
return temp
def delete(self, value):
element = self.search(value)
if not element:
element._prev._next = element._next
element._next._prev = element._prev
return element.data
def __str__(self):
values = []
while temp and temp.data:
values.append(temp.data)
temp = temp._next

2. 栈

class Stack(object):
def __init__(self):
self._top = 0
self._stack = []
def put(self, data):
self._stack.insert(self._top, data)
self._top += 1
def pop(self):
if self.isEmpty():
raise ValueError('stack 为空')
self._top -= 1
data = self._stack[self._top]
return data
def isEmpty(self):
if self._top == 0:
return True
else:
return False
def __str__(self):
return "Stack(%s)"%self._stack

3.队列

class Queue(object):
def __init__(self, max_size=float('inf')):
self._max_size = max_size
self._top = 0
self._tail = 0
self._queue = []
def put(self, value):
if self.isFull():
raise ValueError("the queue is full")
self._queue.insert(self._tail, value)
self._tail += 1
def pop(self):
if self.isEmpty():
raise ValueError("the queue is empty")
data = self._queue.pop(self._top)
self._top += 1
return data
def isEmpty(self):
if self._top == self._tail:
return True
else:
return False
def isFull(self):
if self._tail == self._max_size:
return True
else:
return False
def __str__(self):
return "Queue(%s)"%self._queue

4. 二叉树（定义与遍历）

class Node:
def __init__(self,item):
self.item = item
self.child1 = None
self.child2 = None
class Tree:
def __init__(self):
self.root = None
node = Node(item)
if self.root is None:
self.root = node
else:
q = [self.root]
while True:
pop_node = q.pop(0)
if pop_node.child1 is None:
pop_node.child1 = node
return
elif pop_node.child2 is None:
pop_node.child2 = node
return
else:
q.append(pop_node.child1)
q.append(pop_node.child2)
def traverse(self): # 层次遍历
if self.root is None:
return None
q = [self.root]
res = [self.root.item]
while q != []:
pop_node = q.pop(0)
if pop_node.child1 is not None:
q.append(pop_node.child1)
res.append(pop_node.child1.item)
if pop_node.child2 is not None:
q.append(pop_node.child2)
res.append(pop_node.child2.item)
return res
def preorder(self,root): # 先序遍历
if root is None:
return []
result = [root.item]
left_item = self.preorder(root.child1)
right_item = self.preorder(root.child2)
return result + left_item + right_item
def inorder(self,root): # 中序序遍历
if root is None:
return []
result = [root.item]
left_item = self.inorder(root.child1)
right_item = self.inorder(root.child2)
return left_item + result + right_item
def postorder(self,root): # 后序遍历
if root is None:
return []
result = [root.item]
left_item = self.postorder(root.child1)
right_item = self.postorder(root.child2)
return left_item + right_item + result
t = Tree()
for i in range(10):
print('层序遍历:',t.traverse())
print('先序遍历:',t.preorder(t.root))
print('中序遍历:',t.inorder(t.root))
print('后序遍历:',t.postorder(t.root))

tags: self,def,root,return,pop,node,None,temp,value,data,element,遍历

1.凡CodeSecTeam转载的文章,均出自其它媒体或其他官网介绍,目的在于传递更多的信息,并不代表本站赞同其观点和其真实性负责；
2.转载的文章仅代表原创作者观点,与本站无关。其原创性以及文中陈述文字和内容未经本站证实,本站对该文以及其中全部或者部分内容、文字的真实性、完整性、及时性，不作出任何保证或承若；
3.如本站转载稿涉及版权等问题,请作者及时联系本站,我们会及时处理。