#include
#include
#include
#define MAXSIZE 100
int count = 0;
typedef char datatype;
typedef struct tree
{
datatype info;
struct tree* lchild;
struct tree* rchild;
}bintree;
typedef struct stack
{
bintree* info[100];
int tag[100];
int top;
}seqstack;
bintree* createbintree();
void preprint(bintree* r);
void inprint(bintree* r);
void postprint(bintree* r);
void _preprint(bintree* r, bintree* const root);
void _inprint(bintree* r, bintree* const root);
void _postprint(bintree* r, bintree* const root);
void pushstack(seqstack* s, bintree* r);
bintree* popstack(seqstack* s);
void display(bintree* r);
void countleaves(bintree* r);
void _countleaves(bintree* r);
bintree* lastinprint(bintree* r);
bintree* getparent(bintree* r, datatype a, datatype b);
seqstack* findchildren(bintree* r, datatype a, datatype b);
void getParent1(bintree* r);
二叉树创建
bintree* createbintree()
{
char ch;
bintree* root;
scanf("%c",&ch);
if (ch == '#') return NULL;
else {
root = (bintree*)malloc(sizeof(bintree));
root->info = ch;
root->lchild=createbintree();
root->rchild=createbintree();
}
return root;
}
二叉树三种递归遍历
void preprint(bintree* r)
{
if (!r)return;
printf("%c",r->info);
preprint(r->lchild);
preprint(r->rchild);
}
void inprint(bintree* r)
{
if (!r)return;
inprint(r->lchild);
printf("%c", r->info);
inprint(r->rchild);
}
void postprint(bintree* r)
{
if (!r)return;
postprint(r->lchild);
postprint(r->rchild);
printf("%c",r->info);
}
二叉树三种非递归遍历
void pushstack(seqstack* s, bintree* r)
{
s->info[s->top++] = r;
}
bintree* popstack(seqstack* s)
{
if (s->top) return s->info[--s->top];
else return NULL;
}
void _preprint(bintree* r)
{
seqstack* s;
s = (seqstack*)malloc(sizeof(seqstack));
s->top = 0;
while (r || s->top) {
if(r){
printf("%c",r->info);
pushstack(s, r);
r = r->lchild;
}
else {
r=popstack(s);
r = r->rchild;
}
}
free(s);
}
void _inprint(bintree* r)
{
seqstack* s;
s = (seqstack*)malloc(sizeof(seqstack));
s->top = 0;
while (r || s->top) {
if (r) {
pushstack(s, r);
r = r->lchild;
}
else {
r=popstack(s);
printf("%c",r->info);
r = r->rchild;
}
}
free(s);
}
void _postprint(bintree* r)
{
seqstack* s;
s = (seqstack*)malloc(sizeof(seqstack));
s->top = 0;
while (r || s->top) {
if (r) {
s->info[s->top] = r;
s->tag[s->top++] = 0;
r = r->lchild;
}
else {
if (s->tag[s->top - 1] == 1) {
r = s->info[--s->top];
printf("%c",r->info);
r = NULL;
}
else {
r = s->info[s->top - 1];
s->tag[s->top - 1] = 1;
r = r->rchild;
}
}
}
free(s);
}
递归与非递归计算叶子节点数
void countleaves(bintree* r)
{
if (!r)return;
if (!(r->rchild) && !(r->lchild))count++;
countleaves(r->lchild);
countleaves(r->rchild);
return;
}
void _countleaves(bintree* r)
{
seqstack* s;
s = (seqstack*)malloc(sizeof(seqstack));
s->top = 0;
while (r || s->top) {
if (r) {
if (!(r->lchild) && !(r->rchild))count++;
pushstack(s, r);
r = r->lchild;
}
else {
r = popstack(s);
r = r->rchild;
}
}
free(s);
}
获得中序遍历最后一个节点
bintree* lastinprint(bintree* r)
{
if (r && r->rchild)r = lastinprint(r->rchild);
return r;
}
取得两节点最近共同祖先
bintree* getparent(bintree* r,bintree* a,bintree* b)
{
if (!r|| !a|| !b) return NULL;
if (r->info == a->info || r->info == b->info) return r;
bintree* left = getparent(r->lchild, a,b);
bintree* right = getparent(r->rchild, a,b);
if (left && right) return r;
if (!left) return right;
else return left;
}
void getParent1(bintree* r)
{
char a, b;
fflush(stdin);
getchar();
printf("请输入两个结点的值:");
a = getchar();
b = getchar();
seqstack* s = NULL;
s = findchildren(r, a,b);
printf("节点%c和%c的最近共同祖先为:%c\n",a,b,getparent(r, s->info[0], s->info[1])->info);
}
seqstack* findchildren(bintree* r, datatype a, datatype b)
{
seqstack* ab;
seqstack* s;
ab= (seqstack*)malloc(sizeof(seqstack));
s = (seqstack*)malloc(sizeof(seqstack));
s->top = 0;
ab->top = 0;
while (r || s->top) {
if (r) {
if (ab->top >= 2)return ab;
if (r->info == a || r->info == b)ab->info[ab->top++] = r;
pushstack(s, r);
r = r->lchild;
}
else {
r = popstack(s);
r = r->rchild;
}
}
}
界面函数和主函数
void display(bintree* r)
{
system("cls");
printf("=======================================\n");
printf("=============二叉树====================\n");
printf("========1.前序遍历输出=================\n");
printf("========2.中序遍历输出=================\n");
printf("========3.后序遍历输出=================\n");
printf("========4.计算叶子节点数===============\n");
printf("========5.返回中序遍历最后一个结点=====\n");
printf("========6.返回两节点间共同祖先=========\n");
printf("========0.退出程序=====================\n");
printf("=======================================\n");
char a;
while (1) {
a = getch();
switch (a) {
case '1': {printf("前序遍历输出结果为:"); preprint(r); printf("\n按任意键返回"); getch(); display(r); break; }
case '2': {printf("中序遍历输出结果为:"); inprint(r); printf("\n按任意键返回"); getch(); display(r); break; }
case '3': {printf("后序遍历输出结果为:"); postprint(r); printf("\n按任意键返回"); getch(); display(r); break; }
case '4': {countleaves(r); printf("叶子节点数为:%d\n", count); printf("\n按任意键返回"); getch(); count = 0; display(r); break; }
case '5': {printf("中序遍历最后一个节点为:%c\n",lastinprint(r)->info); printf("\n按任意键返回"); getch(); display(r); break; }
case '6': { getParent1(r); printf("\n按任意键返回"); getch(); display(r); break; }
case '0': {exit(0); }
}
}
}
int main()
{
printf("正在建立二叉树:\n");
printf("请输入字符,以#作为节点的结束:");
bintree* root = NULL;
root=createbintree();
while(1)display(root);
return 0;
}