C/C++ data structure: recursive traversal of binary tree

The recursive traversal operation of the binary tree is divided into pre-order traversal, middle-order traversal, and post-order traversal according to the traversal order of the root node.

Pre-order traversal:
before traversing the left and right nodes of the tree with the current node as the root node (at this time, the left and right nodes have not been traversed yet), output the value of the current node.
As shown in the figure below, the result of pre-order traversal should be:

A B D E C F G

Preorder traversal (illustration):
Insert picture description here

Middle-order traversal:
before traversing the right node of the tree with the current node as the root node (the left node has been traversed at this time), output the value of the current node.
In the binary tree as shown in the figure below, the middle-order traversal result should be:

D B E A F C G

In-order traversal (illustration):
Insert picture description here

Post-order traversal:
After traversing the left and right nodes of the tree with the current node as the root node (at this time both the left and right nodes have been traversed), output the value of the current node.
In the binary tree as shown in the figure below, the post-order traversal result should be:

D E B F G C A

Post-order traversal (illustration):
Insert picture description here

Layer sequence traversal:
In addition to the above three traversal methods, the binary tree also has a layer sequence traversal method, which traverses all nodes in turn from top to bottom and from left to right. The implementation of layer sequence traversal requires the help of queues, which are somewhat different.
As shown in the above binary tree, the traversal result of the sequence should be:

A B C D E F G

The specific implementation is as follows.

Code:

#include <iostream>
#include <queue>

using namespace std;

struct TNode {
    
    
	char _val;
	TNode* _left;
	TNode* _right;
	TNode(char val):_val(val), _left(NULL), _right(NULL) {
    
    }
};

class Tree {
    
    
	public:
		Tree();
		~Tree();
	
	public:
		void PreOrder();  //先序遍历
		void InOrder();   //中序遍历
		void PostOrder(); //后序遍历 
		void LevelOrder();//层序遍历 
	
	private:
		//为了避免类的调用者访问类的私有成员 root_ 
		//将具体操作封装为类私有函数供公共接口调用 
		TNode* Creat();            //创建树 
		void Release(TNode* root); //释放树 
		void preOrder(TNode* root);
		void inOrder(TNode* root); 
		void postOrder(TNode* root); 
		void levelOrder(TNode* root);
	
	private:
		TNode* root_;
};

TNode* Tree::Creat() {
    
    
	TNode* temp;
	char val;
	
	cin >> val;
	
	//若输入的节点的值为 # ,则表示以当前节点为根节点创建一棵空树 
	if (val == '#') temp = NULL;
	else {
    
    
		temp = new TNode(val);
		temp->_left = Creat();
		temp->_right = Creat();
	}
	
	return temp;
}

void Tree::Release(TNode* root) {
    
    
	if (root == NULL) return ;
	else {
    
    
		Release(root->_left);
		Release(root->_right);
		delete root;
	}
}

void Tree::preOrder(TNode* root) {
    
    
	//若以当前节点为根节点的树为空,则退出当前节点的遍历 
	if (root == NULL) return ;
	else {
    
    
		cout << root->_val << ' ';//输出节点内的值。注意输出位置(前) 
		preOrder(root->_left);
		preOrder(root->_right);
	}
}

void Tree::inOrder(TNode* root) {
    
    
	if (root == NULL) return ;
	else {
    
    
		inOrder(root->_left);
		cout << root->_val << ' ';//输出节点内的值(中) 
		inOrder(root->_right);
	}
}

void Tree::postOrder(TNode* root) {
    
    
	if (root == NULL) return ;
	else {
    
    
		postOrder(root->_left);
		postOrder(root->_right);
		cout << root->_val << ' ';//输出节点内的值(后) 
	}
}

void Tree::levelOrder(TNode* root) {
    
    
	//层序遍历。利用队列实现
	queue<TNode*> q;
	
	if (root == NULL) return ;
	else {
    
    
		//若树不为空,将树的根节点入队
		q.push(root);
	}

	while (!q.empty()) {
    
    
		TNode* temp = q.front();
		q.pop();
		cout << temp->_val << ' ';
		//从队列中弹出当前节点并输出值后,将以当前节点为根节点的树的左右子树入队
		//若当前节点为空(深度已经大于叶子节点),则不入队 
		if (temp->_left != NULL) q.push(temp->_left);
		if (temp->_right != NULL) q.push(temp->_right);
	}
}

Tree::Tree() {
    
    
	root_ = Creat();
}

Tree::~Tree() {
    
    
	Release(root_);
}

void Tree::PreOrder() {
    
    
	cout << "前序遍历:"; 
	preOrder(root_);
	cout << endl;
} 

void Tree::InOrder() {
    
    
	cout << "中序遍历:";
	inOrder(root_);
	cout << endl;
}

void Tree::PostOrder() {
    
    
	cout << "后序遍历:";
	postOrder(root_);
	cout << endl;
}

void Tree::LevelOrder() {
    
    
	cout << "层序遍历:";
	levelOrder(root_);
	cout << endl;	
} 

int main() {
    
    
	//测试数据:A B D # # E # # C F # # G # #
	Tree t;
	
	t.PreOrder();
	t.InOrder();
	t.PostOrder();
	t.LevelOrder();
	
	return 0;
}

operation result:
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45711556/article/details/108933668