Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2244114
  • 博文数量: 556
  • 博客积分: 11457
  • 博客等级: 上将
  • 技术积分: 5973
  • 用 户 组: 普通用户
  • 注册时间: 2011-02-24 22:33
文章分类

全部博文(556)

文章存档

2013年(22)

2012年(74)

2011年(460)

分类: C/C++

2012-07-24 14:43:01

tree.h

  1. tree.h
  2. #ifndef _TREE_H
  3. #define _TREE_H

  4. typedef struct node
  5. {
  6.     char data;
  7.     struct node * lchild,* rchild;

  8. }TREE;

  9. #endif

stack.h

点击(此处)折叠或打开

  1. #ifndef _STACK_H
  2. #define _STACK_H //避免重复定义 导入重复

  3. #include "tree.h"
  4. #define ElemType TREE *

  5. #define STACK_INIT_SIZE 10
  6. #define STACK_INCREME 10
  7.                  
  8. typedef struct
  9. {
  10.    ElemType * base; //为了实现通用,定义这样一个数据类型的指针
  11.    ElemType * top;
  12.    int size;
  13. }STACK;

  14. STACK * InitStack();
  15. void Destroy(STACK *s);
  16. int Push(STACK *s,ElemType *e);
  17. int Pop(STACK *s,ElemType *e);
  18. int IsEmpty(s);

  19. #endif

stack.c

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "stack.h"


  4. STACK * InitStack()
  5. {
  6.   STACK * s =(STACK *)malloc(sizeof(STACK));
  7.    if(s==NULL)
  8.        exit(0);
  9.    s->base=(ElemType *)malloc(STACK_INIT_SIZE * sizeof(ElemType));
  10.     if(s->base==NULL) exit(0);
  11.     s->top=s->base;
  12.     s->size=STACK_INIT_SIZE;
  13.     return s;
  14. }

  15. int Push(STACK *s,ElemType *e)
  16. {
  17.     if(s==NULL||e==NULL) //检查参数的合法性
  18.         return 0;
  19.     if(s->top -s ->base >=s->size)
  20.     {
  21.         s->base=(ElemType *)realloc(s->base,
  22.             (s->size +STACK_INCREME) * sizeof(ElemType));
  23.         if(s->base==NULL)
  24.             return 0;
  25.         s->top=s->base+s->size;
  26.         s->size=s->size+STACK_INCREME;
  27.     }
  28.     // *s->top=*e;
  29.     // s->top++;
  30.     *s->top++ = *e;
  31.     return 1;
  32. }

  33. int Pop(STACK *s,ElemType *e)
  34. {
  35.     if(s==NULL || e==NULL)
  36.         return 0;
  37.     if(s->top==s->base)
  38.         return 0;
  39.     *e =*--s->top;
  40.     return 1;
  41. }

  42. int IsEmpty(STACK *s)
  43. {
  44.     return s->top ==s->base ? 1: 0;
  45. }

  46. void Destroy(STACK *s)
  47. {
  48.     free(s->base);
  49.     free(s);
  50. }

tree.c

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<conio.h>
  3. #include<stdlib.h>
  4. #include "tree.h"
  5. #include "stack.h"

  6. TREE * MakeTree()
  7. {
  8.    TREE * t;
  9.    char ch;
  10.    ch=getche();
  11.    if(ch == '#')
  12.      return NULL;
  13.    t=(TREE *)malloc(sizeof(TREE));
  14.    if(t==NULL)
  15.      return NULL;
  16.    t->data=ch;
  17.    t->lchild = MakeTree();
  18.    t->rchild = MakeTree();
  19.    return t;
  20. }

  21. void PrintTreeByBefore(TREE *t)
  22. {
  23.    if(t == NULL)
  24.      return;
  25.    printf("[%c]",t->data);
  26.    PrintTreeByBefore(t->lchild);
  27.    PrintTreeByBefore(t->rchild);
  28. }

  29. void PrintTreeByMid(TREE *t)
  30. {
  31.     TREE *p = t;
  32.     STACK *s=InitStack();
  33.     Push(s,&t);
  34.     while(!IsEmpty(s))
  35.     {
  36.      while(p)
  37.         {
  38.            p=p->lchild;
  39.            Push(s,&p); //将左结点的空孩子结点压入了栈
  40.         }
  41.         Pop(s,&p); //先弹出的是空结点
  42.      if(!IsEmpty(s))
  43.         {
  44.            Pop(s,&p);
  45.            printf("[%c]",p->data);
  46.          p = p->rchild;
  47.            Push(s,&p);
  48.         }
  49.     }
  50.     Destroy(s);
  51. }
  52. void main()
  53. {
  54.    TREE *tree =MakeTree();
  55.     PrintTreeByBefore(tree);
  56.     printf("\n\n******************\n");
  57.     PrintTreeByMid(tree);

  58. }


学习文档:

 二叉树.rar   

辛苦整理,欢迎拍砖!

阅读(1085) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~