简单的c语言链式二叉树

代码由四部分组成:

list.c

#include"head.h"
//创建一个二叉树
binary_tree  tree_create(void)
{
    char x;
    binary_tree L;
	scanf("%c",&x);
	if(x=='#')
	{
		L=NULL;
	}
	else
	{
      L=(binary_tree)malloc(sizeof(Lnode));
	  if(L==NULL)
	  {
         printf("L is  apply   failed\n");
		 exit(1);
      }
	  L->date=x;
	  //创建左二叉树
	  L->ltree=tree_create();
	  //创建右二叉树
	  L->rtree=tree_create();
	}
	return L;
}


//遍历根左右
void  tree_show(binary_tree  L)
{
     if(L!=NULL)
	 {
		 printf("%c ",L->date);
	     
		 tree_show(L->ltree);

		 tree_show(L->rtree);
     }

}

//遍历左根右
void tree_show1(binary_tree  L)
{
	if(L!=NULL)
	{
		tree_show1(L->ltree);
		printf("%c ",L->date);
		tree_show1(L->rtree);
    }

}

//遍历左右根
void tree_show2(binary_tree L)
{
	if(L!=NULL)
	{
		tree_show2(L->ltree);
		tree_show2(L->rtree);
		printf("%c ",L->date);
	}
}

head.h

#ifndef  __HEAD_H
#define  __HEAD_H
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef   char    datetype;
typedef   struct  tree{
      datetype date;
	  struct  tree *rchild;
	  struct  tree *lchild;
}Lnode,*stree;

stree  tree_create();
void   tree_show1(stree L);
void   tree_show2(stree L);
void   tree_show3(stree L);
#endif 

test.c

#include"head.h"
int main(int argc,char *argv[])
{
	stree  L;
	L=tree_create();
    if(L==NULL)
	{
		printf("L create is fail\n");
	    exit(1);
	}
	tree_show1(L);
	return 0;
}

Makefile

CC=gcc
CFLAGS=-c  -Wall  -O
objs=list.o  test.o
yuxiang:$(objs)
	$(CC)  -o  $@  $^
%*.o:%*.c
	$(CC)  $(CFLAGS) -c  -o  $@  $<
.PHONY:clean 
clean:
	rm -rf *.o  yuxiang 

输入数据的时候的结点没有左子树或者右子树使用的#表示

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值