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

📄 c++.cpp

📁 前序后序中序遍历
💻 CPP
字号:
#include<iostream>
#include<string>
using namespace std;
int n=0;
template <class T>
struct BiNode  {
  T data;       
  BiNode<T> *lchild, *rchild;
};
template <class T>
class BiTree
{
public:
    BiTree( ); 
    ~BiTree(void); 
	BiNode<T>* Getroot();  
    void PreOrder(BiNode<T> *root); 
    void InOrder(BiNode<T> *root); 
    void PostOrder(BiNode<T> *root); 
    void LeverOrder(BiNode<T> *root); 
	void PreOrdercount(BiNode<T>*root);
private:
    BiNode<T> *root; 
    BiNode<T> *Creat( ); 
    void Release(BiNode<T> *root); 
};
template<class T>
BiTree<T>::BiTree( )
{
	//int m;
	//cout<<"请输入结点的个数:"<<endl;
	//cin>>m;
	this->root = Creat( );
}
template<class T>
BiTree<T>::~BiTree(void)
{
	Release(root);
}
template<class T>
BiNode<T>* BiTree<T>::Getroot( )
{
	return root;
}
template<class T>
void BiTree<T>::PreOrder(BiNode<T> *root)
{
	if(root==NULL)  return;
	else{		
		cout<<root->data<<" ";
        PreOrder(root->lchild);
		PreOrder(root->rchild);
	}
}
template <class T>
void BiTree<T>::InOrder (BiNode<T> *root)
{
    if (root==NULL)  return; 
    else{	
        InOrder(root->lchild); 
        cout<<root->data<<" "; 
        InOrder(root->rchild); 
	}
}
template <class T>
void BiTree<T>::PostOrder(BiNode<T> *root)
{ 
    if (root==NULL)   return; 
    else{	
        PostOrder(root->lchild); 
        PostOrder(root->rchild);
        cout<<root->data<<" "; 
	}
}
template <class T>
void BiTree<T>::LeverOrder(BiNode<T> *root)
{
	const int MaxSize = 100;
	int front = 0;
	int rear = 0;  //采用顺序队列,并假定不会发生上溢
	BiNode<T>* Q[MaxSize];
    BiNode<T>* q;
	if (root==NULL) return;
	else{
		Q[rear++] = root;
		while (front != rear)
		{
			q = Q[front++];
     		cout<<q->data<<" "; 		
    		if (q->lchild != NULL)    Q[rear++] = q->lchild;		
			if (q->rchild != NULL)    Q[rear++] = q->rchild;
		}
	}
}
template <class T>
BiNode<T>* BiTree<T>::Creat( )
{
	BiNode<T>* root;
	T ch;
	cout<<"请输入创建一棵二叉树的结点数据"<<endl;
	cin>>ch;
    if (ch=="#") root = NULL;
    else{ 
	     root = new BiNode<T>;       // 生成一个结点
         root->data=ch;
         root->lchild = Creat( );    //递归建立左子树
         root->rchild = Creat( );    //递归建立右子树
    }
    return root;
}
template<class T>
void BiTree<T>::Release(BiNode<T>* root)
{
  if (root != NULL){                  
	  Release(root->lchild);   //释放左子树
      Release(root->rchild);   //释放右子树
      delete root;
  }  
}
template<class T>
void BiTree<T>::PreOrdercount(BiNode<T>*root)
{  if(root)
{ 
	if(!root->lchild&&root->rchild||root->lchild&&!root->rchild)  n++;
	PreOrdercount(root->lchild);
	PreOrdercount(root->rchild);
}
}
int main()
{	

	BiTree<string> bt; 
	char e;
	BiNode<string>* root = bt.Getroot( ); 
	while(true)
	{
		cout<<"*************************************"<<endl;
		cout<<"*\t\t实现二叉树的基本功能\t\t*"<<endl;
		cout<<"*\t\t 1.前序遍历\t\t\t*"<<endl;
		cout<<"*\t\t 2.中序遍历\t\t\t*"<<endl;
		cout<<"*\t\t 3.后序遍历\t\t\t*"<<endl;
		cout<<"*\t\t 4.层序遍历\t\t\t*"<<endl;
		cout<<"*\t\t 5.计算单枝节点数\t\t*"<<endl;
		cout<<"*\t\t 6.退出\t\t\t*"<<endl;
		cout<<"*************************************"<<endl;
		do{
		cout<<"请输入你的选择(1-6):";
		cin>>e;
		}while(e<'1'||e>'6');
		switch(e)
		{
		 case'1':
			cout<<"前序遍历:"<<endl;
			bt.PreOrder(root);
			cout<<endl;
			break;
		 case'2':
			cout<<"中序遍历:"<<endl;
			bt.InOrder(root);
			cout<<endl;
			break;
		case'3':
			cout<<"后序遍历:"<<endl;
			bt.PostOrder(root);
			cout<<endl;
			break;
		case'4':
			cout<<"层序遍历:"<<endl;
			bt.LeverOrder(root);
			cout<<endl;
			break;
		case'5':
			cout<<"计算单枝节点数:";
			bt.PreOrdercount(root);
			cout<<n<<endl;
			break;
		case'6':
			return 0;
		}
	}
}

⌨️ 快捷键说明

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