ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Invert Level

2021-06-15 13:01:09  阅读:192  来源: 互联网

标签:return Level int Invert BiTNode MaxSize front void


AllFun.h

#include <stdio.h>
#include <stdlib.h>     // malloc()
#include <stddef.h>   // NULL
#define MaxSize 10

typedef int ElemType;
typedef struct node {
	ElemType  data;
	struct node* lchild, * rchild;
} *BiTNode;

typedef struct stack {
	BiTNode PArray[MaxSize];
	int top;
} Stack;

typedef struct queue {
	BiTNode PArray[MaxSize];
	int front, rear;
} Queue;

BiTNode create();
void InvertLevel(BiTNode T);

MainFunc.c

#include "AllFun.h"
int main(int argc, char* argv[])
{
	BiTNode T;
	T = create();
	InvertLevel(T);
	return 0;
}

OtheFunc.c

#include "AllFun.h"

BiTNode newNode(ElemType x)
{
	BiTNode T = (BiTNode)malloc(sizeof(struct node));
	if (T) {
		T->data = x;
		T->lchild = T->rchild = NULL;
	}
	return T;
}

void insert(BiTNode* T, ElemType x)
{
	if (*T == NULL) {
		*T = newNode(x);
		return;
	}
	if (x < (*T)->data) {
		insert(&(*T)->lchild, x);
	}
	else {
		insert(&(*T)->rchild, x);
	}
}

BiTNode create()
{
	ElemType num[MaxSize];
	BiTNode T = NULL;
	for (int i = 0; i < MaxSize; ++i) {
		num[i] = ' ';
	}
	printf("Enter some value to create a binary search tree: ");
	for (int i = 0; i < MaxSize; ++i) {
		scanf_s("%d", &num[i]);
		insert(&T, num[i]);
	}
	return T;
}

InitStack(Stack* s)
{
	s->top = -1;
}

InitQueue(Queue* Q)
{
	Q->front = Q->rear = 0;
}

void EnQueue(Queue* Q, BiTNode bt)
{
	if ((Q->rear + 1) % MaxSize == Q->front) {
		return;
	}
	Q->PArray[Q->rear] = bt;
	Q->rear = (Q->rear + 1) % MaxSize;
	return;
}

int IsQEmpty(Queue* Q)
{
	int ret = 0;
	if (Q->rear == Q->front) {
		ret = 1;
	}
	return ret;
}

void DeQueue(Queue* Q, BiTNode* p)
{
	if (Q->rear == Q->front) {
		return;
	}
	*p = Q->PArray[Q->front];
	Q->front = (Q->front + 1) % MaxSize;
	return;
}

void Push(Stack* s, BiTNode p)
{
	if (s->top == MaxSize - 1) {
		printf("stack is full, push fail\n");
	}
	else {
		s->PArray[++s->top] = p;
	}
	return;
}

int IsSEmpty(Stack* s)
{
	int ret = 0;
	if (s->top == -1) {
		ret = 1;
	}
	return ret;
}

void Pop(Stack* s, BiTNode* p)
{
	if (s->top == -1) {
		printf("stack is empty, pop fail\n");
		return;
	}
	*p = s->PArray[s->top--];
	return;
}

void visit(BiTNode p)
{
	printf("%d ", p->data);
}

void InvertLevel(BiTNode bt)
{
	Stack s;
	Queue Q;
	BiTNode p = NULL;
	if (bt != NULL) {
		InitStack(&s);
		InitQueue(&Q);
		EnQueue(&Q, bt);
		while (!IsQEmpty(&Q)) {
			DeQueue(&Q, &p);
			Push(&s, p);
			if (p->lchild) {
				EnQueue(&Q, p->lchild);
			}
			if (p->rchild) {
				EnQueue(&Q, p->rchild);
			}
		}   // while
		while (!IsSEmpty(&s)) {
			Pop(&s, &p);
			visit(p);
		}
	}   // if
}

标签:return,Level,int,Invert,BiTNode,MaxSize,front,void
来源: https://blog.csdn.net/qq_44193781/article/details/117922143

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有