二叉树(二叉链表)的非递归遍历

#include <stdio.h>
#include <stack>
#include <queue>
using namespace std;

class TreeNode
{
public:
	TreeNode(char ch)
	{
		data = ch;
		parent = lchild = rchild = nullptr;
	}
	TreeNode* addLeftChild(char ch)
	{
		TreeNode* node = new TreeNode(ch);
		lchild = node;
		node->parent = this;
		return node;
	}
	TreeNode* addRightChild(char ch)
	{
		TreeNode* node = new TreeNode(ch);
		rchild = node;
		node->parent = this;
		return node;
	}
public:
	char data;
	TreeNode* parent;
	TreeNode* lchild;
	TreeNode* rchild;
};

//二叉树(二叉链表)前序遍历的非递归实现
void PreOrder(TreeNode* root)
{
	stack<TreeNode*> s;
	while (root != nullptr || !s.empty())
	{
		while (root != nullptr)
		{
			s.push(root);
			root = s.top()->lchild;
			printf("%c ", s.top()->data);//访问结点数据
		}
		if (!s.empty())
		{
			root = s.top()->rchild;
			s.pop();
		}
	}
}

//二叉树(二叉链表)中序遍历的非递归实现
void InOrder(TreeNode* root)
{
	stack<TreeNode*> s;
	while (root != nullptr || !s.empty())
	{
		while (root != nullptr)
		{
			s.push(root);
			root = root->lchild;
		}
		if (!s.empty())
		{
			printf("%c ", s.top()->data);//访问结点数据
			root = s.top()->rchild;
			s.pop();
		}
	}
}

//二叉树(二叉链表)后序遍历的非递归实现
class StackElementForPostOrder
{
public:
	StackElementForPostOrder(TreeNode* ptr,int flag)
	{
		this->ptr = ptr;
		this->flag = flag;
	}
public:
	TreeNode* ptr;
	int flag;
};
void PostOrder(TreeNode* root)
{
	stack<StackElementForPostOrder> s;
	while (root != nullptr || !s.empty())
	{
		while (root != nullptr)
		{
			s.push(StackElementForPostOrder(root, 1));
			root = root->lchild;
		}
		while (!s.empty() && s.top().flag == 2)
		{
			printf("%c ", s.top().ptr->data);//访问结点数据
			s.pop();
		}
		if (!s.empty())
		{
			s.top().flag = 2;
			root = s.top().ptr->rchild;
		}
	}
}

//二叉树(二叉链表)层序遍历
void LeverOrder(TreeNode* root)
{
	if (root == nullptr)
		return;
	queue<TreeNode*> q;
	q.push(root);
	while (!q.empty())
	{
		TreeNode* node = q.front();
		printf("%c ", node->data);//访问结点数据
		q.pop();
		if (node->lchild != nullptr)q.push(node->lchild);
		if (node->rchild != nullptr)q.push(node->rchild);
	}
}

//二叉树(二叉链表)获取树的最大深度
int BTreeDepth(TreeNode* root) 
{
	if (root == nullptr)
		return 0;
	int res = 0;
	queue<TreeNode*> q;
	q.push(root);
	while (!q.empty())
	{
		int size = q.size();
		while (size--)
		{
			TreeNode* cur = q.front();
			q.pop();
			if (cur->lchild) q.push(cur->lchild);
			if (cur->rchild) q.push(cur->rchild);
		}
		res++;
	}
	return res;
}

//二叉树(二叉链表)获取指定结点所在的层号
int GetTreeNodeLayer(TreeNode* node)
{
	int lay = 0;
	TreeNode* p;
	for (p=node;p!=nullptr;p=p->parent)
		lay++;
	return lay;
}

//二叉树(二叉链表)获取两个结点的最近公共祖先
TreeNode* LowestCommonAncestor(TreeNode* node1, TreeNode* node2)
{
	for (TreeNode* p = node1; p != nullptr; p = p->parent)
	{
		for (TreeNode* q = node2; q != nullptr; q = q->parent)
		{
			if (p == q)
				return p;
		}
	}
	return nullptr;
}

void main()
{
	TreeNode root('A');

	TreeNode* A = &root;

	TreeNode* B = A->addLeftChild('B');
	TreeNode* D = B->addLeftChild('D');
	TreeNode* E = B->addRightChild('E');

	TreeNode* C = A->addRightChild('C');
	TreeNode* F = C->addLeftChild('F');
	TreeNode* G = C->addRightChild('G');
	
	TreeNode* H = F->addLeftChild('H');
	TreeNode* I = F->addRightChild('I');


	LeverOrder(A);
	printf("\n完成\n");

	TreeNode* lca = LowestCommonAncestor(A, D);
	printf("最近公共祖先:%c\n", lca->data);

	getchar();
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值