學習資料結構,用Python的字典實現二叉樹是否合適?

時間 2021-06-07 19:39:57

1樓:

class Node(object):

def __init__(self, data):

self.data = data

self.left = self.right = Nonedef pre_order(self, fn):

fn(data)

self.left.pre_order(fn) if self.left

self.right.pre_order(fn) if self.right

class BinaryTree(object):

def __init__(self, firstdata):

self.root = Node(firstdata)def insert(self, data):

pass # ...

def pre_order(self, fn):

return self.root.pre_order(fn)t = BinaryTree(1)

def print_node(n):

print (n.data)

t.pre_order(print_node)既然是實現資料結構習題就別偷懶來,要寫二叉樹當然從 Node 寫起。

學習資料結構有什麼用?

秋葉 換種說法吧,你初中學過的勾股定律,二次函式,概率 高中學的集合,指數對數,立體幾何,統計,各種不等式在95 以上的人在以後的工作生活中,都用不到.但是這社會的發展,大部分就是靠的那5 的人,所以才會被寫入教材資料結構和演算法也是一樣,95 的程式設計師也是用不到的,但是有的人是想當那5 的 如...

有哪些用 Python 語言講演算法和資料結構的書?

黃哥 黃哥 黃哥推薦的八本資料結構和演算法 Python描述 書。Data Structures and Algorithms in Python Data Structures and Algorithms Using Python and C Data Structures and Algori...

學習資料結構有什麼好的教程?

資料結構和數學很類似都是比較抽象的,而往往實際問題都是非常複雜的,所以要先掌握最基本最抽象最特殊的規律。學習資料結構首先要掌握一門計算機語言,起碼要知道語法,能利用它完成一些基本程式,就像首先要掌握數學最基本的加減乘除,定理之類的規則。其次要知道資料結構和演算法是分不開的,學習資料結構的同時也需要一...