二叉树的存储结构

来源:互联网 发布:如何预防网络电信诈骗 编辑:程序博客网 时间:2024/06/06 11:37

1.顺式存储结构 :
      

          数据元素结构的定义如下 :
         typedef struct node{
             DataType data;   
             int lchildIndex;
             int rchildIndex; 
          }BitreeNode;


此操作代码略去;


2.链式存储结构 :

   代码如下 
   
#include <stdio.h>#include <stdlib.h>typedef char DataType;//数据元素结构typedef struct node{   DataType data;   struct node * lchild;   struct node * rchild; }BitTreeNode;//初始化根节点void initate(BitTreeNode ** root,DataType e){   *root = (BitTreeNode *)malloc(sizeof(BitTreeNode));   (*root)->data  = e;   (*root)->lchild = NULL;   (*root)->rchild = NULL; }//插入左结点BitTreeNode * insertLeftChild(BitTreeNode * curr,DataType e){    if(curr == NULL) return NULL;        BitTreeNode *node = (BitTreeNode *)malloc(sizeof(BitTreeNode));    node->data = e;    node->lchild = curr->lchild;    node->rchild = NULL;    curr->lchild = node;    return node;}//插入右结点BitTreeNode * insertRightChild(BitTreeNode * curr,DataType e){    if(curr == NULL) return NULL;        BitTreeNode *node = (BitTreeNode *)malloc(sizeof(BitTreeNode));    node->data = e;    node->rchild = curr->rchild;    node->lchild = NULL;    curr->rchild = node;    return node;}
/** 销毁树**/void destroyTree(BitTreeNode *node){     if(node == NULL) return;    if(node->lchild == NULL && node->rchild == NULL)     free(node);    //left first to destroy;    destroyTree(node->lchild);    destroyTree(node->rchild);    //destroy self    free(node);}
//delete left childBitTreeNode * deleteLeftChild(BitTreeNode * curr){     if(curr == NULL) return NULL;     if(curr->lchild == NULL) return curr;     //delete      
destroyTree(curr->lchild);
 
     return curr;
}






BitTreeNode * deleteRightChild(BitTreeNode * curr){
     if(curr == NULL) return NULL;
     if(curr->rchild == NULL) return curr;


     //delete 
     
destroyTree(curr->rchild);
     return curr;
 
}




/**
  前序输出
**/


void preOrder(BitTreeNode * node){
   if(node == NULL) return;


   printf("%c ",node->data);
   preOrder(node->lchild);
   preOrder(node->rchild);
}


/**
  中序输出
**/


void inOrder(BitTreeNode * node){
   if(node == NULL) return;


  
   inOrder(node->lchild);
    printf("%c ",node->data);
   inOrder(node->rchild);
}




/**
  后序输出
**/


void postOrder(BitTreeNode * node){
   if(node == NULL) return;


  
   postOrder(node->lchild);
   postOrder(node->rchild);
   printf("%c ",node->data);
}





/**
凹式输出二叉树的信息
**/void printBitTree(BitTreeNode * curr,int level){    //print current node    if(curr == NULL)     return;       //left child    printBitTree(curr->lchild,level+1);     int i;    for(i=0;i<level ; i++){    printf(" ");    }    printf("%c\n",curr->data);    printBitTree(curr->rchild,level+1);}//在二叉树查找元素BitTreeNode  * findElement(BitTreeNode * curr,DataType e){    BitTreeNode * node =  NULL;    if(curr== NULL)    return NULL;    //self    if(curr->data == e)     return curr;    //left    node = findElement(curr->lchild,e);    if(node != NULL)     return node;    node = findElement(curr->rchild,e);    if(node != NULL)     return node;         return node;  //huisu}//  int main(){    //initate a tree    BitTreeNode *root;    initate(&root,'a');    //insert    BitTreeNode * b =  insertLeftChild(root,'b');    BitTreeNode * c=   insertRightChild(root,'c');    BitTreeNode * d =  insertLeftChild(b,'d');    BitTreeNode * e =  insertRightChild(b,'e');    BitTreeNode * f=   insertLeftChild(c,'f');    BitTreeNode * g=   insertLeftChild(c,'g');            printf("preorder  :");    preOrder(root);    printf("\n");    printf("inorder   :");    inOrder(root);    printf("\n");    printf("podtorder :");    postOrder(root);    printf("\n");        printf("ao ru shi :\n");    printBitTree(root,1);         //find    if(findElement(root,'e')){      printf("e is the element of the tree\n");    }else{      printf("sorry e is not the element of the tree\n");    }      //destroy the tree     destroyTree(root);    printf("destroy succeed\n");    return 0;  }

原创粉丝点击