DS树结构转换(先序转双亲)

这篇博客介绍了如何通过给定的二叉树先序遍历序列构建二叉树,并使用双亲表示法输出树的结构。作者提到在原有的二叉树算法基础上增加双亲指针,并利用层次遍历来创建双亲表示法的存储结构。代码示例中展示了C++实现,包括创建二叉树、层次遍历以及输出节点值和双亲下标的过程。

题目描述

给出一棵二叉树的特定字符先序遍历结果(空子树用字符'#'表示),构建该二叉树,并输出该二叉树的双亲表示法结果

双亲表示法的数组下标从0开始,根结点必定是在下标0元素,且根结点的双亲下标为-1,左右孩子按下标递增顺序排列,

结点下标是层次遍历顺序。

输入

第一个输入t,表示有t棵二叉树

接着t行,每行输入含特定字符的二叉树先序遍历序列

输出

共输出2t行

每棵二叉树输出两行,第一行输出各个结点的数值,第二行输出各结点的双亲下标

输入样例1

3
AB#C##D##
ABD##E##C##
AB##CDW###E#F##

输出样例1

A B D C
-1 0 0 1
A B C D E
-1 0 0 1 1
A B C D E W F
-1 0 0 2 2 3 4

NOTICE:这个题只要在原来二叉树的算法基础上加个双亲指针parent就可以,然后借助层次遍历来创建双亲表示法下的树的存储结构;(有一个地方没搞懂,就是参数为什么要引用?我单步调试了一下,发现不用引用的话根结点是空的,也就是根本就没有创建?)

#include <iostream>
#include <queue>
using namespace std;

class BiTreeNode
{
private:
	char data;
	BiTreeNode* lchild;
	BiTreeNode* rchild;
	BiTreeNode* parent;
public:
	BiTreeNode():data(' '),lchild(nullptr),rchild(nullptr),parent(nullptr) {}
	friend class BiTree;
};

class BiTree
{
private:
	BiTreeNode* root;
	//根据题目要求定义的新的属性
	BiTreeNode* a[1000];//这里非常关键,数组存储的是指针而非数据,方便find函数
	int b[1000];//双亲下标
	int len;//两个数组的长度

	void Create(BiTreeNode* &t,BiTreeNode* par)//指针传引用???
	{
		char ch;
		cin >> ch;
		if (ch != '#')
		{
			t = new BiTreeNode;
			t->data = ch;
			t->parent = par;
			Create(t->lchild, t);
			Create(t->rchild, t);
		}
		else
			t = nullptr;
	}
	int find(BiTreeNode* t)//根据传过来的双亲指针,找到对应的双亲下标
	{
		if (t == nullptr)
			return -1;

		for (int i = 0; i < 1000; i++)
		{
			if (t == a[i])
				return i;
		}
	}
public:
	void Create()
	{
		Create(root, nullptr);
	}
	void BFS()
	{
		queue<BiTreeNode*> q;
		int index = 0;//数组a的下标
		if (root != nullptr)
		{
			q.push(root);
			while (!q.empty())
			{
				a[index] = q.front();
				b[index] = find(q.front()->parent);
				index++;

				if (q.front()->lchild != nullptr)
					q.push(q.front()->lchild);
				if (q.front()->rchild != nullptr)
					q.push(q.front()->rchild);
				q.pop();

			}
		}
		len = index;
	}
	void display()
	{
		for (int i = 0; i < len; i++)
		{
			cout << a[i]->data;
			if (i == len - 1)
				cout << endl;
			else
				cout << " ";
		}
		for (int i = 0; i < len; i++)
		{
			cout << b[i];
			if (i == len - 1)
				cout << endl;
			else
				cout << " ";
		}
	}
};

int main()
{
	int t;
	cin >> t;
	while (t--)
	{
		BiTree mytree;
		mytree.Create();
		mytree.BFS();
		mytree.display();
	}
	return 0;
}

### 将二叉树双亲表示法转换遍历 为了实现这一目标,可以采用递归的方法来重建并遍历二叉树。具体来说: 给定一个数组形式的双亲表示法,其中`parent[i]`指向节点`i`的父亲节点索引,而根节点具有特殊的父亲指针值(通常设为-1)。通过查找这个特殊标记的位置,能够定位到根节点。 一旦找到了根节点,则可以通过访问其对应的左右孩子来进行进一步处理。对于每一个非叶节点,在执行遍历时应按照“当前节点 -> 左子树 -> 右子树”的顺依次打印或记录各个节点的数据[^3]。 下面是具体的Python代码示例用于完成上述过程: ```python def preorder_traversal(parent_list, data_list): visited = set() def dfs(index): if index not in visited and parent_list[index] != '#': print(data_list[index], end='') # 执行操作 visited.add(index) left_child_index = find_left_child(parent_list, index) right_child_index = find_right_child(parent_list, index) if left_child_index is not None: dfs(left_child_index) if right_child_index is not None: dfs(right_child_index) root_index = parent_list.index(-1) dfs(root_index) def find_left_child(parents, node_index): """辅助函数:寻找指定节点的第一个未被访问过的左孩子""" for i, p in enumerate(parents): if p == node_index and (parents.count(node_index) % 2 or parents[:i].count(node_index) % 2 == 0): return i return None def find_right_child(parents, node_index): """辅助函数:寻找指定节点的第一个未被访问过的右孩子""" count = 0 for i, p in enumerate(parents): if p == node_index: count += 1 if count > 1 or all(x != node_index for x in parents[:i]): return i return None # 测试用例 array_data = "ADCBKFGHETL" parent_indices = [-1, 0, 1, 1, 1, 2, 2, 2, 4, 8, 8] preorder_traversal(parent_indices, array_data) ``` 这段程定义了一个名为 `dfs()` 的内部函数作为深度优搜索的一部分,它接受一个参数——要探索的节点索引;接着实现了两个帮助者方法来找寻左右孩子的索引位置;最后调用了外部接口 `preorder_traversal()`, 它接收整个父级列表以及相应的数据列表,并启动DFS流程以生成最终所需的遍历结果。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值