Python 实现链表和二叉树
生活随笔
收集整理的這篇文章主要介紹了
Python 实现链表和二叉树
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Python 實現鏈表和二叉樹
1、鏈表
class Node:def __init__(self, init_data):self.data = init_dataself.next = Nonedef get_data(self):return self.datadef get_next(self):return self.nextdef setData(self,new_data):self.data = newdatadef setNext(self,new_next):self.next = new_next2、二叉樹
# 樹的結構 class BinaryTree:def __init__(self,root):self.key = rootself.leftChild = Noneself.rightChild = None# 樹的前序遍歷 def preorder(tree):if tree:print(tree.key)preorder(tree.leftChild)preorder(tree.rightChild)GOOD LUCK!
總結
以上是生活随笔為你收集整理的Python 实现链表和二叉树的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java中BigDecimal类介绍及用
- 下一篇: 数据结构与算法(Python)– 回溯法