数据结构笔记
目录
第九章
什么是树
树结构相关术语
树的定义1
树的定义2
树的嵌套列表实现
def BinaryTree(r):
return [r,[],[]]
def insertLeft(root,newBranch):
t = root.pop(1)
if len(t) > 1:
root.insert(1,[newBranch,t,[]])
else:
root.insert(1,[newBranch,[],[]])
return root
def insertRight(root,newBranch):
t = root.pop(2)
if len(t) > 1:
root.insert(2,[newBranch,[],t])
else:
root.insert(2,[newBranch,[],[]])
return root
def getRootVal(root):
return root[0]
def setRootVal(root,newVal):
root[0] = newVal
def getLeftChild(root):
return root[1]
def getRightChild(root):
return root[2]
r = BinaryTree(5)
insertLeft(r,4)
insertLeft(r,10)
insertRight(r,7)
insertRight(r,1)
left = getRightChild(r)
print(left)
setRootVal(left,6)
print(r)
insertLeft(left,3)
print(r)
print(getRightChild(getRightChild(r)))
==>
[1, [], [7, [], []]]
[5, [10, [4, [], []], []], [6, [], [7, [], []]]]
[5, [10, [4, [], []], []], [6, [3, [], []], [7, [], []]]]
[7, [], []]
树的链表实现
class BinaryTree:
def __init__(self,rootObj):
self.key = rootObj
self.leftChild = None
self.rightChild = None
def insertLeft(self,newNode):
if self.leftChild == None:
self.leftChild = BinaryTree(newNode)
else:
t = BinaryTree(newNode)
t.leftChild = self.leftChild
self.leftChild = t
def insertRight(self,newNode):
if self.rightChild == None:
self.rightChild = BinaryTree(newNode)
else:
t = BinaryTree(newNode)
t.rightChild = self.rightChild
self.rightChild = t
def getRightChild(self):
return self.rightChild
def getLeftChild(self):
return self.leftChild
def setRootVal(self,obj):
self.key = obj
def getRootVal(self):
return self.key
r = BinaryTree('a')
r.insertLeft('b')
r.insertRight('c')
r.getRightChild().setRootVal('hello')
r.getLeftChild().insertRight('d')
树的应用:表达式解析
from Stack import Stack
from BinaryTree1 import BinaryTree
import operator as o
def buildPraseTree(fpexp):
fplist = fpexp.split()
pStack = Stack()
eTree = BinaryTree('')
pStack.push(eTree)
current = eTree
for i in fplist:
if i == '(':
current.insertLeft()
pStack.push(current)
current = current.getLeftChild()
elif i not in ['+','-','*','/']:
current.setRootVal(int(i))
parent = pStack.pop()
current = parent
elif i in ['+','-','*','/']:
current.setRootVal(i)
current.insertRight()
pStack.push(current)
current = current.getRightChild()
elif i == ')':
current = pStack.pop()
else:
raise ValueError
return eTree
def evaluate(praseTree):
opers = {'+':o.add,'-':o.sub,'*':o.mul,'/':o.truediv}
leftC = praseTree.getLeftChild()
rightC = praseTree.getRightChild()
if leftC and rightC:
fn = opers[praseTree.getRootVal()]
return fn(evaluate(leftC),evaluate(rightC))
else:
return praseTree.getRootVal()
树的遍历
前序遍历
def preorder(tree):
if tree:
print(tree.getRootVal())
preorder(tree.getLeftChild())
preorder(tree.getRightChild())
中序遍历
def inorder(tree):
if tree:
inorder(tree.getLeftChild())
print(tree.getRootVal())
inorder(tree.getRightChild())
后序遍历
def postorder(tree):
if tree:
postorder(tree.getLeftChild())
postorder(tree.getRightChild())
print(tree.getRootVal())
优先队列和二叉堆
二叉堆初始化
class BinHeap:
def __init__(self):
self.heaplist = [0] #保持完全二叉树倍数特性
self.currentSize = 0
def percUp(self,i):
while i//2 > 0:
if self.heaplist[i] < self.heaplist[i//2]:
t = self.heaplist[i]
self.heaplist[i] = self.heaplist[i//2]
self.heaplist[i//2] = t
i = i//2
def percDown(self,i):
while(i*2) <= self.currentSize:
mc = self.minChild(i)
if self.heaplist[i] > self.heaplist[mc]:
t = self.heaplist[i]
self.heaplist[i] = self.heaplist[mc]
self.heaplist[mc] = t
i = mc
def minChild(self,i):
if i*2+1 > self.currentSize:
return i*2+2
elif self.heaplist[i*2] < self.heaplist[i*2+1]:
return i*2
else:
return i*2+1
def insert(self,k):
self.heaplist.append(k)
self.currentSize += 1
self.perUp(self.currentSize)
def delMin(self):
retval = self.heaplist[1]
self.heaplist[1] = self.heaplist[self.currentSize]
self.heaplist.pop()
self.currentSize -= 1
self.percDown(1)
return retval
def buildHeap(self,alist):
i = len(alist) // 2
self.currentSize = len(alist)
self.heaplist = [0] + alist
print(len(self.buildHeap),i)
while(i>0):
print(self.heaplist,i)
self.percDown(i)
i -= 1
print(self.heaplist,i)