tree3.c

来自「这是我学习数据结构时用过的程序。索性打包传上去。数据结构实验用。」· C语言 代码 · 共 96 行

C
96
字号
#include <stdlib.h>
#include <stdio.h>
#define queuesize 100
#define null 0
typedef char datatype;
typedef struct node{
    datatype data;
    struct node *lchild,*rchild;
  }bintnode;
typedef bintnode *bintree;


void createbintree(bintree *t)
  {
    char ch;
    ch=getchar();
    if (ch==' ')
	  *t=null;
    else{
	  *t=(bintnode*)malloc(sizeof(bintnode));
	  (*t)->data=ch;
	  createbintree(&((*t)->lchild));
	  createbintree(&((*t)->rchild));
	}
    return;
  }

void preorder(bintree t)
  {
    if (t){
      printf("%c",t->data);
      preorder(t->lchild);
      preorder(t->rchild);
    }
  }

void inorder(bintree t)
  {
      if (t){
	      inorder(t->lchild);
	      printf("%c",t->data);
	      inorder(t->rchild);
	    }
  }

void postorder(bintree t)
  {
    if (t){
	    postorder(t->lchild);
	    postorder(t->rchild);
	    printf("%c",t->data);
	  }
  }
void print(bintree  t)
  {
    if (t!=null)
    {
       printf("%c",t->data);
       if (t->lchild!=null || t->rchild!=null)
       {
	  printf("(");
	  print(t->lchild);
	  if (t->rchild!=null) printf(",");
	  print(t->rchild);
	  printf(")");
       }
    }
  }
error(char *message)
  {
      fprintf(stderr,"error:%s\n",message);
      exit(1);
  }

main()
  {

    bintree t=null;
    //int n;
    printf("\nplease input nodes of bintree:\n");
    createbintree(&t);

    printf("the preorder is:\n");
    preorder(t);
    printf("\n");

    printf("\nthe inorder is:\n");
    inorder(t);

    printf("\nthe postorder is:\n");
    postorder(t);

    printf("\nthe bintree is:\n");
    print(t);
  }

⌨️ 快捷键说明

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