A binary tree for DS Traversal

Source: Internet
Author: User

A binary tree for DS Traversal

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 <e <","; return OK ;}

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 <iostream> # include <Cstdlib> 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 First Order. The space character indicates the empty tree and the 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 <e <","; return OK;} int main () {BiTree T; // construct the binary tree CreateBiTree (T); // first input the data element cout of the Binary Tree in sequence <"output of the first traversal Binary Tree:"; PreOrderTraverse (T, printElement ); cout <endl; cout <"output of a central traversal Binary Tree:"; InOrderTraverse (T, printElement); cout <endl; cout <"output of a post-sequential traversal Binary Tree: "; PostOrderTraverse (T, printElement); cout <endl; return 0 ;}

Input data: First input ABC # DE # G # F ### (# indicates null)

The figure of this tree is:

 

The output result is:

 


 

 


 

 


 





 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.