Depth-first traversal algorithm binary tree
这不就是二叉树吗?嗯,风景都在提示我该学学二叉树了
1. What is depth-first traversal
The depth-first traversal algorithm is a classic graph theory algorithm. Start searching from a certain node v. Continue to search until all the edges of the node have been traversed. When all the edges of the node v have been traversed, the depth-first traversal algorithm needs to go back to the v predecessor node to continue searching for this node.
Note : The depth-first traversal problem must try all possible methods according to the rules.
Two, binary tree
1. Introduction to Binary Tree
Binary tree is a special data structure. Common data structures include arrays, linked lists, graphs, queues, hash tables, and trees. The binary tree belongs to the tree structure. Each node in the binary tree has two branches called the left subtree and the right subtree. Each level of the binary tree has at most
nodes. Unlike ordinary trees, the nodes of ordinary trees have no branch restrictions, and the nodes of ordinary trees are not divided between left and right and subtrees.
2. Binary tree type
Binary tree types: empty binary tree, full binary tree, complete binary tree, perfect binary tree, balanced binary tree.
- Empty binary tree : there are zero nodes
- Perfect binary tree : every layer of nodes is full of binary trees (such as the example in Figure 1)
- Full binary tree : each node has zero or two child nodes
- Complete binary tree : beyond the last layer, the nodes of each layer are full, and the nodes of the last layer are all arranged from the left
- Balanced binary tree : The depth of the two subtrees of each node does not differ by more than 1.
Note : The definition of perfect binary tree and full binary tree are the same in China
3. Binary tree related terms
the term | Explanation |
---|---|
degree | The degree of a node is the number of subtrees of the node |
Leaf node | Node with zero degree |
Branch node | Nodes with non-zero degrees |
Child node | Two child nodes under the node |
Parent node | The source node one level above the node |
Sibling node | Nodes with the same parent node |
root | Source node of binary tree |
depth | The number of layers of nodes in the binary tree |
4. Node code of binary tree
Because each node has two child nodes connected, we only need to have the root node to find any node on the binary tree, and each node has the same definition
class Node : #二叉树节点定义
def _init_(self,x):
self.val = x #节点值
self.left = None #左侧子节点
self.right = None #右侧子节点
5. Binary tree traversal order
Three traversal forms of binary tree:
- DLR (first order):
- LDR (Intermediate Order):
- LRD (Post-order):
Note : L represents the left subtree R represents the right subtree; D represents the root
6. Depth-first traversal and breadth-first traversal
- Depth-first traversal : the pre-order, middle-order and post-order are all depth-first traversal
from the root node to the farthest node, - Breadth-first traversal : First visit the nearest node of the root node of the example, and progress in layers. The order of traversing the above picture in breadth-first is: 1-2-3-4-5-6-7
3. Interview questions + inspirational
Penguin O & M interview questions :
1. Binary tree traversal sequence: see above
2. Tell me how to create a binary tree in a language you are familiar with? python see above