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");
    	}
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?