非递归方法实现遍历树(C++)

代码还是比较简洁好懂的~吧

不过因为不是一次性打完的所以里面的代码命名风格会突然有变化,所以说还是太浪了。。。

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

///树节点
struct Node
{
	char   data;
	Node* right;
	Node* left;
	Node(char data)
	{
		this->data = data;
		right = left = NULL;
	}
};


class Tree
{
private:
	Node* Root;
public:
	///构造函数 构造的时候调用初始化函数
	Tree()
	{
		Root = NULL;
		Init();
	}

	///析构函数
	~Tree()
	{
		deque<Node*> d;
		stack<Node*> s;
		if (Root == NULL) return;
		d.push_front(Root);
		while (!d.empty())
		{
			Node* now = d.front();
			s.push(now);
			d.pop_front();
			if (now->right != NULL) d.push_front(now->right);
			if (now->left != NULL) d.push_front(now->left);
		}
		while(!s.empty())
		{
			Node* now = s.top();
			s.pop();
			delete now;
		}
	}

	///初始化函数
	void Init()
	{
		char tmp;
		cout << "Input Root char:";
		cin >> tmp;
		if (tmp == '#') return;
		Root = new Node(tmp);
		queue<Node*> q;
		q.push(Root);
		while(!q.empty())
		{
			Node* tmpNode=NULL,*now=q.front();
			q.pop();
			cout << "Input "<<now->data<<"'s Left char:";
			cin >> tmp;
			if (tmp != '#') {
				tmpNode = new Node(tmp);
				q.push(tmpNode);
				now->left = tmpNode;
			}
			else now->left = NULL;
			cout << "Input " << now->data << "'s Right char:";
			cin >> tmp;
			if (tmp != '#') {
				tmpNode = new Node(tmp);
				q.push(tmpNode);
				now->right = tmpNode;
			}
			else now->right = NULL;
		}
	}

	///前序遍历函数
	void Pre_Order()
	{
		deque<Node*> d;
		if (Root == NULL) return;
		d.push_front(Root);
		while(!d.empty())
		{
			Node* now = d.front();
			d.pop_front();
			if (now->right != NULL) d.push_front(now->right);
			if (now->left != NULL) d.push_front(now->left);
			cout << now->data << ' ';
		}
		cout << endl;
	}

	///中序遍历函数
	void In_Order()
	{
		stack<Node*> s;
		Node* p=Root;
		while(!s.empty()||p!=NULL)
		{
			while(p!=NULL)
			{
				s.push(p);
				p = p->left;
			}
			if(!s.empty())
			{
				p = s.top();
				s.pop();
				cout << p->data << ' ';
				p = p->right;
			}
		}
		cout << endl;
	}

	///后序遍历函数
	void Post_Order()
	{
		stack<char>  MyStack;
		deque<Node*> MyDeque;
		MyDeque.push_front(Root);
		while(!MyDeque.empty())
		{
			Node* now = MyDeque.front();
			MyDeque.pop_front();
			MyStack.push(now->data);
			if (now->left != NULL) MyDeque.push_front(now->left);
			if (now->right != NULL) MyDeque.push_front(now->right);
		}
		while(!MyStack.empty())
		{
			cout << MyStack.top() << ' ';
			MyStack.pop();
		}
		cout << endl;
	}
	
};
int main()
{
	Tree* tree = new Tree();
	tree->Pre_Order();
	tree->In_Order();
	tree->Post_Order();
	return 0;
}

            
阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页