树的基本操作
1.创建二叉树
2.先序遍历二叉树
3.中序遍历二叉树
4.后序遍历二叉树
5.层次遍历二叉树
6.计算树的的高度
7.计算叶子个数
8.非递归遍历二叉树.
源代码如下:
#include<stdio.h>
#include<stdlib.h>
#define ElemType int
typedef struct BNode{
ElemType data;
struct BNode * left;
struct BNode * right;
}BNode,*BTree;
typedef struct QNode{
BNode *data;
struct QNode * next;
}QNode;
typedef struct{
QNode * front;
QNode * rear;
}Queue;
typedef struct Stack{
QNode * base;
QNode * top;
}Stack;
void CreateBTree(BNode **t); //创建二叉树
void preOrder(BNode *T); //先序遍历
void midOrder(BNode *T); //中序遍历
void lastOrder(BNode *T);
int calHeight(BNode *T); //计算树的高度
void layerOrder(BNode *T); //层次遍历
int countLeaf(BNode *T); //计算叶子个数
void midOrderStack(BNode *T); //非递归中序遍历
void createQueue(Queue *q);
void enQueue(Queue *q,BNode * data);
QNode * deQueue(Queue *q);
void showQueue(Queue *q);
void createStack(Stack *s);
void push(Stack *s,BNode *e);
QNode * pop(Stack *s);
void createMenu()
{
int i;
char * menu[] = {
"\t\t Welcome \n ",
"\t\t1.创建二叉树\n",
"\t\t2.先序遍历二叉树\n",
"\t\t3.中序遍历二叉树\n",
"\t\t4.后序遍历二叉树\n",
"\t\t5.层次遍历二叉树\n",
"\t\t6.计算树的的高度\n",
"\t\t7.计算叶子个数\n",
"\t\t8.非递归遍历二叉树\n",
"\t\t请选择(1~8):"};
for(i=0;i<10;i++)
{
printf("%s",menu[i]);
}
}
void main()
{
BNode *t=NULL;
int select=1;
char ch;
while(select)
{
system("cls");
createMenu();
scanf("%d",&select);
switch (select)
{
case 0:
exit(1);
case 1:
CreateBTree(&t);
break;
case 2:
preOrder(t);
break;
case 3:
midOrder(t);
break;
case 4:
lastOrder(t);
break;
case 5:
layerOrder(t);
break;
case 6:
printf("\n The height is %d\n",calHeight(t));
break;
case 7:
printf("\n The num of the leaf is %d\n",countLeaf(t));
break;
case 8:
midOrderStack(t);
break;
}
if(select == 1 || select == 2||select == 3||select == 4||select == 5||select == 6||select == 7||select == 8)
{
printf("按回车键继续!");
scanf("%c%*c",&ch); //%*c 表示读取字符,但不存储。
}
}
// Queue q;
// createQueue(&q);
// enQueue(&q,3);
// enQueue(&q,4);
// enQueue(&q,5);
// showQueue(&q);
}
void CreateBTree(BNode **t)
{
int temp;
scanf("%d",&temp);
if(temp==0)
{
(*t)=NULL;
}else{
(*t)=(BNode *)malloc(sizeof(BNode));
(*t)->data = temp;
printf("请输入%d的左孩子",(*t)->data);
CreateBTree(&(*t)->left);
printf("请输入%d的右孩子",(*t)->data);
CreateBTree(&(*t)->right);
}
}
void preOrder(BNode *T)
{
if(T!=NULL)
{
printf("%2d",T->data);
preOrder(T->left);
preOrder(T->right);
}
}
void midOrder(BNode *T)
{
if(T!=NULL)
{
midOrder(T->left);
printf("%2d",T->data);
midOrder(T->right);
}
}
//中序排列非递归算法
//找到最左节点。。。。经过的节点全部入栈,弹栈,打印节点,对节点的右子树做同样的操作
void midOrderStack(BNode *T)
{
Stack s;
BNode * p = (BNode*)malloc(sizeof(BNode));
QNode *node =(QNode *)malloc(sizeof(QNode));
createStack(&s);
p=T;
while(p || s.top->next)
{
while(p) //找到最左节点
{
push(&s,p);
p=p->left;
}
node = pop(&s);
if(node)
{
printf("%2d",node->data->data);
p=node->data->right; //对节点的右孩子进行同样的操作
}
}
}
void lastOrder(BNode *T)
{
if(T!=NULL)
{
lastOrder(T->left);
lastOrder(T->right);
printf("%2d",T->data);
}
}
void layerOrder(BNode *T)
{
Queue q;
QNode * temp=NULL;
createQueue(&q);
enQueue(&q,T);
temp=deQueue(&q);
//每一个节点的左右孩子入队列。。。并依此出队列
while(temp)
{
printf("%2d",temp->data->data);
if(temp->data->left)
enQueue(&q,temp->data->left);
if(temp->data->right)
enQueue(&q,temp->data->right);
temp=deQueue(&q);
}
}
int countLeaf(BNode *T)
{
if(!T->left && !T->right)
return 1;
else
{
return countLeaf(T->left)+countLeaf(T->right);
}
}
//
// void CreateBTree(BTree *t)
// {
//
// int temp;
//
// scanf("%d",&temp);
//
//
// if(temp==0)
// {
// t=NULL;
// }else{
//
// t=(BNode *)malloc(sizeof(BNode));
//
// t->data = temp;
//
// printf("请输入%d的左孩子",t->data);
// CreateBTree(t->left);
// printf("请输入%d的右孩子",t->data);
// CreateBTree(t->right);
// }
// }
//
//
// void show(BTree &T)
// {
//
// if(T)
// printf("%2d",T->data);
// else
// printf("sorry\n");
// // if(T!=NULL)
// // {
// // printf("%2d",T->data);
// //
// // show(T->left);
// // show(T->right);
// // }
// }
int calHeight(BNode *T)
{
if(T)
{
if(!T->left && !T->right)
return 1;
else{
return (max(calHeight(T->left),calHeight(T->right))+1);
}
}else{
return 0;
}
}
void createQueue(Queue *q)
{
q->front=(QNode *)malloc(sizeof(QNode));
q->rear=q->front;
q->front->next=NULL;
}
void enQueue(Queue *q,BNode *data)
{
QNode *temp = (QNode*) malloc(sizeof(QNode));
temp->data = data;
temp->next = NULL;
q->rear->next = temp;
q->rear = q->rear->next;
}
QNode * deQueue(Queue *q)
{
QNode *p=NULL;
if(q->front->next)
{
p=q->front->next;
q->front=q->front->next;
}
return p;
}
void showQueue(Queue *q)
{
QNode * p = deQueue(q);
while(p)
{
printf("%2d",p->data);
p=p->next;
}
}
void createStack(Stack *s)
{
QNode * p = (QNode*)malloc(sizeof(QNode));
s->base = s->top = p;
s->top->next=NULL;
}
void push(Stack *s,BNode *e)
{
QNode * p = (QNode*)malloc(sizeof(QNode));
p->data = e;
p->next = NULL;
if(s->top->next)
{
p->next=s->top->next;
s->top->next = p;
}
else{
s->top->next= p;
}
}
QNode * pop(Stack *s)
{
QNode *p =(QNode*)malloc(sizeof(QNode));
p=s->top->next;
s->top->next=p->next;
return p;
}
实验结果截图有点多,就不贴了。。。