刷题(数据结构-树)

1、编写后序遍历二叉树的非递归算法。

代码实现

/*后序遍历二叉树的非递归算法*/
#include"stdio.h"
#include"malloc.h"

typedef char datatype;

typedef struct BiNode
{
	datatype data;
	struct BiNode *lchild,*rchild;
}bitree;

bitree *creatbitree()//创建 ABD#G###CE##F##
{
	bitree *root;
	datatype x;
	x=getchar();
	if(x=='#')
	  root=NULL;
	else
	{
	  root=(bitree *)malloc(sizeof(bitree));
	  root->data=x;
  	  root->lchild=creatbitree();
	  root->rchild=creatbitree();
	}
	return root;
}
void Postorder(bitree *root)//后序 GDBEFCA
{
/*	if(!root) return;//递归算法 
	else
	{
		Postorder(root->lchild);
		Postorder(root->rchild);
		printf("%c ",root->data);

	}*/
    //非递归 
    if(root == NULL) return;  
    bitree *stack[100], *p;  
    int top = -1;  
    bitree *prev = NULL;    
    stack[++top] = root;  
    while (top != -1) {  
        p = stack[top];   
        // 如果当前节点没有孩子或者它的孩子已经被访问过  
        if ((p->lchild == NULL && p->rchild == NULL) ||  
            (prev != NULL && (prev == p->lchild || prev == p->rchild))) {  
            printf("%c", p->data);  
            top--;  
            prev = p;  
        } else {  
            // 否则,先将右孩子入栈,再将左孩子入栈  
            if (p->rchild != NULL) stack[++top] = p->rchild;  
            if (p->lchild != NULL) stack[++top] = p->lchild;  
        }  
    }  
}
main()
{
	int n,i,j,k,z;
	bitree *T,*root,*p;
	printf("请按照前序排列的方式输入你想要创建的二叉树:");
     root=creatbitree();
     printf("二叉树的后序遍历顺序为:");
     Postorder(root);
}

2、试给出二叉树的自下而上、从右到左的层次遍历算法。

草图分析:

代码实现:

/*二叉树的自下而上、从右到左的层次遍历算法*/ 
#include <stdio.h>  
#include <stdlib.h>  
  
#define MAXSIZE 20  
  
typedef struct{  
    int data[MAXSIZE];  
    int front, rear;  
}Queue;  

typedef struct {  
    int data[MAXSIZE];  
    int top;  
}Stack;  
  
typedef char datatype;  
typedef struct BiNode{  
    datatype data;  
    struct BiNode *lchild, *rchild;  
} bitree;  
  
// 队列操作函数  
void initQueue(Queue *q){  
    q->front = 0;  
    q->rear = -1;  
}  
  
int isEmpty(Queue *q){  
    return (q->front > q->rear);  
}  
  
void enqueue(Queue *q, bitree *node){  
    if(q->rear == MAXSIZE - 1){  
        printf("队满!\n");  
        return;  
    }  
    q->rear++;  
    q->data[q->rear] = node;  
}  
  
bitree *dequeue(Queue *q){  
    if(isEmpty(q)){  
        printf("队空!\n");  
        return NULL;  
    }  
    bitree *node = q->data[q->front];  
    q->front++;  
    return node;  
}  
  
// 栈操作函数  
void initStack(Stack *s){  
    s->top = -1;  
}  
  
int isStackEmpty(Stack *s){  
    return (s->top == -1);  
}  
  
void push(Stack *s, bitree *node){  
    if(s->top == MAXSIZE - 1){  
        printf("栈满!\n");  
        return;  
    }  
    s->top++;  
    s->data[s->top] = node;  
}  
  
bitree *pop(Stack *s){  
    if(isStackEmpty(s)){  
        printf("栈空!\n");  
        return NULL;  
    }  
    bitree *node = s->data[s->top];  
    s->top--;  
    return node;  
}  
  
// 创建二叉树(使用前序遍历方式输入)  
bitree *creatbitree(){  //创建 ABD#G###CE##F##
    char x;  
    scanf("%c", &x);  
    if (x == '#') {  
        return NULL;  
    }  
    bitree *root = (bitree *)malloc(sizeof(bitree));  
    root->data = x;  
    root->lchild = creatbitree();  
    root->rchild = creatbitree();  
    return root;  
}  
  
//层次遍历并自下而上、从右往左输出二叉树节点  
//正确结果:GFEDCBA
void Invert_level(bitree *T){  
    if(T == NULL) return;  
      
    Queue q;  
    Stack s;  
    initQueue(&q);  
    initStack(&s);  
      
    // 根节点入队  
    enqueue(&q, T);  
    
    // 遍历当前层的所有节点  
    while(!isEmpty(&q)){
        bitree *node = dequeue(&q); 
        // 将当前节点压入栈中  
            push(&s,node);  
              
            // 如果当前节点有左子节点,则将左子节点入队  
            if(node->lchild != NULL){  
                enqueue(&q, node->lchild);  
            }  
            // 如果当前节点有右子节点,则将右子节点入队  
            if(node->rchild != NULL){  
                enqueue(&q, node->rchild);  
            }  
        }      
            
    // 访问当前层的所有节点(自下而上,从右往左)  
     while(!isStackEmpty(&s)){  
          bitree *node = pop(&s);  
          printf("%c ",node->data);  
     }    
}  
  
int main() {  
    printf("请按照前序排列的方式输入你想要创建的二叉树(使用'#'表示空节点): ");  
    bitree *T = creatbitree();  
      
    printf("二叉树的层次遍历顺序为:(自下而上,从右往左)\n");  
    Invert_level(T);  
    return 0;  
}  
  

3、假设二叉树采用二叉链表存储结构,设计一个非递归算法求二叉树的高度。

草图分析:

代码实现:

/*后序遍历二叉树的非递归算法*/
#include"stdio.h"
#include"malloc.h"

#define MAXSIZE 20

typedef char datatype;

typedef struct BiNode
{
	datatype data;
	struct BiNode *lchild,*rchild;
}bitree;

typedef struct {  
    int data[MAXSIZE];  
    int front, rear;  
} Queue;  

bitree *creatbitree()//创建 ABD#G###CE##F##
{
	bitree *root;
	datatype x;
	x=getchar();
	if(x=='#')
	  root=NULL;
	else
	{
	  root=(bitree *)malloc(sizeof(bitree));
	  root->data=x;
  	  root->lchild=creatbitree();
	  root->rchild=creatbitree();
	}
	return root;
}
/*int Height(bitree *T)//递归 
{
	int m,n;
	if(!T) return 0;
	else
	{
	   m=Height(T->lchild);
	   n=Height(T->rchild);
	   return(m>n)?m+1:n+1;
	}
}*/
int Height(bitree *T)//非递归 
{
	int i,h=0;
	bitree *Queue[MAXSIZE];
	int front,rear; 
	bitree *last = T; 
	bitree *p;
	front = rear = -1;
	Queue[++rear] = T;
     while(front < rear){  
        int level = rear - front; //当前层的节点数  
        for(i = 0;i < level;i++){  
            p = Queue[++front];          
            if(p->lchild) Queue[++rear] = p->lchild;
            if(p->rchild) Queue[++rear] = p->rchild;
        }  
        h++;    
     }  
	return h;
}
main()
{
	bitree *T,*root,*p;
	printf("请按照前序排列的方式输入你想要创建的二叉树:");
    root=creatbitree();
    	int h=Height(root);
    printf("二叉树的高度为:%d",h);
    putchar('\n');  
}

未完待续...

  • 6
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值