Binary tree traversal without recursion and auxiliary space

Source: Internet
Author: User

Recursive and non-recursive binary tree traversal all require auxiliary space in a sense. Is it possible to perform non-recursive traversal without any auxiliary space? The answer is yes. The application of the clue binary tree can take advantage of the Left or right subtree nodes with null values. After the binary tree clue, you may find the frontend or successor of a node. For a Binary Tree Containing n nodes, n + 1 pointers may be null. With this n + 1 pointer, all binary tree clues can be made without missing any node.

Next we will use this clue to traverse the central traversal of a binary tree. Now we will only study the central traversal:

1. Set the variable current = root.

2. If the left subtree of current is empty, print the value of the node and set current to current-> rchild.

3. If the left subtree of current is not empty, locate the left subtree and traverse the last node sequentially. Since it is the last one, the right subtree of this node is empty, let it point to current, and then let current point to its left subtree.

The following code does not destroy the structure of the original tree. After online cable-based traversal, the original structure of the tree is restored. below is the code:


[Cpp]
# Include <iostream>
Using namespace std;
 
Typedef struct tree_node_s {
Int value;
Struct tree_node_s * lchild;
Struct tree_node_s * rchild;
} Tree_node_t;
 
 
Tree_node_t * createNode (int value ){
Tree_node_t * node = (tree_node_t *) malloc (sizeof (tree_node_t ));
Node-> value = value;
Node-> lchild = NULL;
Node-> rchild = NULL;
Return node;
}
 
Void inorderTraverse (tree_node_t * root ){
If (root ){
InorderTraverse (root-> lchild );
Cout <root-> value <"";
InorderTraverse (root-> rchild );
}
}
 
Void iterativeTraverse (tree_node_t * root ){
Tree_node_t * current = root;
Tree_node_t * prev = NULL;
While (NULL! = Current ){
If (NULL = current-> lchild ){
Cout <current-> value <"";
Current = current-> rchild;
} Else {
Prev = current-> lchild;
While (prev-> rchild & prev-> rchild! = Current)
Prev = prev-> rchild;
If (NULL = prev-> rchild ){
Prev-> rchild = current;
Current = current-> lchild;
} Else {
Prev-> rchild = NULL;
Cout <current-> value <"";
Current = current-> rchild;
}
}
}
}
 
 
Int main (int argc, char * argv []) {
Tree_node_t * root = createNode (1 );
Root-> lchild = createNode (2 );
Root-> rchild = createNode (3 );
Root-> lchild = createNode (4 );
Root-> lchild-> rchild = createNode (5 );
IterativeTraverse (root );
Cout <endl;
InorderTraverse (root );
Cin. get ();
Return 0;
}

# Include <iostream>
Using namespace std;

Typedef struct tree_node_s {
Int value;
Struct tree_node_s * lchild;
Struct tree_node_s * rchild;
} Tree_node_t;


Tree_node_t * createNode (int value ){
Tree_node_t * node = (tree_node_t *) malloc (sizeof (tree_node_t ));
Node-> value = value;
Node-> lchild = NULL;
Node-> rchild = NULL;
Return node;
}

Void inorderTraverse (tree_node_t * root ){
If (root ){
InorderTraverse (root-> lchild );
Cout <root-> value <"";
InorderTraverse (root-> rchild );
}
}

Void iterativeTraverse (tree_node_t * root ){
Tree_node_t * current = root;
Tree_node_t * prev = NULL;
While (NULL! = Current ){
If (NULL = current-> lchild ){
Cout <current-> value <"";
Current = current-> rchild;
} Else {
Prev = current-> lchild;
While (prev-> rchild & prev-> rchild! = Current)
Prev = prev-> rchild;
If (NULL = prev-> rchild ){
Prev-> rchild = current;
Current = current-> lchild;
} Else {
Prev-> rchild = NULL;
Cout <current-> value <"";
Current = current-> rchild;
}
}
}
}


Int main (int argc, char * argv []) {
Tree_node_t * root = createNode (1 );
Root-> lchild = createNode (2 );
Root-> rchild = createNode (3 );
Root-> lchild = createNode (4 );
Root-> lchild-> rchild = createNode (5 );
IterativeTraverse (root );
Cout <endl;
InorderTraverse (root );
Cin. get ();
Return 0;
}

 

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.