⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bitrpreiterator.h

📁 包含各种测试,查找和算法等代码,如冒泡算法,树的遍历,链表,队列,堆栈等
💻 H
字号:
#include "LinStack.h"
#include "BiTreeIterator.h"

template <class T> class BiTrPreIterator: public BiTreeIterator<T>
{
	protected:
		LinStack<BiTreeNode<T> *> S;		//存放二叉树结点指针的堆栈
	public:
		BiTrPreIterator(BiTreeNode<T>* tree):
		  BiTreeIterator<T>(tree){}

		virtual void Next(void);
		virtual void Reset(void);
};

template <class T>
void BiTrPreIterator<T>::Reset(void)
{
	S.ClearStack();
	iteComplete = (root == NULL);
	if(root == NULL) return;
	current = root;							//根结点指针为当前结点指针
	if(root->Right() != NULL) S.Push(root->Right());	//右子树入栈
	if(root->Left() != NULL) S.Push(root->Left());		//左子树入栈
}

template <class T>
void BiTrPreIterator<T>::Next(void)
{
	if(iteComplete == 1)
	{
		cerr << "已到二叉树尾!" << endl;
		exit(1);
	}
	if(!S.StackEmpty())
	{
		current = S.Pop();							//退栈取得当前结点指针
		if(current->Right() != NULL) S.Push(current->Right());//右子树入栈
		if(current->Left() != NULL) S.Push(current->Left());//左子树入栈
	}
	else
		iteComplete = 1;						//堆栈空时置结束标记为1
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -