Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4730438
  • 博文数量: 930
  • 博客积分: 12070
  • 博客等级: 上将
  • 技术积分: 11448
  • 用 户 组: 普通用户
  • 注册时间: 2008-08-15 16:57
文章分类

全部博文(930)

文章存档

2011年(60)

2010年(220)

2009年(371)

2008年(279)

分类: C/C++

2009-06-23 09:44:19

#include <stdio.h>
#include <stdlib.h>

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define N 10
typedef struct BiTNode{
        char data;
        struct BiTNode *lchild,*rchild;
}BiTNode, *BiTree;

void CreateBiTree(BiTree *T, char ch)
{
  /*
        If the very tree is empty, then malloc space for it
        and initialize the children NULL
        and evaluate the data as ch
   */

  if(!*T)
  {
        *T = (BiTNode*)malloc(sizeof(BiTNode));
        (*T)->data = ch;
        (*T)->lchild = NULL;
        (*T)->rchild = NULL;
        return;
  }
  /*
        If the tree is not empty, then compare the tree node value
        with the very value, if the node is bigger, then initalize the
        left child, the other with the right child
   */

  else
  {
        if((*T)->data == ch)
          return;
        if((*T)->data > ch)
          CreateBiTree(&((*T)->lchild), ch);
        else
          CreateBiTree(&((*T)->rchild), ch);
  }
}

/*
  Print the value according with Previous Order
  is to print the value first, then left child, and last the right child
 */

void MidOrder(BiTree T)
{
  if(T)
  {
        MidOrder(T->lchild);
    printf("%c ", T->data);
        MidOrder(T->rchild);
  }
  return;
}

/*
  Initalize the Root with NULL, and there using the array
  to provide the values, and this can also get from an extern file
  and even get by inputing
 */

int main()
{
  BiTree T = NULL;
  char a[N] = {'5','2', '3','6', '8', '7', '4', '1', '0', '9'};
  int i = 0;
  for(; i < N; i++)
  {
        CreateBiTree(&T, a[i]);
  }
  MidOrder(T);
  printf("\n");
  system("PAUSE");    
  return 0;

}

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