二叉树

#include<iostream.h>
typedef struct node
{
	node *leftchild,*rightchild,*next;
	char ch;
}*ptrn;
class bintree
{
private:
	int count,i;
	int leafnodenumber;
	int leftdep,rightdep;
	ptrn newnode;
	ptrn leftchildnode;
	ptrn rightchildnode;
	ptrn *stack;ptrn popnode;
	int maxsize,nodenumber,treeheight;
	char *array;
	ptrn *widthtree;
	ptrn *b;
public:
	ptrn root; //根结点                 
	bintree(int size)//析构函数
	{
		count=0;//创建树是所用的栈的计数
		i=0;//以树的高度为容量创建数组
		leftdep=0;
		rightdep=0;
		leafnodenumber=0;//叶子结点数
		nodenumber=0;//结点数
		maxsize=size;//栈的最大容量
		stack=new ptrn[maxsize];//为栈分配空间
		root=NULL;//初始根为空
		array="a(b(d,e(h(j,k(l,m(,n))))),c(f,g(,i)))";//树的表示
	}
	bool empty()//计算栈是否为空
	{
		return count==0;
	}
	void push(ptrn &e)//入栈
	{
		if(count!=maxsize)
		{	stack[count]=e;count++;}
	}
	void pop()//出栈
	{
		if(!empty())
		{count--;}
	}
	int length()//求栈的大小
	{
		return count;
	}
	void creattree()//创建树
	{
		for(int i=0;i<maxsize;i++)
		{
			switch(array[i])
			{
			case '(':
				push(newnode);break;
			case ')':
				pop();newnode=stack[count];break;
			case ',':
				   if(array[i-1]==')')
				   {
					   pop();newnode=stack[count];
                       rightchildnode=new node;
					   rightchildnode->leftchild=NULL;
					   rightchildnode->rightchild=NULL;
				      rightchildnode->ch=array[i+1];i++;
				      newnode->rightchild=rightchildnode;
				     if(array[i+1]=='(')
					 {newnode=newnode->rightchild;}break;
				   }
				   else
				   {
                      rightchildnode=new node;
                       rightchildnode->leftchild=NULL;
					   rightchildnode->rightchild=NULL;
				      rightchildnode->ch=array[i+1];i++;
				      newnode->rightchild=rightchildnode;
				     if(array[i+1]=='(')
					 {newnode=newnode->rightchild;}break;
				   }
			default:
				if(root==NULL)
				{
					root=new node;root->ch=array[i];
				    newnode=root;
					root->leftchild=NULL;
					root->rightchild=NULL;
				}
				else
				{
					leftchildnode=new node;
                    leftchildnode->leftchild=NULL;
					leftchildnode->rightchild=NULL;
					leftchildnode->ch=array[i];
					newnode->leftchild=leftchildnode;
					if(array[i+1]=='(')
						newnode=newnode->leftchild;
				}
			}
		}
	}
	void widthtreesize()//为数组widthtree分配空间
	{
		widthtree=new ptrn[height(root)];
		b=new ptrn[height(root)];
		widthtree[-1]=NULL;
		for(int i=0;i<height(root);i++)
		{
			widthtree[i]=NULL;
		}
	}
	int height(ptrn &e)//求树的高度
	{
		if(e==NULL)
		{
			return 0;
		}
		else
		{
			int leftheight,rightheight;
			leftheight=height(e->leftchild);
			rightheight=height(e->rightchild);
			return (leftheight>rightheight?leftheight:rightheight)+1;
		}
	}
    void width(ptrn &e)//树的每一层为一个链,为每一个链赋值
	{
		if(widthtree[i-1]==NULL)
		{
			widthtree[i]=e;
			b[i]=e;
			b[i]->next=NULL;                                     
			i++;width(widthtree[i-1]);                         
		}
		else
		{
			if(e->leftchild!=NULL)
			{                                                 
				if(widthtree[i]==NULL)
				{
					widthtree[i]=e->leftchild;
			                                            
					b[i]=e->leftchild;
				}
				else
				{   
					b[i]->next=e->leftchild;                                         
					b[i]=e->leftchild;
				}
				if(e->rightchild!=NULL)
				{
					b[i]->next=e->rightchild;                     
					b[i]=e->rightchild;
				}
				else{}
				if(e->next==NULL)
				{                                                
					b[i]->next=NULL;                             
					i++;                                                                              
					if(i<height(root))
					{
						e=widthtree[i-1];                      
						width(e);
					}
				}
				else
				{
					e=e->next;
					width(e);
				}
			}
			else
			{
				if(e->rightchild!=NULL)
				{
					if(widthtree[i]==NULL)
					{
						
						widthtree[i]=e->rightchild;
						b[i]=e->rightchild;
					}
					else
					{
						b[i]->next=e->rightchild;
						b[i]=e->rightchild;
					}
					if(e->next==NULL)
					{
						b[i]->next=NULL;
						i++;                                  
						if(i<height(root))
						{
							e=widthtree[i-1];
							width(e);
						}
					}
					else
					{
						e=e->next;
						width(e);
					}
				}
				else
				{
				    	e=e->next;
                        if(e!=NULL)
						{
							width(e);
						}
						else
						{
							b[i]->next=NULL;
							i++;                                 
							if(i<height(root))
							{
								e=widthtree[i-1];
								width(e);
							}
						}
				}
			}
		}
	}
	int leafnodecount(ptrn &e)//求叶子结点个数
	{
		if(e!=NULL&&e->leftchild==NULL&&e->rightchild==NULL)
		{
			leafnodenumber++;return leafnodenumber;
			
		}
		else if(e!=NULL)
		{
            leafnodecount(e->leftchild);
			leafnodecount(e->rightchild);
		}
    	
	}
	void findnode(ptrn &e,char &ch1)//查找结点内容为ch1的子节点
	{
		if(e!=NULL&&e->ch==ch1)
		{
		    if(e->leftchild!=NULL&&e->rightchild!=NULL)
			{
				cout<<"随求结点的左孩子为"<<e->leftchild->ch<<"右孩子为"<<e->rightchild->ch<<endl;
			}
			else if(e->leftchild!=NULL&&e->rightchild==NULL)
			{
				cout<<"随求结点只有左孩子,内容为"<<e->leftchild->ch<<endl;
			}
			else if(e->leftchild==NULL&&e->rightchild!=NULL)
			{
                cout<<"随求结点只有右孩子,内容为"<<e->rightchild->ch<<endl;
			}
			else
			{
				cout<<"所求结点无孩子"<<endl;
			}	
		}
		else if(e!=NULL&&e->ch!=ch1)
		{
            findnode(e->leftchild,ch1);
		   findnode(e->rightchild,ch1);
		}
	}
	int linklength(ptrn &e)//求上述链以e为头结点的长度
	{
		ptrn temp;
		temp=e;
		int linknodecount=0;
		while(temp!=NULL)
		{
			linknodecount++;
				temp=temp->next;
		}
		return linknodecount;
	}
	void outputwidth()//输出树宽
	{
		int n=0;
		int j=0;
		while(j<height(root))
		{
			n=n>linklength(widthtree[j])?n:linklength(widthtree[j]);j++;
		}
	    cout<<"树的宽度为"<<n<<endl;
	}
	int nodecount()//求结点个数
	{
       return nodenumber;
	}
	void trailnode(ptrn &e)//以先序序列遍历树
	{
       if(e!=NULL)
	   {
		   nodenumber++;
		   cout<<e->ch<<endl;
		   trailnode(e->leftchild);
		   trailnode(e->rightchild);
	   }
	}
};
void main()
{
	char ch2='h';
	bintree bt(36);
	bt.creattree();
	bt.widthtreesize();
	bt.width(bt.root);
	bt.trailnode(bt.root);
	bt.findnode(bt.root,ch2);
    bt.outputwidth();
	cout<<"树高为"<<bt.height(bt.root)<<endl;
	cout<<"叶子数为"<<bt.leafnodecount(bt.root)<<endl;
	cout<<"结点总数为"<<bt.nodecount()<<endl;
}

原文地址:https://www.cnblogs.com/zztong/p/6695308.html