Non-recursive algorithm for first-order traversal of binary tree implementation of _javascript techniques

Source: Internet
Author: User

In the previous article, we said that the recursive traversal algorithm of the two-fork tree (the improvement of the binary tree root (First order) traversal), this article mainly talks about the two-fork tree's non-recursive algorithm, uses the stack structure

The idea of the non recursive algorithm based on the first root traversal is summarized as follows:

1) into the stack, mainly the advance node into the stack, and then visit this node

2 while, loop through the current node until the left child has no nodes.

3 If the right child of the node is true, turn to 1 to continue to traverse, otherwise exit the current node into the parent node traversal into 1.

First look at the algorithm that fits this idea:

Copy Code code as follows:

int Preordertraversenonrecursiveex (const bitree &t, int (*visitnode) (Telemtype data)
{
if (T = = NULL)
{
return-1;
}

Bitnode *pbinode = T;
Sqstack S;
Initstack (&s);
Push (&s, (Selemtype) T);

while (! Isstackempty (S))
{
while (Pbinode)
{
Visitnode (Pbinode->data);
if (Pbinode!= T)
{
Push (&s, (Selemtype) pbinode);
}
Pbinode = pbinode->lchild;
}
if (Pbinode = NULL)
{
POPs (&s, (selemtype*) &pbinode);
}
if (Pbinode->rchild = NULL)
{
POPs (&s, (selemtype*) &pbinode); If the stack is empty at this time, there is a problem
}
Pbinode = pbinode->rchild;
}

return 0;
}

Note: 1 The stack structure is used here, which can be referred to in the sequential structure storage stack

2 here in the preservation of the node, I saved the pointer is the address of the node, it becomes an int storage, in the pop when the use of the pointer, so take is &pbinode, not pbinode, why do you think of the use of the pointer, The best understanding is Bitnode *pbinode, the definition changed to Bitree Pbinode very well understood.


The above algorithm is actually wrong! Why, then? Here I check for a long time, there have been endless loops, also appeared from the left subtree after the right subtree does not show, and finally I modified the first while to determine the conditions, why? Because if the stack is empty after the pop, but the right subtree is still there, can not continue, this after I wrote and did not do too much verification, and then elaborated, here and did not press the null pointer, look at the example of the pressure into the null pointer, the main left subtree is empty when the stack, as follows:

Copy Code code as follows:

int preordertraversenonrecursive (const bitree &t, int (*visitnode) (Telemtype data)
{
if (T = = NULL)
{
return-1;
}

Bitnode *pbinode = T;
Sqstack S;
Initstack (&s);
Push (&s, (Selemtype) T);

while (! Isstackempty (S))
{
GetTop (S, (selemtype*) &pbinode);
while (Pbinode)
{
Visitnode (Pbinode->data);
Pbinode = pbinode->lchild;
Push (&s, (Selemtype) pbinode);
}
if (Pbinode = NULL)
{
POPs (&s, (selemtype*) &pbinode);
}
if (! Isstackempty (S))
{
POPs (&s, (selemtype*) &pbinode);
Pbinode = pbinode->rchild;
Push (&s, (Selemtype) pbinode);
}
}

return 0;
}

Here's the thing, first press into the root node, then determine whether the Zuozi is empty, not empty on the stack, or quit while loop after the null node out of the stack, and then determine whether the current stack is empty, if not empty on the stack to get the parent node and then judge the right child, pressed into the right child node, Then determine if the left child of this right subtree is empty and continue looping.

Here are two waste places: one is to press the empty child node into the stack, and the second is to use GetTop to get the top element of the stack.


Here to return to look at the initial design of the algorithm, where there is no pressure to null pointer or empty child node, but can not output integrity, here we think can be in the decision stack to join, the current node is null can be, This will not appear the embarrassment that does not show exit left subtree node does not show right subtree node, as follows:

Copy Code code as follows:

Non-recursive first-order traversing binary tree
int Preordertraversenonrecursiveex (const bitree &t,
Int (*visitnode) (Telemtype data)
{
if (T = = NULL)
{
return-1;
}

Bitnode *pbinode = T;
Sqstack S;
Initstack (&s);
Push (&s, (Selemtype) T);

while (! Isstackempty (S) | | Pbinode)///The main change is the sentence
{
while (Pbinode)
{
Visitnode (Pbinode->data);
if (Pbinode!= T)
{
Push (&s, (Selemtype) pbinode);
}
Pbinode = pbinode->lchild;
}
if (Pbinode = NULL)
{
POPs (&s, (selemtype*) &pbinode);
}
if (Pbinode->rchild = NULL)
{
POPs (&s, (selemtype*) &pbinode); If the stack is empty at this time, there is a problem
}
Pbinode = pbinode->rchild;
}
return 0;
}

After the first while loop joins this, the test case is similar to the two-fork tree first-order traversal. The following test section of the two-tree example:

The data entered at this time is still 12 34 0 0 78 0 0, and the test results are as follows:


---bitree---
Please Enter bitree Node data:
12
Please Enter bitree Node data:
34
Please Enter bitree Node data:
0
Please Enter bitree Node data:
0
Please Enter bitree Node data:
78
Please Enter bitree Node data:
0
Please Enter bitree Node data:
0
12 34 78

This is not enough to test, then look at the following two fork tree

The input data should be: 12 34 24 0 0 50 0 0 78 37 0 0-0, the test results are as follows:

---bitree---
Please Enter bitree Node data:
12
Please Enter bitree Node data:
34
Please Enter bitree Node data:
24
Please Enter bitree Node data:
0
Please Enter bitree Node data:
0
Please Enter bitree Node data:
50
Please Enter bitree Node data:
0
Please Enter bitree Node data:
0
Please Enter bitree Node data:
78
Please Enter bitree Node data:
37
Please Enter bitree Node data:
0
Please Enter bitree Node data:
0
Please Enter bitree Node data:
0
12 34 24 50 78 37

By first-order traversal know, just right, and the other these algorithms are not only on the first order traversal, if you want to become a sequence or later, just the above algorithm in the visit and so on, and then add it to the appropriate location, you can

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.