一图看懂二叉树的前序、中序、后序遍历(附代码)(●‘◡‘●)

图片如下:

图中列举的双亲表示法以及孩子表示法是在前序遍历的基础上的

前序遍历:将双亲节点放在前面开始数,即双亲、左、右的顺序,如A)(BD)(CEF)

//前序
void FirstRoot(BinaryNode *root)
{
	if (root == nullptr)
	{
		return;
	}
	VisttorNode(root);//注意
	FirstRoot(root->GetLeftChild());
	FirstRoot(root->GetRightChild());
}

中序遍历:将双亲节点放在中间开始数,即左、双亲、右的顺序,如(DB)(A)(ECF)

//中序
void MidRoot(BinaryNode* root)
{
	if (root == nullptr)
	{
		return;
	}
	MidRoot(root->GetLeftChild());
	VisttorNode(root);//注意
	MidRoot(root->GetRightChild());
}

后序遍历:将双亲节点放在后面开始数,即左、右、双亲的顺序,如(DB)(EFC)(A)

//后序
void LastRoot(BinaryNode* root)
{
	if (root == nullptr)
	{
		return;
	}
	LastRoot(root->GetLeftChild());
	LastRoot(root->GetRightChild());
	VisttorNode(root);//注意
}

ps.这三种遍历在代码上实现是非常简单的,就是递归调用,不同之处在于开始的顺序不同。

双亲表示法以及孩子表示法

//双亲表示法
//这里暂时使用中序的遍历方法,放入到list中
void FirstRootList(BinaryNode* root,vector<BinaryNode*>&list)
{
	if (root == nullptr)
	{
		return;
	}
	list.push_back(root);
	FirstRootList(root->GetLeftChild(), list);
	FirstRootList(root->GetRightChild(), list);
}

//这里使用中序遍历试试
void MidRootList(BinaryNode* root, vector<BinaryNode*>& list)
{
	if (root == nullptr)
	{
		return;
	}
	MidRootList(root->GetLeftChild(), list);
	list.push_back(root);
	MidRootList(root->GetRightChild(), list);
}
//找到双亲表示法需要的双亲的下标,这个list目前是按照前序遍历放置的
int FindIndex(vector<BinaryNode*>list, BinaryNode* node)
{
	for (int i=0;i<list.size();++i)
	{
		if (list[i] == node)
		{
			return i;
		}
	}
	return -1;
}
void PrintParentPresent(BinaryNode* root)
{
	vector<BinaryNode*>list;

	//用前序排列
	//FirstRootList(root, list);
	
	//用中序遍历试试
	MidRootList(root, list);
	for (int i = 0; i < list.size(); ++i)
	{
		BinaryNode* node = list[i];
		int parentIndex = -1;
		//找到它的双亲节点
		BinaryNode* parent = node->GetParent();
		if (parent != nullptr)
		{
			//找到双亲节点的下标,返回
			parentIndex = FindIndex(list, parent);
		}
		//printf("%s : %d \r\n", node->GetObject().val, parentIndex);
		cout << node->GetObject().val << ":" << parentIndex << endl;
	}
}

//孩子表示法
void PrintChildPresent(BinaryNode* root)
{
	vector<BinaryNode*>list;
	//用前序排列
	//FirstRootList(root, list);

	//用中序遍历试试
	MidRootList(root, list);
	for (int i = 0; i < list.size(); ++i)
	{
		BinaryNode* node = list[i];
		int leftChildIndex = -1;
		int rightChildIndex = -1;
		BinaryNode* leftChild = node->GetLeftChild();
		BinaryNode* rightChild = node->GetRightChild();
		if (leftChild != nullptr)
		{
			leftChildIndex = FindIndex(list, leftChild);
		}
		if (rightChild != nullptr)
		{
			rightChildIndex = FindIndex(list, rightChild);
		}
		//打印这个节点的值,以及它的孩子的下标
		//printf("%s : %d , %d \r\n", node->GetObject().val, leftChildIndex, rightChildIndex);
		cout << node->GetObject().val << " : " << leftChildIndex << "," << rightChildIndex << endl;
	}
}

全部代码如下:

//二叉树
#include <string>
#include <iostream>
#include <vector>
/*
用printf("%s")输出字符串是乱码的,得用cout...无语了
*/
using namespace std;
//内容
class Object
{
public:
	string val;
};

//节点
class BinaryNode
{
	//双亲
	BinaryNode* Parent;
	//内容
	Object obj;
	//左边,右边
	BinaryNode* LeftChild;
	BinaryNode* RightChild;

public:
	//构造函数,初始化
	BinaryNode()
	{
		Parent = nullptr;
		LeftChild = nullptr;
		RightChild = nullptr;
		obj.val = {0};
	}
	//创造节点
	static BinaryNode* CreatNode(Object o)
	{
		BinaryNode* newNode = new BinaryNode();
		newNode->SetObject(o);//输入内容
		return newNode;
	}
	//得到节点内容
	Object GetObject()
	{
		return obj;
	}
	//输入内容·
	void SetObject(Object o)
	{
		obj = o;
	}
	BinaryNode* GetParent()
	{
		return Parent;
	}
	BinaryNode* GetLeftChild()
	{
		return LeftChild;
	}
	BinaryNode* GetRightChild()
	{
		return RightChild;
	}
	void SetLeftChild(BinaryNode* child)
	{
		LeftChild = child;
		child->Parent = this;
	}
	void SetRightChild(BinaryNode* child)
	{
		RightChild = child;
		child->Parent = this;
	}
};

//访问节点,输出节点内容
void VisttorNode(BinaryNode* node)
{
	if (node != nullptr)
	{
		/*printf("%s ", node->GetObject().val);*/
		cout << node->GetObject().val << " ";
	}
}
//前序
void FirstRoot(BinaryNode *root)
{
	if (root == nullptr)
	{
		return;
	}
	VisttorNode(root);
	FirstRoot(root->GetLeftChild());
	FirstRoot(root->GetRightChild());
}
//中序
void MidRoot(BinaryNode* root)
{
	if (root == nullptr)
	{
		return;
	}
	MidRoot(root->GetLeftChild());
	VisttorNode(root);
	MidRoot(root->GetRightChild());
}
//后序
void LastRoot(BinaryNode* root)
{
	if (root == nullptr)
	{
		return;
	}
	LastRoot(root->GetLeftChild());
	LastRoot(root->GetRightChild());
	VisttorNode(root);
}

//双亲表示法
//这里暂时使用中序的遍历方法,放入到list中
void FirstRootList(BinaryNode* root,vector<BinaryNode*>&list)
{
	if (root == nullptr)
	{
		return;
	}
	list.push_back(root);
	FirstRootList(root->GetLeftChild(), list);
	FirstRootList(root->GetRightChild(), list);
}

//这里使用中序遍历试试
void MidRootList(BinaryNode* root, vector<BinaryNode*>& list)
{
	if (root == nullptr)
	{
		return;
	}
	MidRootList(root->GetLeftChild(), list);
	list.push_back(root);
	MidRootList(root->GetRightChild(), list);
}
//找到双亲表示法需要的双亲的下标,这个list目前是按照前序遍历放置的
int FindIndex(vector<BinaryNode*>list, BinaryNode* node)
{
	for (int i=0;i<list.size();++i)
	{
		if (list[i] == node)
		{
			return i;
		}
	}
	return -1;
}
void PrintParentPresent(BinaryNode* root)
{
	vector<BinaryNode*>list;

	//用前序排列
	//FirstRootList(root, list);
	
	//用中序遍历试试
	MidRootList(root, list);
	for (int i = 0; i < list.size(); ++i)
	{
		BinaryNode* node = list[i];
		int parentIndex = -1;
		//找到它的双亲节点
		BinaryNode* parent = node->GetParent();
		if (parent != nullptr)
		{
			//找到双亲节点的下标,返回
			parentIndex = FindIndex(list, parent);
		}
		//printf("%s : %d \r\n", node->GetObject().val, parentIndex);
		cout << node->GetObject().val << ":" << parentIndex << endl;
	}
}

//孩子表示法
void PrintChildPresent(BinaryNode* root)
{
	vector<BinaryNode*>list;
	//用前序排列
	//FirstRootList(root, list);

	//用中序遍历试试
	MidRootList(root, list);
	for (int i = 0; i < list.size(); ++i)
	{
		BinaryNode* node = list[i];
		int leftChildIndex = -1;
		int rightChildIndex = -1;
		BinaryNode* leftChild = node->GetLeftChild();
		BinaryNode* rightChild = node->GetRightChild();
		if (leftChild != nullptr)
		{
			leftChildIndex = FindIndex(list, leftChild);
		}
		if (rightChild != nullptr)
		{
			rightChildIndex = FindIndex(list, rightChild);
		}
		//打印这个节点的值,以及它的孩子的下标
		//printf("%s : %d , %d \r\n", node->GetObject().val, leftChildIndex, rightChildIndex);
		cout << node->GetObject().val << " : " << leftChildIndex << "," << rightChildIndex << endl;
	}
}
int main()
{
	Object objA;
	objA.val = "A";
	BinaryNode* nodeA = BinaryNode::CreatNode(objA);

	Object objB;
	objB.val = "B";
	BinaryNode* nodeB = BinaryNode::CreatNode(objB);

	Object objC;
	objC.val = "C";
	BinaryNode* nodeC = BinaryNode::CreatNode(objC);

	Object objD;
	objD.val = "D";
	BinaryNode* nodeD = BinaryNode::CreatNode(objD);

	Object objE;
	objE.val = "E";
	BinaryNode* nodeE = BinaryNode::CreatNode(objE);

	Object objF;
	objF.val = "F";
	BinaryNode* nodeF = BinaryNode::CreatNode(objF);

	//连接
	nodeA->SetLeftChild(nodeB);
	nodeA->SetRightChild(nodeC);

	nodeB->SetLeftChild(nodeD);


	nodeC->SetLeftChild(nodeE);
	nodeC->SetRightChild(nodeF);

	BinaryNode* root = nodeA;

	cout << "前序:";
	FirstRoot(root);
	cout << endl;

	cout << "中序:";
	MidRoot(root);
	cout << endl;

	cout << "后序:";
	LastRoot(root);
	cout << endl;
	cout << endl;

	cout << "双亲:" << endl;
	PrintParentPresent(root);
	cout << endl;
	cout << "孩子:" << endl;
	PrintChildPresent(root);
	return 0;
}

必看:(✿◡‿◡)

点赞,收藏,分享,加关注ψ(`∇´)ψ

  • 10
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值