当前位置:   article > 正文

二叉树编程模型_二期二叉树模型编程

二期二叉树模型编程


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

typedef struct _bitree
{
 char data;
 struct _bitree *lchild,*rchild;
 
}*BiTree,BiTNode;


BiTree bitree_create(BiTree tree);
void prev_display(BiTree tree);
void midl_display(BiTree tree);
void last_display(BiTree tree);

//建立二叉树
BiTree bitree_create(BiTree tree)
{
 char ch = getchar();
 if(ch == '#')
  tree = NULL;
 else
 {
  tree = (BiTNode *)malloc(sizeof(BiTNode));
  
  if(tree == NULL )
  {
   printf("creat error!\n");
   return;
  }
  
  tree->data = ch;
  
  tree->lchild = bitree_create(tree->lchild);
  
  tree->rchild = bitree_create(tree->rchild);
 }
 
 return tree;
}


//先序遍历
//中,左,右
void prev_display(BiTree tree)
{
 if(tree != NULL)
 {
  printf("%c ",tree->data);
  prev_display(tree->lchild);
  prev_display(tree->rchild);
 
 }
}

//中序遍历
//左,中,右
void midl_display(BiTree tree)
{
 if(tree != NULL)
 {
  prev_display(tree->lchild);
  
  printf("%c ",tree->data);
  
  prev_display(tree->rchild);
 
 }
}

//后序遍历
//左,右,中
void last_display(BiTree tree)
{
 if(tree != NULL)
 {
  prev_display(tree->lchild);
  
  prev_display(tree->rchild);
 
  printf("%c ",tree->data);
 }

}

//求深度

int main()
{
 BiTree tree = bitree_create(tree);
 
 prev_display(tree);
 printf("\n");
 midl_display(tree);
 printf("\n");
 last_display(tree);
 printf("\n");
 
 return 0;
}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/923230
推荐阅读
相关标签
  

闽ICP备14008679号