Binary tree recursive traversal and non-recursive Traversal

Source: Internet
Author: User

1. binary tree traversal Definition
Traversing a binary tree means accessing all nodes in the binary tree in a certain order, so that each node is accessed only once. The "access" mentioned here refers to a certain operation on the node, which can be the output node information and the modification of the node value. However, such access is required not to damage its original data structure. In this article, the traversal operation is to access and output the node value, and the binary linked list is used as the storage structure of the binary tree. Because a binary tree is a non-linear structure, each node may have more than one direct successor. Therefore, you must specify the traversal rules and traverse the binary tree according to these rules, finally, a linear sequence of all nodes in the binary tree is obtained.


If L, R, and D represent the left subtree, right subtree, and root node of the binary tree, there are six rules for traversing the binary tree: DLR, DRL, LDR, LRD, RDL, and RKD. If left and right are required in a binary tree (the order between left and right cannot be reversed), only three traversal Rules are available: DLR, LDR, and LRD. DLR is called the forward root traversal (or the forward, first, and first root traversal). LDR is called the middle root traversal (or middle-order traversal ), LRD is called post-root traversal (or post-sequential traversal ).


The binary tree structure is defined:
Typedef struct _ BinaryTreeNode
{
Int value;
Struct _ BinaryTreeNode * pLeft; // pointer to the left subnode
Struct _ BinaryTreeNode * pRight; // pointer to the right subnode
} BinaryTreeNode_t;


2. Root Traversal
Root traversal is the first traversal of the root node, followed by the Left subtree, and finally the right subtree.

2.1 recursive Traversal
The recursive traversal algorithm of the first root traversal binary tree is described as follows:

If the binary tree is empty, the algorithm ends. Otherwise
1) output the root node;
2) traverse the left subtree with the root;
3) traverse the right subtree first;


Void PreOrder (BinaryTreeNode_t * pRoot)
{
BinaryTreeNode_t * p;

P = pRoot;

If (p! = NULL)
{
Std: cout <p-> value <"";
PreOrder (p-> pLeft );
PreOrder (p-> pRight );
}
}


2.2 Non-recursive Traversal
Using stack to store Binary Tree nodes, the algorithm IDEA is:
Starting from the root node of the binary tree, the left subtree goes all the way to the end (left blank. During the process, access the node and sequentially Add the node to the stack. When the left subtree is empty, exit a node from the top of the stack, and point the pointer to the right child of the node. This is repeated until the stack is null or the pointer is null.
Void PreOrder (const BinaryTreeNode_t * pRoot)
{
Std: stack <BinaryTreeNode_t *> s;
BinaryTreeNode_t * p;
P = pRoot;


While (p! = NULL | s. size ()> 0)
{
While (p! = NULL)
{

// Access the current node

Std: cout <p-> value <std: endl;


// Add the current node to the stack
S. push (p );


// Access his left son
P = p-> pLeft;
}


// Process the left son and then the right son
P = s. top ();
S. pop ();
P = p-> pRight;
}
}


3. Root Traversal
The so-called root traversal means that the root is in the middle. First the left subtree, then the root node, and finally the right subtree.

3.1 recursive Traversal
The recursive traversal algorithm of the root traversal binary tree is described as follows:

If the binary tree is empty, the algorithm ends. Otherwise
1)The middle root traverses the left subtree;
2)Output the root node;
3)The middle root traverses the right subtree;


Void InOrder (BinaryTreeNode_t * pRoot)
{
BinaryTreeNode_t * p;
P = pRoot;
If (p! = NULL)
{
InOrder (p-> pLeft );
Cout <p-> value <"";
InOrder (p-> pRight );
}
}



3.2 non-recursive Traversal
We also use stacks to store Binary Tree nodes. The algorithm IDEA is as follows:
Starting from the root node of the binary tree, the left subtree is always at the end (left child is empty). During the process, the node encountered in sequence is pushed to the stack, when the left subtree is empty, the node is exited from the stack and accessed, and then switched to its right subtree. This is repeated until the stack is null or the pointer is null.


Void InOrder (const BinaryTreeNode_t * pRoot)
{
Stack <BinaryTreeNode_t *> s;
BinaryTreeNode_t * p;
P = pRoot;


While (p! = NULL | s. size ()> 0)
{

// Always visit the left son

While (p! = NULL)
{
S. push (p );
P = p-> pLeft;
}


// Point to the top element of the stack, that is, the node with the left son being NULL.
P = s. top ();


// Pop up the node and access
S. pop ();
Std: cout <p-> value <std: endl;


// Handle the right son of another user
P = p-> pRight;
}
}


4. Post-root Traversal
The so-called root traversal means that the root is at the end, that is, the left subtree, the right subtree, And the last root node.

4.1 recursive Traversal
The recursive traversal algorithm of the next root traversal binary tree is described as follows:
If the binary tree is empty, the algorithm ends. Otherwise
1) The back root traverses the left subtree:
2) the back end is rooted in the calendar subtree;
3) access the root node.


Void PostOrder (BinaryTreeNode_t * pRoot)
{
BinaryTreeNode_t * p;
P = root;
If (p! = NULL)
{
PostOrder (p-> pLeft );
PostOrder (p-> pRight );
Cout <p-> value <"";
}
}


4.2 non-recursive Traversal
Using stacks to implement post-root traversal of Binary Trees is much more complex than pre-and mid-order traversal. In post-root traversal, when the search Pointer Points to a node, the node cannot be accessed immediately, but the left subtree must be traversed first. Therefore, the node should be saved on the stack first, after traversing the left subtree, return to the node again and you cannot access it. You also need to traverse the right subtree first. This node can be accessed only when its right subtree is traversed and then rolled back to the stack again.
Void PostOrder (const BinaryTreeNode_t * pRoot)
{
Stack <BinaryTreeNode_t *> s;
BinaryTreeNode_t * p;

BinaryTreeNode_t * pre = NULL;
P = pRoot;


While (p! = NULL | s. size ()> 0)
{

// Traverse the left subnode from the left
While (p! = NULL)
{
S. push (p );
P = p-> pLeft;
}

// Point to the top element of the stack, that is, the last node whose left son is NULL.
P = s. top ();
// P = p-> pRight;


// If the right son of the node is empty, the node is a leaf node to access the node.

// If the right son of the node has been accessed, access the node

If (p-> pRight = NULL | p-> pRight = pre)
{
// P = s. top ();
Std: cout <p-> value <std: endl;
S. pop ();

Pre = p;
P = NULL;
}

// The right son of the node is not empty. It also traverses the child tree whose right son is the root node.

Else

P = p-> pRight;
}
}

This article is from the redpaopaw blog, please be sure to keep this source http://redpaopaw.blog.51cto.com/7900594/1297972

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.