tinyxml2遍历所有节点_Python实现二叉树的遍历
Outline:
樹(Tree)
是一種抽象數據類型(ADT),是由n(n>=0)個有限節(jié)點來模擬的一個具有樹狀結構性質的數據集合。
二叉樹 Binary Tree
- 至少有一個節(jié)點(根節(jié)點)
- 每個節(jié)點最多有2棵子樹(左右子樹)(即每個節(jié)點的度小于3)
- 左子樹和右子樹是有順序的,次序不能任意顛倒。
- 即使樹中某節(jié)點只有一棵子樹,也要區(qū)分它是左子樹還是右子樹。
Python定義二叉樹類:
Class二叉樹分類:
- 滿二叉樹
- 完全二叉樹
- 二叉查找樹(Binary Search Tree - BST)
二叉查找樹
root node的值(5),大于其left subtree中任意一個節(jié)點的值,小于其right subtree中任意一節(jié)點的值。
【說人話】:以root節(jié)點為界,小于root節(jié)點的值保存在left節(jié)點,大于root節(jié)點的值保存在right節(jié)點。
- 任意節(jié)點的左、右子樹也分別為二叉查找樹;
- 沒有鍵值相等的節(jié)點
A binary search tree (BST) is a form of rooted binary tree.
Each node within a binary tree has an associated payload and references to the root node of any right or left subtrees at that point.
Any payload contained within any left subtree must be less than the value of the payload of node N and, conversely, that any payload contained within any right subtree must exceed the value of the payload of node N.
二叉樹遍歷
二叉樹的遍歷:
- 深度優(yōu)先(DFS: Depth-First Search)
- 寬度優(yōu)先(BFS: Breadth-First Search)
其中深度優(yōu)先遍歷又分為:
- 前序遍歷:根-左-右
- 中序遍歷:左-根-右
- 后序遍歷:左-右-根
前序遍歷:root-left-right: 5-3-2-4-7-6
遞歸
def中序遍歷:left-root-right : 2-3-4-5-7-6
遞歸
def后序遍歷:left-right-root: 2-4-3-6-7-5
遞歸
def寬度優(yōu)先遍歷(BFS):自頂向下: 5-3-7-2-4-6
迭代
def總結
以上是生活随笔為你收集整理的tinyxml2遍历所有节点_Python实现二叉树的遍历的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python如何调用阿里云接口_Pyth
- 下一篇: python特征匹配 查找_特征匹配+单