zoukankan      html  css  js  c++  java
  • 数据结构与算法之各种方法遍历二叉树

    搬砖以前的题目

    需要复习栈与队列的小伙伴,可以到我的栈与队列文章里面搬砖

    #include<iostream>
    #include<stdio.h>
    #include<stdlib.h>
    #include<algorithm>
    #define MaxSize  100
    using namespace std;
    typedef struct node
    {
    	char data;
    	struct node * lchild;
    	struct node * rchild;
    }BTNode;
    
    
    typedef struct
    {
    	BTNode *data[MaxSize];
    	int front,rear;
    }SqQueue;
    
    
    typedef struct
    {
    	BTNode *data[MaxSize];
    	int top;
    }SqStack;
     
    void InitStack(SqStack *&s)
    {
    	s=(SqStack *)malloc(sizeof(SqStack));
    	s->top=-1;
    } 
    
    void InitQueue(SqQueue *&q)
    {	q=(SqQueue *)malloc (sizeof(SqQueue));
    	q->front=q->rear=0;
    }
    
    void DestroyStack(SqStack *&s)
    {
    	free(s);
    }
    bool StackEmpty(SqStack *s)
    {
    	return(s->top==-1);
    }
    bool Push(SqStack *&s,BTNode * e)
    {
    	if (s->top==MaxSize-1)    
    		return false;
    	s->top++;
    	s->data[s->top]=e;
    	return true;
    }
    
    bool QueueEmpty(SqQueue *q)
    {
    	return(q->front==q->rear);
    }
    
    bool enQueue(SqQueue *&q,BTNode * e)
    {	if ((q->rear+1)%MaxSize==q->front)	//队满上溢出
    		return false;
    	q->rear=(q->rear+1)%MaxSize;
    	q->data[q->rear]=e;
    	return true;
    }
    bool deQueue(SqQueue *&q,BTNode *&e)
    {	if (q->front==q->rear)		//队空下溢出
    		return false;
    	q->front=(q->front+1)%MaxSize;
    	e=q->data[q->front];
    	return true;
    }
    bool Pop(SqStack *&s,BTNode * &e)
    {
    	if (s->top==-1)		
    		return false;
    	e=s->data[s->top];
    	s->top--;
    	//printf("%c
    ",e);
    	return true;
    }  
    bool GetTop(SqStack *s,BTNode *&e)
    {
    	if (s->top==-1) 		
    		return false;
    	e=s->data[s->top];
    	return true;
    }
    void CreateBTree(BTNode *&b,char *str)// 创造二叉树 
    {
    	BTNode *St[MaxSize],*p;
    	int top=-1,k,j=0;
    	char ch;
    	b=NULL;
    	ch=str[j];
    	while(ch!='')
    	{
    		switch(ch)
    		{
    			case'(':
    					top++;
    					St[top]=p;
    					k=1;
    					break;
    			case')':
    					top--;
    					break;
    			case',':
    					k=2;
    					break;
    			default:
    					p=(BTNode *)malloc(sizeof(BTNode));
    					p->data = ch;
                		p->lchild = p->rchild = NULL;
    			if(b==NULL)
    			{
    				b=p;
    			}
    			else
    			{
    				switch(k)
    				{
    					case 1:St[top]->lchild=p;break;
    					case 2:St[top]->rchild=p;break;
    				}
    			}
    		}
    		j++;
    		ch=str[j];
    	}
    }
    void PreOrderFX(BTNode *b)    //先序遍历 非递归 
    {
    	BTNode *p;
    	SqStack *st;
    	InitStack(st);
    	if(b!=NULL)
    	{
    		Push(st,b);
    		while(!StackEmpty(st))
    		{
    			Pop(st,p);
    			printf("%c",p->data);
    			if(p->rchild !=NULL)
    			{
    				Push(st,p->rchild);
    			}
    			if(p->lchild !=NULL)
    			{
    				Push(st,p->lchild);
    			}
    		}
    		printf("
    ");
    	}
    	DestroyStack(st);
    }
    
    void InOrderFX(BTNode *b)    //中序遍历 非递归 
    {
    	BTNode *p;
    	SqStack *st;
    	InitStack(st);
    	p=b;
    	while(!StackEmpty(st) || p!=NULL)
    	{
    		while(p!=NULL)
    		{
    			Push(st,p);
    			p=p->lchild;
    		}
    		if(!StackEmpty(st))
    		{
    			Pop(st,p);
    			printf("%c",p->data);
    			p=p->rchild;
    		}
    	}
    	printf("
    ");
    	DestroyStack(st);
    }
    void PostOrderFX(BTNode *b)//后序遍历 非递归 
    {
    	BTNode *p,*r;
    	bool flag;
    	SqStack *st;
    	InitStack(st);
    	p=b;
    	do
    	{
    		while(p!=NULL)
    		{
    			Push(st,p);
    			p=p->lchild;
    		}
    		r=NULL;
    		flag=true;
    		while(!StackEmpty(st)&& flag)
    		{
    			GetTop(st,p);
    			if(p->rchild==r)
    			{
    				printf("%c",p->data);
    				Pop(st,p);
    				r=p;
    			}
    			else
    			{
    				p=p->rchild;
    				flag=false;
    			}
    		}
    	}
    	while(!StackEmpty(st));
    	printf("
    ");
    	DestroyStack(st);
    }
    void PreOrder(BTNode *b)//先序遍历 递归 
    {
    	if(b!=NULL)
    	{
    		printf("%c",b->data);
    		PreOrder(b->lchild);
    		PreOrder(b->rchild);
    	}
    }
    void InOrder(BTNode *b)//中序遍历 递归 
    {
    	if(b!=NULL)
    	{
    		InOrder(b->lchild);
    		printf("%c",b->data);
    		InOrder(b->rchild);
    	}
    }
    void PostOrder(BTNode *b)//后序遍历 递归 
    {
    	if(b!=NULL)
    	{
    		PostOrder(b->lchild);
    		PostOrder(b->rchild);
    		printf("%c",b->data);
    	}
    }
    
    void LevelOrder(BTNode *b)   //层次遍历 
    {
    	BTNode *p;
    	SqQueue *qu;
    	InitQueue (qu);
    	enQueue(qu,b);
    	while(!QueueEmpty(qu))
    	{
    		deQueue(qu,p);
    		printf("%c",p->data);
    		if(p->lchild!=NULL)
    		{
    			enQueue(qu,p->lchild);
    		}
    		if(p->rchild!=NULL)
    		{
    			enQueue(qu,p->rchild);
    		}
    	}
    }
    
    void DestroyBTree(BTNode *& b)//销毁二叉树 
    {
    	if(b!=NULL)
    	{
    		DestroyBTree(b->lchild);
    		DestroyBTree(b->rchild);
    		free(b);
    	}
     } 
    
    
    int main()
    {   BTNode *b,*p;
    	CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
    	printf("先序递归"); 
    	PreOrder(b);
    	printf("
    ");
    	printf("中序递归");  
    	InOrder(b);
    	printf("
    ");
    	printf("后序递归") ;
    	printf("
    ");
    	printf("先序非递归") ;
    	PreOrderFX(b);
    	printf("中序非递归");  
    	InOrderFX(b);
    	printf("后序非递归"); 
    	PostOrderFX(b);
        printf("先序非递归") ;
    	LevelOrder(b);  
        DestroyBTree(b);
        printf("
    ");
        printf("二叉树已经释放"); 
    }
    
  • 相关阅读:
    vim官方教程
    vim命令
    vim设置——/home/.vimrc设置
    Django中静态及媒体文件的引用设置
    博客园特效,拖动鼠标吸附线条
    机器学习工具Octave安装(Win10环境)
    【原】Coursera—Andrew Ng斯坦福机器学习(0)——课程地址和软件下载
    Anaconda 安装教程(Win10环境) Tensorflow安装
    博客园 设置各级标题样式
    Android WebView 捕捉点击的URL中的信息
  • 原文地址:https://www.cnblogs.com/AmosAlbert/p/12832287.html
Copyright © 2011-2022 走看看