codeman_Ean 2021-03-16 16:39 采纳率: 66.7%
浏览 43
已采纳

C/C++上机题,求救呀

  • 写回答

1条回答 默认 最新

  • 随缘|为而不争 2021-03-17 17:18
    关注

     Linux环境下编译的,试过没有问题。

    #include <stdio.h>
    #include <stdlib.h>
    
    //ABC^^DE^G^^F^^^
    
    struct bi_node{
    	char data;
    	struct bi_node *lchild;
    	struct bi_node *rchild;
    };
    
    int create_bi_node(struct bi_node **node)
    {
    	char ch;
    	int ret = 0;
    	struct bi_node *temp_node;
    
    	*node = NULL;
    	scanf("%c", &ch);
    	//判断节点是否空
    	if(ch == '^'){
    		return 0;
    	}
    	//分配节点内存
    	temp_node = malloc(sizeof(struct bi_node));
    	if(!temp_node)
    		return -1;
    		
    	temp_node->data = ch;
    
    	ret = create_bi_node(&temp_node->lchild);//左节点
    	ret |= create_bi_node(&temp_node->rchild);//右节点
    	
    	*node = temp_node;
    	return ret;
    }
    
    void print_pre_order(struct bi_node *node){
    	
    	if(!node)
    		return;
    	printf("%c", node->data);
    	print_pre_order(node->lchild);
    	print_pre_order(node->rchild);
    }
    
    void print_in_order(struct bi_node *node){
    	
    	if(!node)
    		return;
    
    	print_in_order(node->lchild);
    	printf("%c", node->data);
    	print_in_order(node->rchild);
    }
    
    void print_post_order(struct bi_node *node){
    	
    	if(!node)
    		return;
    
    	print_post_order(node->lchild);
    	print_post_order(node->rchild);
    	printf("%c", node->data);
    }
    
    int main(int argc, char *argv[])
    {
    	struct bi_node *bi_tree_node = NULL;
    	
    	if(create_bi_node(&bi_tree_node) ==0)
    	{
    		printf("\nPre Order:");
    		print_pre_order(bi_tree_node);
    		printf("\nIn Order:");
    		print_in_order(bi_tree_node);
    		printf("\nPost Order:");
    		print_post_order(bi_tree_node);
    		printf("\n");
    		printf("Create biTree Sucess!\n\n");
    	}else{
    		printf("Create biTree error!\n");
    	}
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥15 请问Lammps做复合材料拉伸模拟,应力应变曲线问题
  • ¥30 python代码,帮调试
  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元