二叉树的创建及其基本操作

一、给出按照 先序遍历序列 创建二叉树的例子 且 使用顺序栈和循环队列

1.代码如下©:

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 100005 //注意栈不能太小,假如有树有n个结点(包括非空) 则需要2^n 的长度 

typedef char DataType; 

// 定义二叉链表结点结构
typedef struct Node {
    
    
    DataType data; // 树中结点数据 
    struct Node * LChild;//左孩子 
    struct Node * RChild;//右孩子 
}BiTNode,*BiTree;

// 定义栈结构
typedef struct stackElem {
    
    
    BiTNode node; // 栈中元素包含二叉结点类型 
    int flag;//标记 进左子树为0 右子树为1 
}ElemType;
typedef struct stack{
    
    
    ElemType elem[MAXSIZE];//顺序栈数组 
    int top;//栈顶指针 
}SeqStack;


//typedef struct queueElem{
    
    
//	BiTNode node; //队列中结点数据 
//}QElemType; 

// 定义队列结构
typedef BiTree QElemType; 
typedef struct queue{
    
    
	QElemType elem[MAXSIZE]; // 顺序队列数组 
	int front; //头指针 
	int rear; //尾指针 
}SeqQueue;

BiTNode* CreateBiTree(BiTNode* root); //创建二叉树
void PreOrder(BiTree root); //递归先序遍历
void InOrder(BiTree root); // 递归中序遍历
void PostOrder(BiTree root); // 递归后序遍历 
void PostOrder1(BiTree root); // 非递归后序遍历
void PreOrder1(BiTree root); // 非递归先序遍历
void InOrder1(BiTree root); // 非递归中序遍历 
void LevelOrder(BiTree root); // 层次遍历 
int NonLeafNode(BiTree root);//叶子结点个数
int BiTreeDeepth(BiTree root);//树的高度

//栈相关操作 
SeqStack* InitStack(void);//初始化栈
int Empty(SeqStack* s);//判断栈是否为空
int Push(SeqStack* s,ElemType x);//入栈
int Pop(SeqStack *s);//出栈
ElemType* GetTop(SeqStack* s);//取栈顶元素

//队列相关操作 
SeqQueue* InitQueue(void); //初始化队列 
int IsQueueEmpty(SeqQueue* q);//判队空 
int IsQueueFull(SeqQueue* q); //判队满
int EnterQueue(SeqQueue* q,QElemType x);//入队
int DelQueue(SeqQueue* q,QElemType* x);//出队
 

int main()
{
    
    
    BiTNode * root = CreateBiTree(root);
    
    printf("递归遍历:\n");
    PreOrder(root);
    printf("\n");
    InOrder(root);
    printf("\n");
    PostOrder(root);
    printf("\n");
    
    printf("非递归遍历:\n");
    PreOrder1(root);
    printf("\n");
    InOrder1(root);
    printf("\n");
    PostOrder1(root);
    printf("\n");
    
    printf("%d\n",NonLeafNode(root));
    printf("%d\n",BiTreeDeepth(root));
	printf("层次遍历:\n");
	LevelOrder(root);
	printf("\n");
	
    return 0;
}
BiTNode* CreateBiTree(BiTNode* root){
    
    
	//	根据先序序列创建二叉树 
    char ch;
    ch = getchar();
    if(ch == '#'){
    
    
        return NULL;
    }else{
    
    
        root = (BiTNode*)malloc(sizeof(BiTNode));
        root->data = ch;
        root->LChild = CreateBiTree(root->LChild);
        root->RChild = CreateBiTree(root->RChild);
        
    }
    return root;
}


SeqStack* InitStack(void){
    
    
    SeqStack* s;
    s= (SeqStack*)malloc(sizeof(SeqStack));
    s->top = -1;
    return s;
}
int Empty(SeqStack* s){
    
    
    if(s->top == -1){
    
    
        return 1;
    }else{
    
    
        return 0;
    }
}
int Push(SeqStack* s,ElemType x){
    
    
    if(s->top == MAXSIZE - 1){
    
    
        return 0;
    }else{
    
    
        s->top++;
        s->elem[s->top] = x;
        return 1;
    }
}
int Pop(SeqStack* s){
    
    
    if(Empty(s)){
    
    
        return 0;
    }else{
    
    
        s->top--;
        return 1;
    }
}
ElemType* GetTop(SeqStack* s){
    
    
    if(Empty(s)){
    
    
        return NULL;
    }else{
    
    
        return &(s->elem[s->top]);
    }
}

void PreOrder(BiTree root){
    
    
    if(root){
    
    
        printf("%c",root->data);
        PreOrder(root->LChild);
        PreOrder(root->RChild);
    }
    
}
void InOrder(BiTree root){
    
    
    if(root){
    
    
        InOrder(root->LChild);
        printf("%c",root->data);
        InOrder(root->RChild);
    }
}
void PostOrder(BiTree root){
    
    
	if(root){
    
    
		PostOrder(root->LChild);
		PostOrder(root->RChild);
		printf("%c",root->data);
	}
}

void PreOrder1(BiTree root){
    
    
	SeqStack* s = InitStack();
	while(root || !Empty(s)){
    
    
		while(root){
    
    
			ElemType temp;
			printf("%c",root->data);
			temp.node = *root;
			Push(s,temp);
			root = root->LChild;
		}
		if(!Empty(s)){
    
    
			root=GetTop(s)->node.RChild;
			Pop(s);
		}
	}
}
void InOrder1(BiTree root){
    
    
	SeqStack* s = InitStack();
	
	while(root || !Empty(s)){
    
    
		while(root){
    
    
			ElemType temp;
			temp.node = * root;
			Push(s,temp);
			root = root->LChild;
		}
		if(!Empty(s)){
    
    
			printf("%c",GetTop(s)->node.data);
			root = GetTop(s)->node.RChild;
			Pop(s);
		}
	}
}
void PostOrder1(BiTree root){
    
    
    SeqStack* s = InitStack();
    
    while(root || !Empty(s)){
    
    
        while(root){
    
    
            ElemType temp;
            temp.node = * root;
            temp.flag = 0;
            Push(s,temp);
            root = root->LChild;
        }
		//栈非空 且 上一个访问的是右子树 才会访问结点        
        while(!Empty(s)&& GetTop(s)->flag==1){
    
    
            printf("%c",GetTop(s)->node.data);
            Pop(s);
        }
		      
        if(!Empty(s)){
    
    
        	//改变root 下一次循环相当于遍历右子树  
            root = GetTop(s)->node.RChild;
            GetTop(s)->flag = 1;
        }
    }
}
int NonLeafNode(BiTree root){
    
    
    if(!root){
    
    
        return 0;
    }else if(!root->LChild && !root->RChild){
    
    
        return 0;
    }else{
    
    
        return NonLeafNode(root->LChild) + NonLeafNode(root->RChild) + 1;
    }
}
int BiTreeDeepth(BiTree root){
    
    
    if(!root){
    
    
        return 0;
    }else{
    
    
        if(BiTreeDeepth(root->LChild) > BiTreeDeepth(root->RChild)){
    
    
            return BiTreeDeepth(root->LChild) +1 ;
        }else{
    
    
            return BiTreeDeepth(root->RChild) +1 ;
        }
    }
}
//初始化队列 
SeqQueue* InitQueue(void){
    
    
	SeqQueue* q;
    q= (SeqQueue*)malloc(sizeof(SeqQueue));
    q->front = 0;
    q->rear = 0;
    return q;
}
//判队空 
int IsQueueEmpty(SeqQueue* q){
    
    
	if(q->front == q->rear){
    
    
		return 1;
	}else{
    
    
		return 0;
	}
}
//判队满 
int IsQueueFull(SeqQueue* q){
    
    
	if(q->rear - q->front - MAXSIZE){
    
    
		return 0;
	}else{
    
    
		return 1;
	}
}
//入队 
int EnterQueue(SeqQueue* q,QElemType x){
    
    
	if(IsQueueFull(q)){
    
    
		return 0;
	}
	q->rear = (q->rear + 1)% MAXSIZE;
	q->elem[q->rear] = x;
	return 1;
}
//出队 
int DelQueue(SeqQueue* q,QElemType* x){
    
    
	if(IsQueueEmpty(q)){
    
    
		return 0;
	}else{
    
    
		q->front=(q->front+1)%MAXSIZE;
		*x = q->elem[q->front];
		return 1;
	}
}

//层次遍历
void LevelOrder(BiTree root){
    
    
	
	//初始化一个队列	
	SeqQueue* q;
	q = InitQueue();
	QElemType p;
	//首先,根节点入队	
	EnterQueue(q,root);
	
	
	while(!IsQueueEmpty(q)){
    
    
		//队头节点出队,并访问队头节点		
		DelQueue(q,&p);
		printf("%c",p->data);
		
		//出队节点的非空左右孩子依次入队		
		if(p->LChild){
    
    
			EnterQueue(q,p->LChild);
		}
		if(p->RChild){
    
    
			EnterQueue(q,p->RChild);
		}
		
	}
} 
 

猜你喜欢

转载自blog.csdn.net/qq_51368103/article/details/121068593