源代码
#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <stdlib.h>
#define MAXSIZE 100
typedef struct node {
char data[10];
struct node *left;
struct node *right;
}Node;
typedef struct queueNode {
Node node;
struct queueNode *next;
}QueueNode;
typedef struct {
QueueNode *front;
QueueNode *rear;
}Queue;
typedef struct{
Node *nodes[MAXSIZE];
int top;
}Stack;
void initQueue(Queue *q) {
if (!(q->front = (QueueNode *)malloc(sizeof(QueueNode)))) {
printf("\n队列初始化失败!\n");
return;
}
q->rear = q->front;
q->front->next = NULL;
q->rear->next = NULL;
}
void enQueue(Queue *q, Node node) {
QueueNode *qn;
if ( !(qn = (QueueNode *)malloc(sizeof(QueueNode)) ) ) {
printf("\n队列结点初始化失败!\n");
return;
}
qn->node = node;
qn->next = NULL;
q->rear->next = qn;
q->rear = qn;
}
Node deQueue(Queue *q) {
if (q->front == q->rear) {
printf("\n队列为空!\n");
exit(-1);
}
QueueNode *qn;
Node node;
if (q->front->next == q->rear) {
qn = q->rear;
node = qn->node;
q->rear = q->front;
free(qn);
}
else
{
qn = q->front->next;
node = qn->node;
q->front->next = qn->next;
free(qn);
}
return node;
}
void initStack(Stack *s) {
s->top = -1;
}
int StackIsEmpty(Stack *s) {
if(s->top < 0){
return 1;
}else{
return 0;
}
}
int StackIsFull(Stack *s) {
if (s->top >= MAXSIZE) {
return 1;
}
else
{
return 0;
}
}
void StackPush(Stack *s, Node *node) {
if (StackIsFull(s)) {
printf("\n栈已满!\n");
return;
}
s->top++;
s->nodes[s->top] = node;
}
Node *StackPop(Stack *s) {
if(StackIsEmpty(s)){
printf("\n栈为空!\n");
return NULL;
}
Node *node = s->nodes[s->top];
s->top--;
return node;
}
Node *StackGetTop(Stack *s) {
if(StackIsEmpty(s)){
return NULL;
}
return s->nodes[s->top];
}
int addNode(Node *parent, Node *node, int type) {
if (parent == NULL) {
printf("父结点不存在!\n");
return 0;
}
switch (type)
{
case 1:
if (parent->left) {
return 0;
}
else {
parent->left = node;
}
break;
case 2:
if (parent->right) {
return 0;
}
else
{
parent->right = node;
}
break;
default:
printf("参数有误!\n");
return 0;
}
return 1;
}
Node *getLeftTree(Node *parent) {
if (parent) {
return parent->left;
}
else
{
return NULL;
}
}
Node *getRightTree(Node *parent) {
if (parent) {
return parent->right;
}
else
{
return NULL;
}
}
int isEmpty(Node *node) {
if (node) {
return 0;
}
else
{
return 1;
}
}
int getTreeDepth(Node *parent) {
int depth1, depth2;
if (parent == NULL) {
return 0;
}
else {
depth1 = getTreeDepth(parent->left);
depth2 = getTreeDepth(parent->right);
if (depth1 > depth2) {
return depth1 + 1;
}
else {
return depth2 + 1;
}
}
}
Node *find(Node *parent, char* data) {
Node *target;
if (!parent) {
return NULL;
}
else {
if (strcmp(parent->data, data) == 0) {
return parent;
}
else {
if (target = find(parent->left, data)) {
return target;
}
else if(target = find(parent->right, data)){
return target;
}
else {
return NULL;
}
}
}
}
Node *find2(Node *parent, char* data) {
Node *target;
if (!parent) {
return NULL;
}
else {
if (target = find(parent->left, data)) {
return target;
}
else if (target = find(parent->right, data)) {
return target;
}
else {
return NULL;
}
}
}
void destroyTree(Node *parent) {
if (parent) {
destroyTree(parent->left);
destroyTree(parent->right);
free(parent);
parent = NULL;
}
return;
}
void preOrder(Node *node) {
if (node) {
printf("%s ", node->data);
preOrder(node->left);
preOrder(node->right);
}
return;
}
void infixOrder(Node *node) {
if (node) {
infixOrder(node->left);
printf("%s ", node->data);
infixOrder(node->right);
}
return;
}
void infixOrder2(Node *node) {
Stack *s;
if (!(s = (Stack *)malloc(sizeof(Stack)))) {
printf("创建栈失败!\n");
return;
}
Node *p;
initStack(s);
StackPush(s,node);
printf("\n");
while(!StackIsEmpty(s)){
while(p = StackGetTop(s)){
StackPush(s, p->left);
}
StackPop(s);
if(!StackIsEmpty(s)){
p = StackPop(s);
printf("\t%s\t",p->data);
StackPush(s, p->right);
}
}
printf("\n");
}
void postOrder(Node *node) {
if (node) {
postOrder(node->left);
postOrder(node->right);
printf("%s ", node->data);
}
return;
}
void levelOrder(Node *root) {
Queue *q = (Queue *)malloc(sizeof(Queue));
initQueue(q);
Node node;
printf("\n层序遍历:\n");
if (root != NULL) {
enQueue(q,*root);
while (q->front != q->rear) {
node = deQueue(q);
printf("\t%s", node.data);
if (node.left != NULL) {
enQueue(q, *node.left);
}
if (node.right != NULL) {
enQueue(q, *node.right);
}
}
}
else
{
printf("\n这是一棵空树!\n");
}
}
void addNode(Node *bt) {
Node *node, *parent;
char data[10];
char choice;
if (node = (Node *)malloc(sizeof(Node))) {
printf("\n请输入该结点的数据:");
fflush(stdin);
scanf("%s", node->data);
node->left = NULL;
node->right = NULL;
printf("\n输入父结点数据: ");
fflush(stdin);
scanf("%s", data);
parent = find(bt, data);
if (!parent) {
printf("\n未找到父结点!\n");
free(node);
return;
}
printf("\n1.添加到左子树 2.添加到右子树\n");
do {
choice = getch();
if (choice == '1' || choice == '2') {
while (addNode(parent, node, choice - '0') == 0) {
parent = find2(parent, data);
if (parent == NULL) {
return;
}
}
}
} while (choice != '1' && choice != '2');
}
return;
}
void revertNode(Node *node) {
if (node->left != NULL || node->right != NULL) {
Node * temp;
temp = node->left;
node->left = node->right;
node->right = temp;
}
if (node->left != NULL) {
revertNode(node->left);
}
if (node->right != NULL) {
revertNode(node->right);
}
}
Node *initRoot() {
Node *root;
char data[10];
if (root = (Node *)malloc(sizeof(Node))) {
printf("\n请输入根结点数据:");
scanf("%s", root->data);
root->left = NULL;
root->right = NULL;
return root;
}
return NULL;
}
int main() {
Node *root = NULL;
int choice = 0;
do {
printf("\n1.设置树根 2.添加结点\n");
printf("3.先序遍历 4.中序遍历\n");
printf("5.后序遍历 6.二叉树深度\n");
printf("7.清空二叉树 0.退出\n");
printf("8.层序遍历 9.中序非递归遍历\n");
printf("10.交换所有结点左右子树\n");
printf("\n请输入选项:");
scanf("%d",&choice);
switch (choice)
{
case 1:
root = initRoot();
break;
case 2:
addNode(root);
break;
case 3:
printf("\n先序遍历:\n");
preOrder(root);
break;
case 4:
printf("\n中序遍历:\n");
infixOrder(root);
break;
case 5:
printf("\n后序遍历:\n");
postOrder(root);
break;
case 6:
printf("\n二叉树深度为:%d\n", getTreeDepth(root));
break;
case 7:
destroyTree(root);
break;
case 8:
levelOrder(root);
break;
case 9:
infixOrder2(root);
break;
case 10:
revertNode(root);
printf("交换完成...\n");
break;
default:
break;
}
} while (choice != 0);
destroyTree(root);
root = NULL;
return 0;
}