Binary tree traversal in DS
In some applications of Binary Trees, it is often required to search for nodes with certain features in the tree, or to process all nodes in the tree one by one. This raises the issue of traversing a binary tree, that is, how to patrol each node in a tree by a specific search path so that each node is accessed once and only once.
According to the recursive definition of a binary tree, a binary tree consists of three basic units: the root node, the left subtree, And the right subtree. If we can traverse these three parts in sequence, we will traverse the entire binary tree. If the order is left-right, there are three algorithms for Traversing Binary Trees: first, middle, and last.
The first-order traversal of a binary tree operation is defined:
If the binary tree is empty, the operation is null; otherwise;
(1) access the root node; (2) traverse the left subtree first; (3) traverse the right subtree first.
The operations for traversing a binary tree in a forward direction are defined:
If the binary tree is empty, the operation is null; otherwise;
(1) traverse the left subtree in the middle order; (2) access the root node; (3) traverse the right subtree in the middle order.
The operation for traversing a binary tree in descending order is defined:
If the binary tree is empty, the operation is null; otherwise;
(1) traverse the left subtree in descending order; (2) traverse the right subtree in descending order; (3) Access the root node.
The binary tree traversal is implemented on the binary linked list of the binary tree. Four traversal methods are also introduced in the basic operation of Binary Tree binary linked list. We will implement the first three traversal methods. In addition to a pointer parameter, the parameter of the traversal function also has a pointer parameter pointing to the function. The simplest implementation of this function is the Visit () function. The simplest code for this function is:
Status printElement (TElemType e) // The simplest Visit () function {cout <
The code for constructing a binary table (first input data elements) is as follows:
Status CreateBiTree (BiTree & T) // enter the value of the node in the binary tree in the First Order (one character). The space character indicates the empty tree, create a binary tree T {int I = 0; char a [100]; cin> a [I]; if (a [I] = '#') T = NULL; else {if (! (T = (BiTNode *) malloc (sizeof (BiTNode) {exit (OVERFLOW);} T-> data = a [I]; // generate the root node CreateBiTree (T-> lchild); // construct the left subtree CreateBiTree (T-> rchild); // construct the right subtree} return OK ;}
Let's take a look at the code for first-order traversal of the binary linked list:
Status PreOrderTraverse (BiTree T, Status (* Visit) (TElemType e) // first-order traversal of Binary Trees {if (T) {if (Visit (T-> data )) {if (PreOrderTraverse (T-> lchild, Visit) {if (PreOrderTraverse (T-> rchild, Visit) {return OK ;}} return ERROR ;}} else {return OK ;}}
Let's take a look at the code of the forward traversal of the binary linked list:
Status InOrderTraverse (BiTree T, Status (* Visit) (TElemType e) // median traversal Binary Tree {if (T) {if (InOrderTraverse (T-> lchild, Visit )) {if (Visit (T-> data) {if (InOrderTraverse (T-> rchild, Visit) {return OK ;}} return ERROR ;}} else {return OK ;}}
Finally, let's look at the code for the backward traversal of the binary linked list:
Status PostOrderTraverse (BiTree T, Status (* Visit) (TElemType e) // binary tree traversal in the descending order {if (T) {if (PostOrderTraverse (T-> lchild, Visit )) {if (PostOrderTraverse (T-> rchild, Visit) {if (Visit (T-> data) {return OK ;}} return ERROR ;}} else {return OK ;}}
The complete code is:
# Include
# Include
Using namespace std; # define OK 1 # define ERROR 0 # define OVERFLOW-2 typedef char TElemType; typedef int Status; typedef struct BiTNode {TElemType data; struct BiTNode * lchild, * rchild; // left and right child pointer} BiTNode, * BiTree; Status CreateBiTree (BiTree & T) // enter the node value (one character) in the binary tree in the FIFO order ), a space character represents an empty tree and a binary tree T {int I = 0; char a [100]; cin> a [I]; if (a [I] = '#') T = NULL; else {if (! (T = (BiTNode *) malloc (sizeof (BiTNode) {exit (OVERFLOW);} T-> data = a [I]; // generate the root node CreateBiTree (T-> lchild); // construct the left subtree CreateBiTree (T-> rchild); // construct the right subtree} return OK ;} status PreOrderTraverse (BiTree T, Status (* Visit) (TElemType e) // first-order traversal of Binary Trees {if (T) {if (Visit (T-> data )) {if (PreOrderTraverse (T-> lchild, Visit) {if (PreOrderTraverse (T-> rchild, Visit) {return OK ;}} return ERROR ;}} else {return OK;} Status InOrderTraverse (BiTree T, Status (* Visit) (TElemType e) // traverse the binary tree in the middle order {if (T) {if (InOrderTraverse (T-> lchild, Visit) {if (Visit (T-> data) {if (InOrderTraverse (T-> rchild, Visit )) {return OK ;}} return ERROR ;}} else {return OK ;}} Status PostOrderTraverse (BiTree T, Status (* Visit) (TElemType e )) // post-order traversal of Binary Trees {if (T) {if (PostOrderTraverse (T-> lchild, Visit) {if (PostOrderTraverse (T-> rchild, Visit )) {if (Visit (T-> data) {return OK ;}} return ERROR ;}} else {return OK ;}} Status printElement (TElemType e) // The simplest Visit () function {cout <
Input data: First input ABC # DE # G # F ### (# indicates null)
The figure of this tree is:
The output result is: