Binary tree traversal and summary

Foreword

Since this project has recently been busy target detection, there has been no time to stop and summarize one of the classic algorithms: binary tree. Now I will introduce you define a binary tree and its application, to facilitate a better understanding of the algorithm.

1. Binary Tree

Binary Tree is a special tree, it has the following characteristics :

1. Each node in the tree can have up to two trees, i.e. each node of at most 2.   

2. binary tree has about sub-divided, that is, the left subtree and right subtree , the order can not be reversed.   

3. binary tree even if only one child, but also to distinguish between the left or right subtree subtree.

2. binary tree traversal

The following traversal of the binary tree as an example:

Pre-order traversal 2.1

Idea : first visit the root node, then preorder left subtree, then preorder right subtree. Overall root - left - right on FIG preorder result of: 1,2,4,8,9,5,3,6,7.

2.2 preorder

Idea : first in order to access the left subtree, then visit the root, and finally in order to access the right subtree. Overall left - Root - the right, as the figure above preorder results: 8,4,9,2,5,1,6,3,7

2.3 preorder

Thinking : has access sequence the left subtree, then the right subtree subsequent visit, the last access to the root. Was generally left - right - roots, after the results of FIG traversal order: 8,9,4,5,2,6,7,3,1

2.4 traverse the level (breadth-first traversal)

Idea : using the queue , in turn, root, left subtree, right subtree into the queue, according to the queue of the FIFO rules to achieve the level traversal. After the results of FIG preorder: 1,2,3,4,5,6,7,8,9

2.5 depth-first traversal

Idea : use the stack , the stack root first, then the stack root, and the root of the right subtree, left sub-tree into a stack, in accordance with the stack of the post-advanced rules to achieve a depth-first traversal. After the results of FIG preorder: 1,2,4,8,9,5,3,6,7

 

 

Reference connection: https://www.cnblogs.com/lliuye/p/9143676.html

Guess you like

Origin www.cnblogs.com/shierlou-123/p/11420978.html