栈:
#栈的实现
'''
栈的操作:
Stark() 创建一个新的栈
push() 压栈
pop() 弹出栈元素
peek() 返回栈顶元素
is_empty() 判断栈是否为空
size() 返回栈的个数
'''
class Stack():
def __init__(self):
self.__list = []
def push(self,item):
'''添加一个元素item到栈顶'''
self.__list.append(item) #把元素加入列表尾部(时间复杂度O1),若是头部(On)
def pop(self):
'''弹出栈元素'''
return self.__list.pop() #从列表尾部弹出元素
def peek(self):
'''返回栈顶元素'''
if self.__list:
return self.__list[-1]
else:
return None
def is_empty(self):
'''判断是否为空'''
if self.__list:
return False
else:
return True
def size(self):
'''返回栈内元素的个数'''
return len(self.__list)
if __name__ == '__main__':
s = Stack()
s.push(1)
s.push(3)
s.push(5)
s.push(9)
print(s.peek()) #9
print(s.pop()) #9
print(s.peek()) #5
print(s.is_empty()) #False
print(s.size()) #3
队列:
#队列的实现
'''
队列的操作:
Queue() 创建一个新的队列
enqueue(item) 往队列中添加一个元素
dequeue 从队列头部删除一个元素
is_empty() 判断队列是否为空
size() 返回队列中元素的个数
'''
class Queue():
def __init__(self):
self.__list = []
def enqueue(self,item):
"""往队列中添加一个元素"""
self.__list.append(item)
def dequeue(self):
'''从队列头部删除一个元素'''
return self.__list.pop(0)
def is_empty(self):
'''判断队列是否为空'''
if self.__list:
return False
else:
return True
def size(self):
return len(self.__list)
if __name__ == '__main__':
q = Queue()
q.enqueue(1)
q.enqueue(2)
q.enqueue(5)
q.enqueue(9)
print(q.size()) # 4
print(q.dequeue()) # 1
print(q.is_empty()) #False
单向链表:
#单向链表的实现
#每个节点包括该节点的元素值和下一节点的连接预
#定义节点类
class Node():
"""节点"""
def __init__(self,elem):
self.elem = elem
self.next = None
#定义链表类
class SingleLinkList():
"""单向链表"""
def __init__(self,node = None):
self.__head = node
def is_empty(self):
"""判断是否为空"""
return self.__head == None
def length(self):
"""判断链表的长度"""
count = 0
cur = self.__head
while cur != None:
count += 1
cur = cur.next
return count
def travel(self):
"""遍历整个链表"""
cur = self.__head
while cur != None:
print(cur.elem,end=" ")
cur = cur.next
def add(self,item):
"""向链表头部添加节点"""
node = Node(item)
node.next = self.__head
self.__head = node
def append(self,item):
"""在链表尾部添加节点"""
node = Node(item)
if self.__head != None:
cur = self.__head
while cur.next != None:
cur = cur.next
cur.next = node
else:
self.__head = node
def insert(self,pos,item):
"""在链表的指定位置插入节点"""
node = Node(item)
count = 0
cur = self.__head
if self.__head == None:
self.__head.next = node
while cur != None:
if count < pos-1:
count +=1
cur = cur.next
elif count == pos-1:
count += 1
node.next = cur.next
cur.next = node
break
def remove(self,item):
"""删除节点"""
cur = self.__head
while cur != None:
if cur.next.elem == item:
cur.next = cur.next.next
break
else:
cur = cur.next
def seach(self,item):
"""查找节点的存在"""
cur = self.__head
count =0
while cur != None:
if cur.elem == item:
return count
cur = cur.next
count += 1
if __name__ == '__main__':
sl_1 = SingleLinkList()
print(sl_1.is_empty()) #true
print(sl_1.length()) #0
sl_1.append(10)
print(sl_1.is_empty()) #False
print(sl_1.length()) #1
sl_1.append(5)
sl_1.append(4)
sl_1.append(3)
sl_1.travel() #10,5,4,3
sl_1.add(11)
sl_1.travel() #11, 10 ,5,4 ,3
print("****")
sl_1.insert(1, 13)
sl_1.travel() #11 13 10 5 4 3
print("***")
sl_1.remove(10)
sl_1.travel() #11 13 5 4 3
print("***")
print(sl_1.seach(5)) #2
双向链表:
#双向链表
class Node():
"""节点"""
def __init__(self,item):
self.elem = item
self.prev = None
self.next = None
class DoubleLinkList():
def __init__(self,node = None):
self.__head = node
def is_empty(self):
return self.__head == None
def length(self):
cur = self.__head
count = 0
while cur != None:
count += 1
cur = cur.next
return count
def trave(self):
"""遍历整个链表"""
cur = self.__head
while cur != None:
print(cur.elem)
cur = cur.next
def add(self,item):
"""链表头部添加元素"""
node = Node(item)
if self.__head == None:
self.__head = node
else:
node.next = self.__head
self.__head.prev = node
self.__head = node
def append(self,item):
node = Node(item)
if self.__head == None:
self.__head = node
else:
cur = self.__head
while cur != None:
if cur.next == None:
cur.next = node
node.prev = cur
break
cur = cur.next
def insert(self,pos,item):
node = Node(item)
count = 0
cur = self.__head
while cur != None:
if count == pos:
cur.prev.next = node
node.prev = cur.prev
cur.prev = node
node.next = cur
break
cur = cur.next
count += 1
def remove(self,item):
cur = self.__head
while cur != None:
if cur.elem == item:
cur.prev.next = cur.next
cur.next.prev = cur.prev
break
cur = cur.next
def search(self,item):
count = 0
cur = self.__head
while cur != None:
if item == cur.elem:
return count
count += 1
cur = cur.next
if __name__ == '__main__':
dll = DoubleLinkList()
dll.add(1)
dll.add(2)
dll.trave() #2,1
dll.append(3)
dll.append(4)
dll.trave() # 2 ,1 ,3, 4
print("*****")
dll.insert(1,5)
dll.trave() # 2 5 1 3 4
print("****")
dll.remove(5)
dll.trave() # 2 1 3 4
print(dll.search(3)) # 2
单向循环链表:
#单向循环链表
#即头尾相接
class Node():
"""节点"""
def __init__(self,item = None):
self.elem = item
self.next = None
class CircleLinkList():
"""单向循环链表"""
def __init__(self,node = None):
self.__head = node
if node:
node.next = node
def is_empty(self):
"""判断链表是否为空"""
return self.__head == None
def length(self):
"""判断链表的长度"""
cur = self.__head
if self.__head == None:
return 0
count = 0
while cur.next != self.__head:
count +=1
cur = cur.next
return count + 1
def trave(self):
'''遍历整个链表'''
cur = self.__head
if self.__head == None:
return None
elif self.__head.next == self.__head:
print(self.__head.elem.elem)
else:
while cur.next != self.__head:
print(cur.elem,end=" ")
cur = cur.next
print(cur.elem) #循环中没有打印最后一个节点元素就跳出循环了,需要补上
def add(self,item):
"""从链表头插入节点"""
node = Node(item)
if self.__head == None:
self.__head = node
node.next = node
elif self.__head.next == self.__head :
node.next = self.__head
self.__head.next = node
self.__head = node
elif self.__head.next != self.__head:
node.next = self.__head
self.__head = node
def append(self,item):
"""从链表尾部插入节点"""
node = Node(item)
if self.__head == None:
self.__head = node
node.next = node
else:
cur = self.__head
while cur.next != self.__head:
cur = cur.next
cur.next = node
node.next = self.__head
def insert(self,pos,item):
"""在指定位置插入节点"""
node = Node(item)
cur = self.__head
count = 0
while cur.next != self.__head:
if count == pos-1:
node.next = cur.next
cur.next = node
cur = node
break
else:
count += 1
cur = cur.next
def remove(self,item):
cur = self.__head
while cur.next != self.__head:
if cur.next.elem == item:
cur.next = cur.next.next
break
cur = cur.next
def search(self,item):
cur = self.__head
count = 0
while cur.next != self.__head:
if cur.elem == item:
return count
count += 1
cur = cur.next
if __name__ == '__main__':
cl = CircleLinkList()
cl.add(1)
cl.add(2)
cl.trave() #2 1
print(cl.length()) #2
cl.append(3)
cl.append(4)
cl.trave() #2 1 3 4
cl.insert(1,5)
cl.trave() #2 5 1 3 4
cl.remove(5)
cl.trave() #2 1 3 4
print(cl.search(3)) #2
二叉树:
#二叉树
"""
在二叉树上,第i层最多有2^(i-1)个节点
深度为k的二叉树最多有2^k - 1 个节点
对于任意二叉树,如果叶节点数为N0,度数为2的节点数为N2,则N0=N2+1
具有n个节点的完全二叉树的的深度为log2(n-1)
完全二叉树,若从上至下,从左至右编号,编号为i的节点,其左孩子比为2i,右孩子为2i+1
"""
"""
层次遍历
先序遍历:根,左,右
中序遍历:左 根 右
后序遍历:左 右 根
"""
class Node():
def __init__(self,item):
self.elem = item
self.l = None
self.r = None
class Tree():
def __init__(self):
self.root = None
def add(self,item):
node = Node(item)
if self.root is None:
self.root = node
return
queue = [self.root]
while queue:
cur_node = queue.pop(0) #表示正在处理的节点
if cur_node.l is None:
cur_node.l = node
return
else :
queue.append(cur_node.l)
if cur_node.r is None:
cur_node.r = node
return
else:
queue.append(cur_node.r)
def breath_trave(self): #层次遍历
if self.root is None:
return None
queue = [self.root]
while queue:
cur_node = queue.pop(0)
print(cur_node.elem,end=' ')
if cur_node.l != None:
queue.append(cur_node.l)
if cur_node.r != None:
queue.append(cur_node.r)
def preorder(self,node):
if node == None:
return
print(node.elem, end= ' ')
self.preorder(node.l)
self.preorder(node.r)
def inorder(self,node):
if node == None:
return
self.inorder(node.l)
print(node.elem, end=' ')
self.inorder(node.r)
def postorder(self,node):
if node == None:
return
self.postorder(node.l)
self.postorder(node.r)
print(node.elem,end=' ')
if __name__ == '__main__':
tr = Tree()
tr.add(0)
tr.add(1)
tr.add(2)
tr.add(3)
tr.add(4)
tr.add(5)
tr.add(6)
tr.add(7)
tr.add(8)
tr.add(9)
tr.breath_trave() # 1 2 3 4 5 6 7 8 9
print("层次遍历")
tr.preorder(tr.root) #1 2 4 8 9 5 3 6 7
print("先序遍历")
tr.inorder(tr.root) # 8 9 4 5 2 6 7 3 1
print("中序遍历")
tr.postorder(tr.root)
print("后序遍历") #7 8 3 9 4 1 5 6 2 0
插入排序:
#插入排序
def insert_sort(alist):
"""插入排序"""
n = len(alist)
l = list()
for i in range(n):
while i > 0:
if alist[i-1] > alist[i] :
alist[i-1] , alist[i] = alist[i] , alist[i-1]
else:
break
i -= 1
return alist
if __name__ == '__main__':
list_beforesort = [1,56,24,98,67,88]
l = insert_sort(list_beforesort)
print(l)
希尔排序:
#希尔排序
def hill_sort(alist):
"""希尔排序"""
n = len(alist)
gap = n // 2
i = gap
while gap >= 1:
#和插入算法的区别就是gap(步长)
for i in range(gap,n):
while i > 0:
if alist[i] < alist[i-gap]:
alist[i] , alist[i-gap] = alist[i-gap] , alist[i]
i -= gap
else:
break
gap //= 2
return alist
if __name__ == '__main__':
l_beforesort = [11,56,78,95,45,36,26,86]
l = hill_sort(l_beforesort)
print(l)
归并排序:
#归并排序
def merge_sort(alist):
n = len(alist)
if n <= 1:
return alist
mid = n // 2
#left 采用归并排序后形成的有序的新的列表
left = merge_sort(alist[:mid])
#right 采用归并后形成的有序的新的列表
right = merge_sort(alist[mid:])
#将两个有序的序列合并为一个新的整体
cur_left = 0 #游标
cur_right = 0
result = []
while cur_left <= len(left)-1 and cur_right <= len(right)-1:
if left[cur_left] <= right[cur_right]:
result.append(left[cur_left])
cur_left += 1
else:
result.append(right[cur_right])
cur_right += 1
#其中一个游标走到头之后把剩余的部分都加到结果列表中
result += left[cur_left:]
result += right[cur_right:]
return result
if __name__ == '__main__':
li = [18,56,33,89,25,46,98,67,55]
li = merge_sort(li)
print(li)
pass