[Data structure of binary tree sorting algorithm]

Binary tree is a very important tree structure, its storage structure and operations are relatively simple, and the tree is also easy to convert into a binary tree, this article mainly discusses the traversal of a binary tree, which is divided into three kinds of traversal: preorder, middle order and postorder Way, use recursion to traverse the code introduction is easy to understand. The following first introduces recursive pre-order traversal and non-recursive middle-order and post-order traversal.

Definition of binary tree traversal

  1. Traverse first.
    If the binary tree is empty, then no operation; otherwise, perform the following operations in sequence.
  • Visit the root node
  • First traverse the left subtree of the root node
  • Traverse the right subtree of the root node first
  1. In-order traversal
    If the binary tree is empty, then no operation; otherwise, perform the following operations in sequence.
  • Traverse the left subtree of the root node in middle order
  • Visit the root node
  • Traverse the right subtree of the root node in middle order
  1. Post-order traversal
    If the binary tree is empty, no operation; otherwise, perform the following operations in sequence.
  • Post-order traverse the left subtree of the root node
  • Post-order traverse the right subtree of the root node
  • Visit the root node

The binary tree example diagram
does not match the type defined in this article.

Type description of binary tree

typedef struct BTNode
{
    
    
	int elem;
	struct BTNode *left,*right;
}*BinTree;

【Pre-order traversal】

void PreOrder(BinTree root)
{
    
    
	if(root!=NULL)
	{
    
    
		printf("%4d",root->elem);  /*访问根结点*/
		PreOrder(root->left);      /*先序遍历根结点的左子树*/
		PreOrder(root->right);     /*先序遍历根结点的右子树*/
	}
}

In-order traversal and subsequent recursive traversal
Without too much introduction, let's start with non-recursive traversal.

[Non-recursive in-order traversal]
The main idea of ​​the non-recursive algorithm of binary tree in-order traversal is to make the variable root a pointer to the root node, and traverse from the root node. Obviously, the root node is not accessed the first time it is encountered, but is pushed into the stack, because at this time the root node pointed to by root and its right subtree have not been accessed yet, so root must be saved in the stack so that it can be accessed after After the left subtree, take root from the stack, and access the root node and its right subtree. After root enters the stack, it traverses its left subtree in the middle order, that is, assigns root's left child to root, and walks down the left chain until the left chain is empty and the left subtree is traversed, and the node goes out. Assign the stack element to root. This is the second time that the node is encountered. At this time, the left subtree has been accessed. According to the definition of the middle order traversal, visit the root node (print the information of the node), and then the middle order Traverse its right subtree, that is, assign root's right child to root, and repeat the above process until root is an empty stack.
It is roughly summarized as:

  1. Check the left side after stacking. Check the right side after being unstacked.
  2. The feature of non-recursive in-order traversal is the advanced stack root node, and then it is judged whether there is a left node
  3. If it continues to push the stack, if it is empty, pop the stack, after popping the stack, judge whether there is a right node
  4. If there is a right node into the stack. And so on. The action of pushing into the stack is continuous (as long as there is a left node, it keeps pushing into the stack), and
    the action of pulling out of the stack is only one time. Because after popping the stack, it will be judged whether there is a right node, if there is and the node is not empty, the above process will be repeated, otherwise the popping will continue.

Define the stack

#define MaxSize
typedef struct
{
    
    
	BinTree elem[MaxSize];
	int top;
}SeqStack;

The algorithm description of the non-recursive realization of middle-order traversal:

void InOrder(BinTree root)
{
    
    
	SeqStack s;
	s.top=-1;
	do
	{
    
    
		while(root!=NULL)
		{
    
    
			s.top++;
			if(s.top>=MaxSize-1)
			{
    
    
				printf("栈已经满了!\n");
				return ;
			}
			else
			{
    
    
				s.elem[s.top]=root;
				root=root->left;
			}
		}
		if(s.top!=-1)
		{
    
    
			root=s.elem[s.top];
			s.top--;
			printf("\n%4d",root->elem);
			root=root->right;
		}
	}while((s.top!=-1)||(root!=NULL));
	
}

[Non-recursive subsequent traversal]

  1. Set the top of the stack to -1. When the root node is not empty, push to the stack and set the number of visits of the node to zero, and judge again whether there is a left element, and if there is, continue to push the stack. (The left element is empty end)
  2. Pop the stack, after exiting, determine whether the stack is empty, and take out the top element of the stack when it is not empty. Determine whether there is a right child or whether the number of visits is zero.
  3. If there is no right child and the number of visits is 1, the stack will be popped. Otherwise (that is, when there is a right child and the number of visits is zero), set the number of visits of the node to 1. Then assign the address of the right child to the node.
  4. Determine whether the node is empty. When it is not empty, push the stack and set the number of visits of the node to zero. Access the left element. Cycle in turn (combined code)
void PostOrder(BinTree root)
{
    
    
	SeqStack s;
	s.top=-1;
	while(root!=NULL)
	{
    
    
		s.top++;
		if(s.top==MaxSize-1)
		{
    
    
			printf("栈已经满了!\n");
			printf("Error");
		}
		else
		{
    
    
			root->count=0;
			s.elem[s.top]=root;
			root=root->left;
		}
	}
	while(s.top!=-1)
	{
    
    
		root=s.elem[s.top];
		if(root->right==NULL||root->count==1)
		{
    
    
			printf("\n%c",root->elem);
			s.top--;
		}
		else if(root->right!=NULL&&root->count!=1)
		{
    
    
			root->count=1;
			root=root->right;
			while(root!=NULL)
			{
    
    
				s.top++;
				s.elem[s.top]=root;
				root->count=0;
				root=root->left;
			}
		}
	}
}

Pre-order traversal: ABDECF
Middle-order traversal: DBEAFC
Post-order traversal: DEBFCA

Guess you like

Origin blog.csdn.net/qq_37640410/article/details/108196564