Binary Tree Detailed

Source: Internet
Author: User
Tags printf

Binary tree recursive and non-recursive implementation code:

http://blog.csdn.net/fangfang_666/article/details/52664172




Recursive implementation of one or three traversal modes (relatively simple, not detailed here

1, first order traversal-follow the "root node-left child-right child" in the order of access.

2, middle sequence traversal-follow the "left child-root node-right child" in the order of access.

void In_traverse (BTree pTree)
{
	if (pTree)
	{
		if (ptree->plchild)
			In_traverse (ptree-> Plchild);
		printf ("%c", ptree->data);
		if (ptree->prchild)
			in_traverse (ptree->prchild);	
	}
}

3, post-order traversal-Follow the "left child-right child-root node" sequence to access

void Beh_traverse (BTree pTree)
{
	if (pTree)
	{
		if (ptree->plchild)
			Beh_traverse (ptree- >plchild);
		if (ptree->prchild)
			beh_traverse (ptree->prchild);	
		printf ("%c", Ptree->data);
}

Two, non-recursive implementation of three kinds of traversal methods

For the sake of understanding, here the following figure of the two fork tree as an example, the analysis of the binary tree three ways of implementation of the process.


1. Non-recursive implementation of pre-sequence traversal

according to the sequence of the first order traversal, first access the root node, then access the left subtree, and then access the right subtree, and for each subtree, and in the same order of access to traverse, the first sequence of the above diagram traversal order: ABDECF. The idea of non-recursive implementation is as follows:

For either node p,

1) Output node p, then put it into the stack, and then see if the left child of P is empty;

2) If P's left child is not empty, then P's left child is the current node, repeat 1) operation;

3) If the left child of P is empty, the top node of the stack is out of the stack, but not output, and the right child of the stack node is placed as the current node to see if it is empty;

4) If not empty, then cycle to 1) operation;

5) If it is empty, then continue out of the stack, but not output, at the same time the right child of the stack node is set to the current node, see if it is empty, repeat 4) and 5) operation;

6) until the current node p is null and the stack is empty, the traversal ends.

The following figure is an example of a non-recursive implementation of the its first sequence traversal in detail:

First, starting from root node A, according to Action 1), output A, and put it into the stack, because A's left child is not empty, according to Operation 2), B is the current node, and then according to Operation 1), will b output, and put it into the stack, because B's left child is not empty, according to Operation 2), D is the current node, and then according to Operation 1), Output d, and put it into the stack, when the output sequence is abd;

Because the left child of D is empty, according to Operation 3), stack top node D is out of the stack, but not output, and its right child is placed as the current node;

Because the right child of D is empty, according to Operation 5), continue to stack top Node B out of the stack, but do not output, and put its right child as the current node;

Because B's right child e is not empty, according to Operation 1), output e, and put it into the stack, at this time the output sequence is: Abde;

Because the left child of E is empty, according to Operation 3), the stack top node E is out of the stack, but does not output, and its right child is placed as the current node;

Because the right child of E is empty, according to Operation 5), continue to stack top node A out of the stack, but not output, and its right child is the current node;

Since A's right child C is not empty, according to Operation 1), output C, and put it into the stack, at this time the output sequence is: Abdec;

Since A's left child F is not empty, according to Operation 2), the F is set to the current node, and then according to Operation 1), output F, and put it into the stack, at this time the output sequence is: ABDECF;

Since the left child of f is empty, according to Operation 3, the top node F of the stack is out of stack, but not output, and the right child is placed as the current node;

Because F's right child is empty, according to Operation 5), continue to stack top element c out of the stack, but do not output, and put its right child as the current node;

At this point the stack is empty, and C's right child is null, so the traversal ends.

void Pre_traverse (BTree pTree)
{
	Pstack stack = Create_stack ();  Create an empty stack
	BTree node_pop;                 Used to save the stack node
	BTree pcur = pTree;             Defines a pointer to the node that is currently accessed

	//until the current node pcur is null and the stack is empty, the loop ends while
	(Pcur | |!is_empty (STACK))
	{
		//starts at the root node, outputs the current node and put it into the stack,
		//set its left child as the current node until it has no left child, and the current node is null
		printf ("%c", pcur->data);
		Push_stack (stack,pcur);
		Pcur = pcur->plchild;
		If the current node pcur is null and the stack is not empty, the top node of the stack is out of the stack,
		//At the same time the right child is the current node, and the loop is judged until pcur is not empty while
		(!pcur &&!is_empty (Stack))
		{
			pcur = getTop (stack);
			Pop_stack (Stack,&node_pop);
			Pcur = pcur->prchild;			
		}}}

2. Non-recursive implementation of middle sequence traversal

According to the order of the sequence traversal, the left subtree is accessed first, the root node is accessed, and the right subtree is accessed, and for each subtree, it is traversed in the same order of access, and the sequence traversal order of the above image is: DBEAFC. The idea of non-recursive implementation is as follows:

For either node p,

1) If the left child of P is not empty, then P is put into the stack and the left child of P is placed as the current node, then the same processing is done for the current node;

2) If the left child of P is empty, then the P node is output, then the right child of P is placed as the current node to see if it is empty;

3) If not empty, then repeat 1) and 2) operation;

4) If it is empty, then execute the stack operation, output stack top node, and the right child of the node that is out of the stack as the current node, see whether it is empty, repeat 3) and 4) operation;

5) until the current node p is null and the stack is empty, the traversal ends.


The following figure is an example of the non-recursive implementation of the sequential traversal in detail:

First, starting from the root node A, the left child of a is not empty, according to Operation 1) A into the stack, then the B is the current node, B's left child is not empty, according to Operation 1), the B also into the stack, and then the D is the current node, because D is empty, according to Operation 2), Output D

Since D's right child is also empty, according to Operation 4), perform a stack operation, the stack top node B out of the stack, and the B is the current node, at this time the output sequence is db;

Because B's right child is not empty, according to Operation 3), its right child e is the current node, because E's left child is empty, according to Operation 1), output E, at this time the output sequence is DBE;

Because the right child of E is empty, according to Operation 4), perform a stack operation, stack top Node A out of the stack, and node A as the current node, at this time the output sequence is dbea;

The stack is empty at this time, however, the right child of Node A is not NULL, continue to execute, because A's right child is not empty, according to Operation 3), the right child C is the current node, because C's left child is not empty, according to Operation 1), c into the stack, its left child F is the current node, because F's left child is empty, According to Operation 2), output F, at this time the output sequence is: dbeaf;

Because F's right child is also empty, according to Operation 4), perform a stack operation, the stack top element c out of the stack, and set it as the current node, at this time the output sequence is: DBEAFC;

Because the right child of C is null, and at this time the stack is empty, according to Operation 5), the traversal ends

void In_traverse (BTree pTree)
{
	Pstack stack = Create_stack ();  Create an empty stack
	BTree node_pop;                 Used to save the stack node
	BTree pcur = pTree;             Defines a pointer to the node that is currently accessed

	//until the current node pcur is null and the stack is empty, the loop ends while
	(Pcur | |!is_empty (STACK))
	{
		if (pcur-> Plchild)
		{
			//If the left child of Pcur is not empty, it is placed on the stack and the left child is the current node
			push_stack (stack,pcur);
			Pcur = pcur->plchild;
		}
		else
		{
			///If the left child of Pcur is empty, output the Pcur node and set its right child as the current node to see if it is empty
			printf ("%c", pcur->data);
			Pcur = pcur->prchild;
			If it is empty, and the stack is not empty, the top node of the stack is out of the stack, and the node is output,
			///And its right child is set to the current node, continuing to judge until the current node is not empty while
			(!pcur &&!is_empty (Stack))
			{
				pcur = getTop (stack);
				printf ("%c", pcur->data);	
				Pop_stack (Stack,&node_pop);
				Pcur = pcur->prchild;
			}}}


3. Non-recursive implementation of post-sequential traversal


according to the order of sequential traversal, first access to the left subtree, and then access to the right subtree, and then access the root node, and for each subtree, and in the same order of access to traverse, the previous sequence traversal order is: DEBFCA. Post-order traversal of non-recursive implementation is relatively difficult, to ensure that the root node in the Saozi right subtree is accessed before access, the idea is as follows:

For either node p,

1) the node p into the stack first;

2) If p does not exist left child and right child, or p exists left child or right child, but the child has been output, you can directly output node p, and will be out of the stack, the stack node p is marked as the last output node, and then the top node of the stack at this point is set to the current nodes;

3) If the condition in 2 is not satisfied, then the right child and the left child of P are sequentially put into the stack, the current node is reset to the top node of the stack, and then repeated Operation 2);

4) until the stack is empty, the traversal ends.


The following figure is an example of the non-recursive implementation of the post-order traversal in detail:

First, set two pointers: The cur pointer points to the node that is currently being accessed, and it always points to the top node of the stack, one node at a time

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.