C++实现二叉树的链式存储及非递归遍历

#include<iostream>
#include<queue>
#include<stack>
using namespace std;
#define MaxSize 100

typedef struct TreeNode
{
    
    
	int data;
	TreeNode* left;
	TreeNode* right;
	TreeNode()
	{
    
    
		data = 0;  //数据域 
		left = NULL;
		right = NULL;
	}
}TreeNode, *TN;

class BinTree
{
    
    
	public:
		BinTree();
		TN CreatTree(TN node); //创建二叉树 
		TN GetRoot();
		void PreOrderTraverse2(TN node); //线序非递归遍历 
		void InOrderTraverse2(TN node);  //中序非递归遍历 
		void PostOrderTraverse2(TN node); //后序非递归遍历
		void LevelOrder(TN node); //层次遍历
		void Visit(TN node); //输出指定结点的数据 
		int Depth(TN node);  //获得二叉树深度
		int NodeCount(TN node);   //获得结点个数
		int LeafCount(TN node);   //获得叶子结点个数 
		void Destroy(TN node);  //销毁二叉树 
	private:
		TreeNode* root;	
};

BinTree::BinTree()
{
    
    	
	root = new TreeNode(); 
	root->data = 0;
	root->left = NULL;
	root->right = NULL;
} 

TN BinTree::CreatTree(TN node)  //创建二叉树 
{
    
    
	cout << "请输入结点的数据域,当输入为 0 时代表当前位置无结点:";
	TN t = node;
	int data = 0;
	cin >> data;
	if(data>0)
	{
    
    
		if(t!=root)
		{
    
    
			t = new TreeNode();
		}
		t->data = data;
		t->left = CreatTree(t->left);
		t->right = CreatTree(t->right);
		return t;
	}
    else
	{
    
    
		return NULL;
	}
}

TN BinTree::GetRoot()
{
    
    
	return root;
} 


void BinTree::PreOrderTraverse2(TN node)  //先序非递归遍历 
{
    
    
	if(!node)
	{
    
    
		return;
	}
	else
	{
    
    
		stack<TN> s;
		TN p = node;
		s.push(p);
		while(!s.empty())
		{
    
    
			p = s.top();
			Visit(p);
			s.pop();
			if(p->right)
			{
    
    
				s.push(p->right);
			}
			if(p->left)
			{
    
    
				s.push(p->left);
			}
		}
	}
}

void BinTree::InOrderTraverse2(TN node)  //中序非递归遍历 
{
    
    
	if(!node)
	{
    
    
		return;
	}
	else
	{
    
    
		stack<TN> s;
		TN p = node;
		while(!s.empty()||p)
		{
    
    
			if(p)
			{
    
    
				s.push(p);
				p = p->left;
			}
			else
			{
    
    
				TN q = s.top();
				Visit(q);
				s.pop();
				p = q->right;
				p = p;
			}
		}
	}
	
}

void BinTree::PostOrderTraverse2(TN node) //后序非递归遍历 
{
    
    
	if(!node)
	{
    
    
		return;
	}
	else
	{
    
    
		stack<TN> s;
		TN p = node;  //记录当前要处理的结点 
		TN q = node;  //记录上一个被访问过的结点 
		s.push(p);
		while(!s.empty())
		{
    
    
			p = s.top();
			if(p->left&&p->left!=q&&p->right!=q)
			{
    
    
				s.push(p->left);
			}
			else
			{
    
    
				if(p->right&&p->right!=q)
				{
    
    
					s.push(p->right);
				}
				else
				{
    
    
					s.pop();
					Visit(p);
					q = p;
				}
			}
		}
	}
}

void BinTree::LevelOrder(TN node)  //层次遍历
{
    
    
	if(!node)
	{
    
    
		return;
	}
	else
	{
    
    
		TN p = node;
		queue<TN> q;
	    q.push(p);
		while(!q.empty())
		{
    
    
			p = q.front();
			if(p->left)
			{
    
    
				q.push(p->left);
			}
			if(p->right)
			{
    
    
				q.push(p->right);
			}
			Visit(q.front());
			q.pop();
		}
	}
}

void BinTree::Visit(TN node)  //输出指定结点的数据 
{
    
    
	if(node)
	{
    
    
		cout << node->data << " ";
	}
	else
	{
    
    
		cout << "结点为空";
	}
}

int BinTree::Depth(TN node)  //获得二叉树深度
{
    
    
	if(!node)
	{
    
    
		return 0;
	}
	else
	{
    
    
		int m = Depth(node->left);
		int n = Depth(node->right);
		return m>n?(m+1):(n+1);
	}
}

int BinTree::NodeCount(TN node)  //获得结点个数
{
    
    
	if(!node)
	{
    
    
		return 0;
	}
	else
	{
    
    
		return NodeCount(node->left) + NodeCount(node->right) + 1;
	}
}

int BinTree::LeafCount(TN node)  //获得叶子结点个数
{
    
    
	if(!node)
	{
    
    
		return 0;
	}
	else
	{
    
    
		if(node->left==NULL&&node->right==NULL)
		{
    
    
			return 1;
		}
		else
		{
    
    
			return LeafCount(node->left) + LeafCount(node->right);
		}
	}
}

void BinTree::Destroy(TN node)
{
    
    
	if(!node)
	{
    
    
		return;
	}
	else
	{
    
    
		Destroy(node->left);
		Destroy(node->right);
		delete node;
	}
}

int main()
{
    
    
	BinTree bt;
	bt.CreatTree(bt.GetRoot());
	cout << "先序非递归遍历:";bt.PreOrderTraverse2(bt.GetRoot());
	cout << endl;
	cout << "中序非递归遍历:";bt.InOrderTraverse2(bt.GetRoot());
	cout << endl;
	cout << "后序非递归遍历:";bt.PostOrderTraverse2(bt.GetRoot());
	cout << endl;
	cout << "层次遍历:";bt.LevelOrder(bt.GetRoot());
	cout << endl;
	cout << "深度:" << bt.Depth(bt.GetRoot());
	cout << endl;
	cout << "结点数:" << bt.NodeCount(bt.GetRoot());
	cout << endl;
	cout << "叶子结点数:" << bt.LeafCount(bt.GetRoot());
	cout << endl;
	bt.Destroy(bt.GetRoot());
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_46027243/article/details/113850850