c++二叉树的非递归建立和遍历

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


template<typename T>
struct BiTreeNode
{
	T data;
	BiTreeNode *Lchild, *Rchild;
	BiTreeNode() :Lchild(NULL), Rchild(NULL){}
	BiTreeNode(const T& d, BiTreeNode *lchild = NULL, BiTreeNode *rchild = NULL) :data(d), Lchild(lchild), Rchild(rchild){}
};

template<typename T>
class BiTree
{
public:
	BiTree(){ root = NULL; }
	~BiTree(){ /*makeEmpty();*/ }
	//void makeEmpty(BiTreeNode<T>* &root);
	void createBiTree(BiTreeNode<T>* &root);
	void output(BiTreeNode<T> *root);
	void preOrder(BiTreeNode<T>* &root);
	void preOrder_2(BiTreeNode<T>* &root);
	void levelOrder(BiTreeNode<T>* &root);
	void inOrder(BiTreeNode<T>* &root);
	void postOrder(BiTreeNode<T>* &root);
	BiTreeNode<T> * root;

};


template<typename T>
void BiTree<T>::createBiTree(BiTreeNode<T>* &root)
{
	//按照广义表方式输入
	stack<BiTreeNode<T>* >s;
	root = NULL;
	BiTreeNode<T> *p, *t;
	int k;
	char ch;
	cin >> ch;
	while (ch != '#')
	{
		switch (ch)
		{
		case '(':
			s.push(p); k = 1; break;
		case ')':
			t = s.top(); s.pop(); break;
		case ',':
			k = 2; break;
		default:
			p = new BiTreeNode<T>(ch);
			if (root == NULL)
			{
				root = p;
			}
			else if (k==1)
			{
				t=s.top();
				t->Lchild = p;
			}
			else
			{
				t=s.top();
				t->Rchild = p;
			}
		}
		cin >> ch;
	}

	cout << endl;
	
}


template<typename T>
void BiTree<T>::output(BiTreeNode<T> *root)
{

	if (root)
	{
		cout << root->data << "  ";
		output(root->Lchild);
		output(root->Rchild);
	}
}

template<typename T>
void BiTree<T>::preOrder(BiTreeNode<T>* &root)
{
	//先存右结点,再存做结点,出栈真好相反:先出左结点,后出右结点
	stack<BiTreeNode<T>* > s;
	BiTreeNode<T> * current;
	s.push(root);
	while (!s.empty())
	{
		current = s.top();
		s.pop();
		cout << current->data << " ";
		if (current->Rchild)
		{
			s.push(current->Rchild);
		}
		if (current->Lchild)
		{
			s.push(current->Lchild);
		}
	}
	cout << endl;
}

template<typename T>
void BiTree<T>::preOrder_2(BiTreeNode<T>* &root)
{
	//只存右结点
	stack<BiTreeNode<T>* > s;
	BiTreeNode<T>* current = root;
	s.push(NULL);
	while (current)
	{
		cout << current->data << " ";
		if (current->Rchild)
		{
			s.push(current->Rchild);
		}
		if (current->Lchild)
		{
			current = current->Lchild;
		}
		else
		{
			current = s.top();
			s.pop();
		}
	}
	cout << endl;
}


template<typename T>
void BiTree<T>::levelOrder(BiTreeNode<T>* &root)
{

}

template<typename T>
void BiTree<T>::inOrder(BiTreeNode<T>* &root)
{
	stack<BiTreeNode<T>* > s;
	BiTreeNode<T>  *current = root;
	do 
	{
		while (current)
		{
			s.push(current);
			current = current->Lchild;
		}
		if (!s.empty())
		{
			current = s.top();
			cout << current->data << "  ";
			s.pop();
			current = current->Rchild;
		}

	} while (current||!s.empty());
	cout << endl;
}

/************************************************************************
//后序遍历(非递归形式)
//思想:从根节点开始,向左走到尽头,并入栈,同时设置标志位为1.
//出栈时如果这个节点有右子树,则判断是第几次访问,如果是第1次访问,
//则不出栈,将标志位改为2;如果是第二次访问,则出栈。

************************************************************************/
template<typename T>
void BiTree<T>::postOrder(BiTreeNode<T>* &root)
{
	stack<BiTreeNode<T>* > s;
	stack<int> flag_s;
	unsigned  flag;
	BiTreeNode<T>* current = root;
	s.push(current);
	flag_s.push(1);
	while (!s.empty())
	{
		while ( current && current->Lchild )
		{
			current = current->Lchild;
			s.push(current);
			flag_s.push(1);
		}
		if (!s.empty())
		{
			flag = flag_s.top();
			flag_s.pop();
			current = s.top();
			if (current->Rchild && flag==1)
			{
				flag_s.push(2);
				current = current->Rchild;
				s.push(current);
				flag_s.push(1);
			}
			else
			{
				s.pop();
				cout << current->data << " ";
				current = current->Rchild;
			}
		}
	}
	cout << endl;

}




void main()
{
	BiTree<char>* bt = new BiTree<char>();
	cout << "广义表的方式非递归建立二叉树:eg:A(B(D,E),C(,F)):" << endl;
	bt->createBiTree(bt->root);
	/*cout << endl << endl;
	bt->output(bt->root);*/
	cout << "非递归先序(方法1):" << endl;
	bt->preOrder(bt->root);
	cout << "非递归先序(方法2):" << endl;
	bt->preOrder_2(bt->root);
	cout << "非递归中序遍历:" << endl;
	bt->inOrder(bt->root);
	cout << "非递归后序遍历:" << endl;
	bt->postOrder(bt->root);
	while (1)
	{

	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值