⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tree7.c

📁 括号匹配问题和建立二叉树问题
💻 C
字号:
#include <stdio.h>
#include <malloc.h>
#include <conio.h>
#define OK 1;
typedef struct BiTNode
{
char data;
struct BiTNode *lchild,*rchild;
} BiTNode;

int create(BiTNode **T) /* 建立二叉树,空格结束子树或树*/
{char ch;
scanf("%c",&ch);
if(ch==' ')    *T=NULL;
else
    {
    *T=(BiTNode *)malloc(sizeof(BiTNode));
    (*T)->data=ch;
    create(&((*T)->lchild));
    create(&((*T)->rchild));
    }
return OK;
}
void preorder(BiTNode *T)
{if(T)
{ printf("  %c",T->data);
  preorder(T->lchild);
  preorder(T->rchild);
}
}
void inorder(BiTNode *T)
{if(T)
{ inorder(T->lchild);
  printf("  %c",T->data);
  inorder(T->rchild);
}
}
void postorder(BiTNode *T)
{
if(T)
{postorder(T->lchild);
 postorder(T->rchild);
 printf("  %c",T->data);
}
}
void exchange(BiTNode *T)
{   BiTNode *S;
    if (T) { S=T->lchild;
            T->lchild=T->rchild;
            T->rchild=S;
            exchange(T->lchild);
            exchange(T->rchild);
            printf("  %c",T->data);
 }
}
void main()
{BiTNode *T; int i=1,j;

while(i)
{printf("\n\n          ***************************\n");
printf("          *The system of the bitree *\n");
printf("          *-------------------------*\n");
printf("          *      (0) creat          *\n");
printf("          *      (1) preorder       *\n");
printf("          *      (2) inordert       *\n");
printf("          *      (3) postorder      *\n");
printf("          *      (4) exchange       *\n");
printf("          *      (5) exit           *\n");
printf("          ***************************\n");
printf(" choose one operation  from (0->5): ");
scanf("%d",&j);getchar();
switch(j)
{case 0:{clrscr();
         printf("\nplease input the tree node:");
         create(&T); break;}
 case 1:{clrscr();
         printf("\nThe preorder of the tree is:\n");
         preorder(T);break;}
 case 2: {clrscr();
          printf("\nThe inorder of the tree is:\n");
          inorder(T);  break;}
 case 3: {clrscr();
          printf("\nThe postorder of the tree is:\n");
          postorder(T); break; }
 case 4: {clrscr();
          printf("\nExchange the tree:\n\n");
          exchange(T);break;}
 case 5: {i=0;printf("Thank you for using!"); break; }
 default:{clrscr();printf("Error!\nPress the right number.");break;}
}
}
getch();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -