


Using Python to implement tree traversal algorithm and type of tree traversal
树遍历意味着访问树中的每个节点。和线性数据结构单一的遍历方式不同,二叉树是分层式数据结构可以以不同的方式遍历。

树遍历结构特点
1、每个树的节点都承载一个数据
2、每个树下都有2个子树

树遍历有三种类型
1、中序遍历
先遍历左子树所有节点,在是根节点,最后访问右子树所有节点。
2、前序遍历
先遍历根节点,再访问左子树中的所有节点,最后访问右子树中的所有节点。
3、后序遍历
先访问左子树中的所有节点,再访问右子树中的所有节点,最后访问根节点。
Python实现树遍历
class Node: def __init__(self,item): self.left=None self.right=None self.val=item #中序遍历 def inorder(root): if root: inorder(root.left) print(str(root.val)+"->",end='') inorder(root.right) #前序遍历 def postorder(root): if root: postorder(root.left) postorder(root.right) print(str(root.val)+"->",end='') #后序遍历 def preorder(root): if root: print(str(root.val)+"->",end='') preorder(root.left) preorder(root.right) root=Node(1) root.left=Node(2) root.right=Node(3) root.left.left=Node(4) root.left.right=Node(5) print("中序遍历") inorder(root) print("前序遍历") preorder(root) print("后序遍历") postorder(root)
The above is the detailed content of Using Python to implement tree traversal algorithm and type of tree traversal. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

Regular expressions are powerful tools for pattern matching and text manipulation in programming, enhancing efficiency in text processing across various applications.

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

The article discusses popular Python libraries like NumPy, Pandas, Matplotlib, Scikit-learn, TensorFlow, Django, Flask, and Requests, detailing their uses in scientific computing, data analysis, visualization, machine learning, web development, and H

In Python, how to dynamically create an object through a string and call its methods? This is a common programming requirement, especially if it needs to be configured or run...
