任务一:
基于列表list构建链表,实现链表的增删改查操作
#!/usr/bin/python
# -*- coding: utf-8 -*-
class Node(object):
def __init__(self,val,p=None):
self.data = val
self.next = p
class LinkList(object):
def __init__(self):
self.head = None
def __getitem__(self, key):
if self.is_empty():
print ('linklist is empty.')
return
elif key <0 or key > self.getlength():
print ('the given key is error')
return
else:
return self.getitem(key)
def __setitem__(self, key, value):
if self.is_empty():
print ('linklist is empty.')
return
elif key <0 or key > self.getlength():
print ('the given key is error')
return
else:
self.delete(key)
return self.insert(key)
def initlist(self,data):
self.head = Node(data[0])
p = self.head
for i in data[1:]:
node = Node(i)
p.next = node
p = p.next
def getlength(self):
p = self.head
length = 0
while p!=None:
length+=1
p = p.next
return length
def is_empty(self):
if self.getlength() ==0:
return True
else:
return False
def clear(self):
self.head = None
def append(self,item):
q = Node(item)
if self.head ==None:
self.head = q
else:
p = self.head
while p.next!=None:
p = p.next
p.next = q
def getitem(self,index):
if self.is_empty():
print ('Linklist is empty.')
return
j = 0
p = self.head
while p.next!=None and j <index:
p = p.next
j+=1
if j ==index:
return p.data
else:
print ('target is not exist!')
def insert(self,index,item):
if self.is_empty() or index<0 or index >self.getlength():
print ('Linklist is empty.')
return
if index ==0:
q = Node(item,self.head)
self.head = q
p = self.head
post = self.head
j = 0
while p.next!=None and j<index:
post = p
p = p.next
j+=1
if index ==j:
q = Node(item,p)
post.next = q
q.next = p
def delete(self,index):
if self.is_empty() or index<0 or index >self.getlength():
print ('Linklist is empty.')
return
if index ==0:
q = Node(item,self.head)
self.head = q
p = self.head
post = self.head
j = 0
while p.next!=None and j<index:
post = p
p = p.next
j+=1
if index ==j:
post.next = p.next
def index(self,value):
if self.is_empty():
print ('Linklist is empty.')
return
p = self.head
i = 0
while p.next!=None and not p.data ==value:
p = p.next
i+=1
if p.data == value:
return i
else:
return -1
l = LinkList()
l.initlist([1,2,3,4,5])
print (l.getitem(4))
## 5
l.append(6)
print (l.getitem(5))
## 6
l.insert(4,40)
print (l.getitem(3))
## 4
print (l.getitem(4))
## 40
print (l.getitem(5))
## 5
l.delete(5)
print (l.getitem(5))
## 6
l.index(5)
## 6
任务二:
构建二叉搜索树,实现插入、删除、前序遍历、后序遍历、查找元素等操作
class Node:
def __init__(self,data):
self.left = None
self.right = None
self.parent = None
self.data = data
def insert(self, data):
node = self
while node:
if data < node.data:
next = node.left
else:
next = node.right
if next:
node = next
else:
break
nn = self.createNode(data)
if data < node.data:
node.left = nn
node.left.parent = node
else:
node.right = nn
node.right.parent = node
return nn
def createNode(self, data):
return Node(data)
def printTree (self):
if self.left:
self.left.printTree()
print (self.data)
if self.right:
self.right.printTree()
def postorderTree(self):
print (self.data)
if self.left:
self.left.postorderTree()
if self.right:
self.right.postorderTree()
def lastorderTree(self):
if self.left:
self.left.lastorderTree()
if self.right:
self.right.lastorderTree()
print (self.data)
def maxNode(self):
if self.right:
return self.right.maxNode()
else:
return self
def minNode (self):
if self.left:
return self.left.maxNode()
else:
return self
def lookup(self, data):
if self.data == data:
return self
if self.data > data:
if self.left:
return self.left.lookup(data)
else:
return None
else:
if self.right:
return self.right.lookup(data)
else:
return None
def successor(self):
n= self
if n.right:
return n.right.minNode()
else:
p = n.parent
while p and p.right == n:
n = p
p = p.parent
return p
def delete(self, root):
n= self
if n.left and n.right:
s = n.right.minNode()
n.data = s.data
root = s.delete(root)
return root
if not n.parent:
if n.left:
root = n.left
n.left = None
root.parent = None
elif n.right:
root = n.right
n.right = None
root.parent = None
else:
root = None
return root
if not n.left and not n.right:
if n.parent.left == n:
n.parent.left = None
else:
n.parent.right = None
else:
if n.parent.left == n:
if n.left:
n.parent.left = n.left
else:
n.parent.left = n.right
else:
if n.right:
n.parent.right = n.right
else:
n.parent.right = n.left
n=None
return root
def buildTree():
root = Node(8)
root.insert(3)
root.insert(10)
root.insert(1)
root.insert(6)
root.insert(4)
root.insert(7)
root.insert(14)
root.insert(13)
print('\nroot second\n')
root.printTree()
print('\nroot last\n')
root.lastorderTree()
print('\nroot first\n')
root.postorderTree()
buildTree()
##
## 中序遍历结果:
[1,3,4,6,7,8,10,13,14]
## 后序遍历结果:
[1,4,7,6,3,13,14,10,8]
## 前序遍历结果:
[8,3,1,6,4,7,10,14,13]
任务三:
基于列表构建堆,并实现堆排序功能
def heapify( heap, root, length ):
father=root
child=father*2+1
while child < length:
if child + 1 < length and heap[child] > heap[child + 1]:
child += 1
if heap[father] < heap[child]:
break
heap[father], heap[child] = heap[child],heap[father]
father = child
child = father * 2 + 1
def build_heap( array ):
size = len( array )
for i in range( size // 2 - 1, -1, -1 ):
heapify( array, i, size )
def heap_sort( array ):
size = len( array )
build_heap( array )
b=[]
for i in range(1,size):
b.append(array[0])
n=len(array)
array[0]=array[n-1]
del array[n-1]
build_heap(array)
return b
a = [ 1,7,2,4,5,6,3,8,54,34,778,123,34,54,232,56]
b=[]
b=heap_sort( a )
print( b )
## 结果
[1,2,3,4,5,6,7,8,34,34,54,54,56,123,232]