• 数据结构与算法李春葆系列之二叉树设计性实验


    #include<stdio.h>
    #include<stdlib.h>
    #include <string.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--;
    
    	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];
    	}
    }
    
    int InOrderFX(BTNode *b)  
    {
    	BTNode *p;
    	int count=0;
    	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);
    			count++; 
    			p=p->rchild;
    		}
    	}
    	DestroyStack(st);
    	return count;
    }
    int constcount1=0;
    int  DispLeaf(BTNode  *b)
    {  
    	if(b!=NULL)
    	{
    		if(b->lchild==NULL&&b->rchild==NULL)
    		{
    			constcount1++;
    		}
    		DispLeaf(b->lchild);
    		DispLeaf(b->rchild);
    	}
    }
    int L=1;
    void findlevel(BTNode *p,char x)
    {   
        if(p!=NULL)
        {
          if(p->data==x) printf("层号:%d
    ",L);
          ++L;
          findlevel(p->lchild,x);
          findlevel(p->rchild,x);
          --L;
        }
    }
    int deepth = 0; 
    int width[10] = {0};
    void TreeWidth(BTNode *t){
        if (t == NULL) {
            return;
        }
        if (deepth == 0) {
            width[0] = 1;
        }
        if (t->lchild != NULL) {
            width[deepth+1] += 1;
            deepth += 1;
            TreeWidth(t->lchild);
        }
        if (t->rchild != NULL) {
            width[deepth+1] += 1;
            deepth+=1;
            TreeWidth(t->rchild);
        }
        deepth-=1;
    }
    
    void DestroyBTree(BTNode *& b)
    {
    	if(b!=NULL)
    	{
    		DestroyBTree(b->lchild);
    		DestroyBTree(b->rchild);
    		free(b);
    	}
     } 
    
    int main()
    {   
        BTNode *b;
        char x;
        int h;
    	CreateBTree(b,"A(B(D,E(H(J,K(L,M(,N))))),C(F,G(,I)))");
    	printf("结点个数为%d
    ",InOrderFX(b));
    	DispLeaf(b);
    	scanf("%c",&x);
    	findlevel(b,x);
    	printf("叶子结点个数是%d
    ",constcount1);
    	TreeWidth(b);
    	int max; 
    	for(int i=0;i<9;i++)
    	{
    		if(width[i]>max)
    		{
    			max=width[i];
    		}
    	}
    	printf("最大宽度是%d
    ",max); 
    	DestroyBTree(b);
    	return 0;
    	
    }
    
  • 相关阅读:
    20200721训练记录
    20200717训练记录
    打家劫舍III(力扣第337题)
    HBase API的删除数据操作的分析
    相交链表(第160题)
    删除排序链表中的重复元素(第83题)
    合并两个有序链表(力扣第21题)
    删除链表的倒数第N个节点(第19题)
    HBase的架构原理
    回文链表、链表求和(234、445)
  • 原文地址:https://www.cnblogs.com/AmosAlbert/p/12832254.html
Copyright © 2020-2023  润新知