左神基础班- 序列化-反序列化一棵树

本文介绍了一种非递归方法实现二叉树的序列化与反序列化过程。序列化采用特定分隔符连接节点值,并用特殊标记表示空节点;反序列化则通过解析序列并构建对应的二叉树结构。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

(非递归)序列化方法:

每个节点值之间用'_' 分隔开。然后空位置用'#'表示 

string getTreeWithSerial(TreeNode* head){
	if(head == nullptr){
		return "#_";
	}
	char c[8];
	sprintf(c,"%d",head->value);
	string s = c;
	s = s+ '_';
	string s1 = getTreeWithSerial(head->left);
	string s2 = getTreeWithSerial(head->right);
	return s + s1 + s2;
}

 

(非递归)序列化方法:

下面第一个函数在干什么事呢? 

把字符数以‘_’为分隔符,分割成多个char* 类型的东西,然后把每个char* 所指向的字符串 转成string,存在队列里。

 

下面第二个函数是办事情的函数: (注意参数需要时引用)每次用队列中取出一个元素作为节点,然后递归的连接其左子树和有子树。  so  第二个函数是从队列中取数据 建树的过程


TreeNode* createTreeWithSerial(string str){
	// atoi(str.c_str());
	queue<string> node_queue;
	char* s = (char*)str.data();
	const char* d = "_";
	char* help_c;
	int help_int;
	string help_str;
	//string 分割 char* 
	help_c = strtok(s,d);
	//char*  --> string
	help_str = help_c;
	//遍历多个char *
	while(help_c){
		help_str = help_c;
		node_queue.push(help_str);
		help_c = strtok(NULL, d);
	}
	return process(node_queue);
}
TreeNode* process(queue<string>& node_queue){
	if(!node_queue.empty()){
		string help = node_queue.front();
		node_queue.pop();
		// cout << "now, the value is " << help << endl;
		if(help == "#"){
			return nullptr;
		}
		TreeNode* node = new TreeNode(atoi(help.c_str()));
		node->left = process(node_queue);
		node->right = process(node_queue);
		return node;
	}
	return nullptr;
}

 

总的代码:

#include<stack>
#include<iostream>
#include<queue>
#include<string>
using namespace std;
struct TreeNode{
	int value;
	TreeNode* left;
	TreeNode* right;
	TreeNode(){}
	TreeNode(int value){
		this->value = value;
		left = nullptr;
		right = nullptr;
	}
};
TreeNode* getTree(int arr[],int length,int i);
TreeNode* process(queue<string>& node_queue);
string getTreeWithSerial(TreeNode* head);
TreeNode* createTreeWithSerial(string str);
int main(){
	//用序列化建成一颗树
	string preStr = "1_2_4_#_#_5_#_#_3_6_#_#_7_#_#_";
	TreeNode* head2 = createTreeWithSerial(preStr);
	//用该数得到其序列化结果
	string str2 = getTreeWithSerial(head2);
	cout << str2 <<endl;
	return 0;
}

string getTreeWithSerial(TreeNode* head){
	if(head == nullptr){
		return "#_";
	}
	char c[8];
	sprintf(c,"%d",head->value);
	string s = c;
	s = s+ '_';
	string s1 = getTreeWithSerial(head->left);
	string s2 = getTreeWithSerial(head->right);
	return s + s1 + s2;
}

TreeNode* createTreeWithSerial(string str){
	// atoi(str.c_str());
	queue<string> node_queue;
	char* s = (char*)str.data();
	const char* d = "_";
	char* help_c;
	int help_int;
	string help_str;
	//string 分割 char* 
	help_c = strtok(s,d);
	//char*  --> string
	help_str = help_c;
	//遍历多个char *
	while(help_c){
		help_str = help_c;
		node_queue.push(help_str);
		help_c = strtok(NULL, d);
	}
	return process(node_queue);
}
TreeNode* process(queue<string>& node_queue){
	if(!node_queue.empty()){
		string help = node_queue.front();
		node_queue.pop();
		// cout << "now, the value is " << help << endl;
		if(help == "#"){
			return nullptr;
		}
		TreeNode* node = new TreeNode(atoi(help.c_str()));
		node->left = process(node_queue);
		node->right = process(node_queue);
		return node;
	}
	return nullptr;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值