一些資料結構

2021-10-14 07:52:17 字數 4842 閱讀 1634

>>

>

dir(heapq)

['__about__'

,'__all__'

,'__builtins__'

,'__cached__'

,'__doc__'

,'__file__'

,'__loader__'

,'__name__'

,'__package__'

,'__spec__'

,'_heapify_max',,

'_heapreplace_max'

,'_siftdown'

,'_siftdown_max'

,'_siftup'

,'_siftup_max'

,'heapify',,

,,'heapreplace'

,'merge'

,'nlargest'

,'nsmallest'

]>>

>

import heapq

>>

>

import random

>>

> data =

list

(range(10

))>>

> random.shuffle(data)

#打亂順序

>>

> data[9

,2,1

,6,4

,0,8

,5,3

,7]>>

> heap =

>>

>

for n in data:

#建堆》

> heap[0

,3,1

,4,6

,2,8

,9,5

,7]>>

0.5)

#在堆中插入元素

>>

> heap[0

,0.5,1

,4,3

,2,8

,9,5

,7,6

]>>

#從堆頂取出乙個元素

0>>

0.5>>

> myheap =[1

,2,3

,5,7

,8,9

,4,10

,333

]>>

> heapq.heapify(myheap)

#將列表轉化為堆

>>

> myheap[1

,2,3

,4,7

,8,9

,5,10

,333

]>>

> heapq.heapreplace(myheap,6)

#替換堆頂元素,並重新建堆

1>>

> myheap[2

,4,3

,5,7

,8,9

,6,10

,333

]>>

> heapq.nlargest(

3, myheap)

#獲取最值,並不修改原件

[333,10

,9]>>

> heapq.nsmallest(

3, myheap)[2

,3,4

]>>

> myheap[2

,4,3

,5,7

,8,9

,6,10

,333

]

>>

>

dir(queue)

['empty'

,'full'

,'lifoqueue'

,'priorityqueue'

,'queue'

,'******queue'

,'_py******queue'

,'__all__'

,'__builtins__'

,'__cached__'

,'__doc__'

,'__file__'

,'__loader__'

,'__name__'

,'__package__'

,'__spec__'

,'deque',,

,'threading'

,'time'

]>>

> q = queue.queue(

)>>

> q.put(0)

>>

> q.put(1)

>>

> q.put(2)

>>

> q.queue

deque([0

,1,2

])>>

> q.get()0

>>

> q.get()1

>>

> q.queue

deque([2

])

>>

> mystack =

>>3)

>>5)

>>7)

>>

> mystack[3

,5,7

]>>

> mystack.pop(

)7

對於可能會丟擲異常的情況,可以另外自定義類。

>>

> linktable =

>>3)

#在尾部追加節點

>>5)

>>

> linktable[3

,5]>>

> linktable.insert(1,

4)#在鍊錶中間插入節點

>>

> linktable[3

,4,5

]>>

> linktable.remove(linktable[1]

)刪除節點

>>

> linktable[3

,5]

同樣,對於可能會丟擲異常的情況,可以另外自定義類。

class

binarytree

:def

__init__

(self, value)

: self.__left =

none

self.__right =

none

self.__data = value

definsertleftchild

(self, value)

:if self.__left:

print

('left child tree already exists.'

)else

: self.__left = binarytree(value)

return self.__left

definsertrightchild

(self, value)

:if self.__right:

print

('right child tree already exists.'

)else

: self.__right = binarytree(value)

return self.__right

defshow

(self)

:print

(self.__data)

defpreorder

(self)

:#前序遍歷

print

(self.__data, end =

' ')

if self.__left:

self.__left.preorder(

)if self.__right:

self.__right.preorder(

)def

postorder

(self)

:#後序遍歷

if self.__left:

self.__left.postorder(

)if self.__right:

self.__right.postorder(

)print

(self.__data, end =

' ')

definorder

(self)

:#中序遍歷

if self.__left:

self.__left.inorder(

)print

(self.__data, end =

' ')

if self.__right:

self.__right.inorder(

)if __name__ ==

'__main__'

:print

('please use me as a module.'

)

例項

>>

>

import binarytree

>>

> root = binarytree.binarytree(

'root'

)>>

> b = root.insertrightchild(

'b')

>>

> a = root.insertleftchild(

'a')

>>

> c = a.insertleftchild(

'c')

>>

> d = b.insertrightchild(

'd')

>>

> root.inorder(

)c a root b d

一些C 資料結構知識

陣列 1.大小固定 2.記憶體連續 3.方便訪問 4.不方便新增刪除 5.儲存單一型別元素 arraylist 陣列列表 1.記憶體連續,可以通過下標訪問,大小不固定 2.方便訪問,不方便新增刪除.3.儲存不同型別的資料.訪問的是object型別,裝拆箱印象效能 list是乙個介面,arraylis...

資料結構的一些筆記

資料結構 1.邏輯結構 書中都是對邏輯結構的討論 幫助理解 人為抽象出來的結構 線性 表 非線性 圖,樹 2.儲存結構 物理結構 幫助理解 資料到底以什麼樣的形式儲存在計算機中 順序 鏈式 線性表1.線性表是最常用且最簡單的一種資料結構 2.在稍微複雜的線性表中,乙個資料元素可以由若干個資料項組成 ...

資料結構 串 串的一些操作

head.h include includeusing namespace std class string string string void string getstring void string getsubstring void string index cout index calle...