1. The package of the stack
The stack is a linear table (commonly known as a stack) that is restricted to insert and delete operations at one end. The end that allows operations is called the "top of the stack", and the other fixed end is called the "bottom of the stack". When there is no element in the stack, it is called "Empty stack". Inserting an element into a stack is called pushing, and deleting an element from a stack is called pop. Features: Last In First Out (LIFO)
class Stack(object):
"""栈的封装[1, 2, 3, 4],左边栈底,右边栈顶"""
def __init__(self):
self.stack = []
def push(self, value):
"""入栈"""
self.stack.append(value)
print(f"入栈元素为{value}")
def pop(self):
"""出栈,首先判断栈是否为空"""
if self.is_empty():
raise Exception("栈为空")
item = self.stack.pop()
print(f"出栈元素为{item}")
return item
def is_empty(self):
"""判断栈是否为空"""
return len(self.stack) == 0
def top(self):
"""返回栈顶元素"""
if self.is_empty():
raise Exception("栈为空")
return self.stack[-1]
def __len__(self):
"""魔术方法, len(object)自动执行的方法"""
return len(self.stack)
if __name__ == '__main__':
stack = Stack()
stack.push(1)
stack.push(2)
stack.push(3)
print(len(stack)) # 3
stack.pop()
print(stack.is_empty()) # False
print(stack.top()) # 2
2. Encapsulation of the queue
A queue is a linear table that is restricted to insert operations at one end and delete operations at the other end. The end that allows insert operations is called the "end of the queue", and the end that allows deletions is called the "head of the queue". When there is no element in the queue Called the "empty team". Features: First In First Out (FIFO)
class Queue(object):
"""
队列的封装
1. 列表的左侧队尾
2. 列表的右侧队头
"""
def __init__(self):
self.queue = []
def enqueue(self, value):
"""入队"""
self.queue.insert(0, value)
print("入队元素为:", value)
def dequeue(self):
"""出队"""
if self.is_empty():
raise Exception("队列为空")
item = self.queue.pop()
print("出队元素:", item)
return item
def __len__(self):
"""获取队列的长度"""
return len(self.queue)
def first(self):
"""获取队头元素"""
if self.is_empty():
raise Exception("队列为空")
return self.queue[-1]
def last(self):
"""获取队尾元素"""
if self.is_empty():
raise Exception("队列为空")
return self.queue[0]
def is_empty(self):
"""判断队列是否为空"""
return len(self.queue) == 0
if __name__ == '__main__':
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
queue.enqueue(3)
print(queue.is_empty()) # False
queue.dequeue() # 1出队, 队列只剩32
print(queue.first()) # 2
print(queue.last()) # 3
Three. Binary Tree
"""
二叉树:
参考https://www.cnblogs.com/polly333/p/4740355.html
"""
class Node(object):
"""节点类"""
def __init__(self, val=None, left=None, right=None):
self.val = val
self.left = left
self.right = right
class BinaryTree(object):
"""封装二叉树"""
def __init__(self, root):
self.root = root
def pre_travel(self, root):
"""先序遍历: 根左右"""
if (root != None):
print(root.val)
self.pre_travel(root.left)
self.pre_travel(root.right)
def in_travel(self, root):
"""中序遍历: 左根右"""
if (root != None):
self.in_travel(root.left)
print(root.val)
self.in_travel(root.right)
def last_travel(self, root):
"""后序遍历: 左右根"""
if (root != None):
self.last_travel(root.left)
self.last_travel(root.right)
print(root.val)
if __name__ == '__main__':
node1 = Node(1)
node2 = Node(2)
node3 = Node(3)
node4 = Node(4)
node5 = Node(5)
node6 = Node(6)
node7 = Node(7)
node8 = Node(8)
node9 = Node(9)
node10 = Node(10)
bt = BinaryTree(root=node1)
node1.left = node2
node1.right = node3
node2.left = node4
node2.right= node5
node3.left = node6
node3.right = node7
node4.left = node8
node4.right = node9
node5.left = node10
# 先序遍历
bt.pre_travel(node1)
# 中序遍历
bt.in_travel(node1)
# 后序遍历
bt.last_travel(node1)