Data structure binary tree traversal

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h>
#include<time.h>
const int maxn = 1e5 + 50;
const int N = 1e6;
int sum = 0;
typedef int ElementType;
typedef struct TNode* BinTree;
typedef struct SNode* stack;
typedef struct QNode* queue;
//建树
typedef struct TNode {
    
    
	ElementType Data;
	BinTree Left, Right;
};
BinTree createTree() {
    
    
	BinTree BT;
	char str;
	str = getchar();
	if (str == '#') {
    
    
		BT = NULL;
	}
	else {
    
    
		BT = (BinTree)malloc(sizeof(struct TNode));
		BT->Data = str;
		BT->Left = createTree();
		BT->Right = createTree();
	}
	return BT;
}

//实现栈
typedef struct SNode {
    
    
	BinTree Data;
	stack Next;
};
stack makeStack() {
    
    
	stack p = (stack)malloc(sizeof(struct SNode));
	p->Next = NULL;
	return p;
}
int isEmpty(stack p) {
    
    
	if (p->Next == NULL)return 1;
	else return 0;
}
void push(stack p, BinTree x) {
    
    
	stack temp = (stack)malloc(sizeof(struct SNode));
	temp->Data = x;
	temp->Next = p->Next;
	p->Next = temp;
}
BinTree pop(stack p) {
    
    
	stack temp = (stack)malloc(sizeof(struct SNode));
	BinTree top;
	if (isEmpty(p)) {
    
    
		printf("The stack is empty\n");
		return NULL;
	}
	else {
    
    
		temp = p->Next;
		p->Next = temp->Next;
		top = temp->Data;
		free(temp);
		return top;
	}
}

//实现循环队列
typedef struct QNode {
    
    
	BinTree Data[maxn];
	int front;
	int rear;
};
queue makeQueue() {
    
    
	queue q = (queue)malloc(sizeof(struct QNode));
	q->front = 0;
	q->rear = 0;
	return q;
}
int isEmpty(queue q) {
    
    
	if (q->front == q->rear)return 1;
	else return 0;
}
int isFull(queue q) {
    
    
	if (q->front == (q->rear + 1) % maxn)return 1;
	else return 0;
}
void push(queue q, BinTree x) {
    
    
	if (isFull(q)) {
    
    
		printf("The queue is full\n");
	}
	q->rear++;
	q->Data[q->rear] = x;
}
BinTree pop(queue q) {
    
    
	if (isEmpty(q)) {
    
    
		printf("The queue is empty\n");
		return NULL;
	}
	else {
    
    
		q->front++;
		return q->Data[q->front];
	}
}

//遍历操作
//ABD#G###CE##FH###
void inOrder(BinTree BT) {
    
    
	if (BT) {
    
    
		inOrder(BT->Left);
		printf("%c ", BT->Data);
		inOrder(BT->Right);
	}
}
void preOrder(BinTree BT) {
    
    
	if (BT) {
    
    
		printf("%c ", BT->Data);
		preOrder(BT->Left);
		preOrder(BT->Right);
	}
}
void postOrder(BinTree BT) {
    
    
	if (BT) {
    
    
		postOrder(BT->Left);
		postOrder(BT->Right);
		printf("%c ", BT->Data);
	}
}
void iter_Inorder(BinTree BT) {
    
    //栈实现中序遍历
	stack s = makeStack();
	while (BT || !isEmpty(s)) {
    
    
		while (BT) {
    
    
			push(s, BT);
			BT = BT->Left;
		}
		if (!isEmpty(s)) {
    
    
			BT = pop(s);
			printf("%c ", BT->Data);
			BT = BT->Right;
		}
	}
}
void iter_preOrder(BinTree BT) {
    
    
	stack s = makeStack();
	while (BT || !isEmpty(s)) {
    
    
		while (BT) {
    
    
			printf("%c ", BT->Data);
			push(s, BT);
			BT = BT->Left;
		}
		if (!isEmpty(s)) {
    
    
			BT = pop(s);
			BT = BT->Right;
		}
	}
}
void levelOrder(BinTree BT) {
    
    //队列实现层序遍历
	queue q = makeQueue();
	if (BT)push(q, BT);
	while (!isEmpty(q)) {
    
    
		BT = pop(q);
		printf("%c ", BT->Data);
		if (BT->Left)push(q, BT->Left);
		if (BT->Right)push(q, BT->Right);
	}
}
void sumLeaf(BinTree BT) {
    
    //计算叶子节点个数
	if (BT) {
    
    
		if (BT->Left == NULL && BT->Right == NULL)sum++;
		sumLeaf(BT->Left);
		sumLeaf(BT->Right);
	}
}
int height(BinTree BT) {
    
    //树的高度
	int height_left, height_right;
	if (BT) {
    
    
		height_left = height(BT->Left);
		height_right = height(BT->Right);
		if (height_left > height_right)return height_left + 1;
		else return height_right + 1;
	}
	else {
    
    
		return 0;
	}
}
int main() {
    
    
	//freopen("D:/vs/c/Project2/input.txt", "r", stdin);//反斜杠取地址
	//freopen("D:/vs/c/Project2/output.txt", "w", stdout);
	int n;
	BinTree BT;
	ElementType X;
	BT = NULL;
	BT = createTree();

	printf("Height:");
	printf("%d\n", height(BT));

	printf("Leaf:");
	sumLeaf(BT);
	printf("%d\n", sum);

	printf("inOrder:");
	inOrder(BT);
	printf("\n");

	printf("preOrder:");
	preOrder(BT);
	printf("\n");

	printf("postOrder:");
	postOrder(BT);
	printf("\n");

	printf("iterInorder:");
	iter_Inorder(BT);
	printf("\n");

	printf("iterpreOrder:");
	iter_preOrder(BT);
	printf("\n");

	printf("levelOrder:");
	levelOrder(BT);
	printf("\n");
	//fclose(stdin);//关闭重定向输入
	//fclose(stdout);//关闭重定向输出 
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_40924271/article/details/109676217