实验题目:二叉树的建立、遍历及其应用
两个钟纯手打,用cpp估计半个钟就好了,没有stl太麻烦了。
设树结点的元素类型为char,实现以下二叉树的各种基本操作的程序:
① 建立不少于10个结点的二叉树T;
② 用非递归方式先序遍历方式输出树T的结点;
③ 用中序遍历方式输出树T的结点;
④ 用后序遍历方式输出树T的结点;
⑤ 用层次遍历方式输出树T的结点;
⑥ 输出树T的深度;
⑦ 输出树T的叶子结点和非叶子结点;
⑧ 主函数通过函数调用实现以上各项操作。
实现代码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define DataType char
//树节点
typedef struct TreeNode {
DataType data;
struct Node* lnode;
struct Node* rnode;
}TNode, * TNODE;
//创建树节点
TNODE createTNode(DataType data) {
TNODE node = (TNODE)malloc(sizeof(TNode));
node->data = data;
node->lnode = node->rnode = NULL;
return node;
}
//栈/队列节点
typedef struct Node {
TNODE tnode;
struct Node* next;
}Node, * NODE;
//创建栈/队列节点
NODE createNode(TNODE tnode) {
NODE node = (NODE)malloc(sizeof(Node));
node->tnode = tnode;
node->next = NULL;
return node;
}
//队列
typedef struct Queue
{
NODE front, tail;
}Queue, * QUEUE;
//栈
typedef struct Stack
{
NODE top;
}Stack, * STACK;
//树
typedef struct Tree {
TNODE root;
}Tree, * TREE;
//创建队列
QUEUE createQueue() {
QUEUE queue = (QUEUE)malloc(sizeof(Queue));
queue->front = queue->tail = NULL;
return queue;
}
//创建栈
STACK createStack() {
STACK stack = (STACK)malloc(sizeof(Stack));
stack->top = NULL;
return stack;
}
//创建树
TREE createTree() {
TREE tree = (TREE)malloc(sizeof(Tree));
tree->root = NULL;
return tree;
}
//队列输入
void QueuePush(QUEUE queue, TNODE tnode) {
if (queue->front == NULL) {
queue->front = queue->tail = createNode(tnode);
}
else {
queue->tail->next = createNode(tnode);
queue->tail = queue->tail->next;
}
}
//获取队列头
TNODE QueueGetFront(QUEUE queue) {
return queue->front->tnode;
}
//队列出队
void QueuePop(QUEUE queue) {
NODE node = queue->front;
queue->front = queue->front->next;
if (!queue->front) queue->front = queue->tail = NULL;
free(node);
}
//判断树是否为空
int QueueNotEmpty(QUEUE queue) {
if (queue->front) return 1;
return 0;
}
//入栈
void StackPush(STACK stack, TNODE tnode)
{
NODE node;
if (!stack->top) stack->top = createNode(tnode);
else {
node = createNode(tnode);
node->next = stack->top;
stack->top = node;
}
}
void StackPop(STACK stack) {
NODE temp = stack->top;
stack->top = stack->top->next;
free(temp);
}
//判断栈是否为空
int StackNotEmpty(STACK stack) {
if (stack->top) return 1;
return 0;
}
//插入树节点
void InsertTree(TREE tree, DataType data) {
TNODE tnode;
QUEUE queue = createQueue();
QueuePush(queue, tree->root);
while (QueueNotEmpty(queue)) {
tnode = QueueGetFront(queue);
if (tnode->lnode) QueuePush(queue, tnode->lnode);
else {
tnode->lnode = createTNode(data);
return;
}
if (tnode->rnode) QueuePush(queue, tnode->rnode);
else {
tnode->rnode = createTNode(data);
return;
}
QueuePop(queue);
}
free(queue);
}
//非递归先序
void PreOrder(TREE tree) {
TNODE tnode;
printf("\n先序输出:");
STACK stack = createStack();
StackPush(stack, tree->root);
while (StackNotEmpty(stack)) {
tnode = stack->top->tnode;
StackPop(stack);
printf("%c", tnode->data);
if (tnode->rnode) StackPush(stack, tnode->rnode);
if (tnode->lnode) StackPush(stack, tnode->lnode);
}
free(stack);
}
//中序
void InOrder(TNODE root) {
if (root) {
InOrder(root->lnode);
printf("%c", root->data);
InOrder(root->rnode);
}
else return;
}
int GetHeight(TNODE tnode) {
if (!tnode) return -1;
else return (max(GetHeight(tnode->lnode), GetHeight(tnode->rnode))) + 1;
}
//后序
void PostOrder(TNODE root) {
if (root) {
PostOrder(root->lnode);
PostOrder(root->rnode);
printf("%c", root->data);
}
else return;
}
void PrintLeaves(TREE tree) {
TNODE tnode;
QUEUE queue = createQueue();
QUEUE output = createQueue();
QUEUE others = createQueue();
QueuePush(queue, tree->root);
while (QueueNotEmpty(queue)) {
tnode = QueueGetFront(queue);
if (!tnode->lnode && !tnode->rnode) QueuePush(output, createTNode(tnode->data));
else QueuePush(others, createTNode(tnode->data));
QueuePop(queue);
if (tnode->lnode) QueuePush(queue, tnode->lnode);
if (tnode->rnode) QueuePush(queue, tnode->rnode);
}
printf("\n输出叶子节点:");
while (QueueNotEmpty(output)) {
tnode = QueueGetFront(output);
printf("%c", tnode->data);
QueuePop(output);
}
printf("\n输出非叶子节点:");
while (QueueNotEmpty(others)) {
tnode = QueueGetFront(others);
printf("%c", tnode->data);
QueuePop(others);
}
free(others);
free(output);
free(queue);
}
void Print(TREE tree) {
TNODE tnode;
QUEUE queue = createQueue();
printf("\n层次遍历:");
QueuePush(queue, tree->root);
while (QueueNotEmpty(queue)) {
tnode = QueueGetFront(queue);
printf("%c", tnode->data);
QueuePop(queue);
if (tnode->lnode) QueuePush(queue, tnode->lnode);
if (tnode->rnode) QueuePush(queue, tnode->rnode);
}
PreOrder(tree);
printf("\n中序遍历:");
InOrder(tree->root);
printf("\n后序遍历:");
PostOrder(tree->root);
printf("\n树的高度是:%d", GetHeight(tree->root));
PrintLeaves(tree);
printf("\n");
}
int main() {
int i;
DataType data;
TREE tree = createTree();
printf("请输入一串字符作为树的节点,到#结束输入\n");
for (i = 0;; i++) {
scanf("%c", &data);
if (data == '#') break;
if (!tree->root) tree->root = createTNode(data);
else InsertTree(tree, data);
}
Print(tree);
return 0;
}
实现效果