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

📄 tree3.c

📁 这是我学习数据结构时用过的程序。索性打包传上去。数据结构实验用。
💻 C
字号:
#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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -