基于树的排序,降序输出
来源:互联网 发布:免费域名跳转 编辑:程序博客网 时间:2024/06/04 18:28
#include <stdio.h>//#include <malloc.h>#include <stdlib.h>//#define MAXNUM 10;//error C2143: syntax error : missing ']' before ';//宏定义后不应有该分号;#define MAXNUM 10 //左、右节点的最大数目 /* // error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 前向引用typedef struct{TNode* left;TNode* right;int value;}TNode;*/ struct TNode{TNode* left;TNode* right;int value;};TNode* root=NULL;void append(int N);void PrintBiTreeMax(TNode*L);int main( ){append(63);append(45);append(32);append(77);append(96);append(21);append(17);PrintBiTreeMax(root);return 0;}void append(int N){TNode * NewNode=(TNode*)malloc(sizeof(TNode));NewNode->value=N;NewNode->left=NULL;// 要初始化啊NewNode->right=NULL;if (root==NULL){root=NewNode;return ;} else{TNode* temp;temp=root;while (N >= temp->value &&temp->left!=NULL ||N<temp->value &&temp->right!=NULL){while (N >= temp->value &&temp->left!=NULL){temp=temp->left;}while(N<temp->value &&temp->right!=NULL){temp=temp->right;}}if (N>=temp->value){temp->left=NewNode;} else{temp->right=NewNode;}return ;}}void PrintBiTreeMax(TNode * L){int left1[MAXNUM];int right1[MAXNUM];TNode* temp;int i;int lenLeft=0;int lenRight=0;//for(int i=0,temp=L->left;temp!=NULL;temp=temp->left){ //error C2227: left of '->value' must point to class/struct/union/generic type , // type is 'int' 重新定义的int i=0,temp=L->left 将使得temp为int 型for( i=0,temp=L->left;temp!=NULL;temp=temp->left){// left1[i++]=temp->value; lenLeft++;}for (i=0,temp=L->right;temp!=NULL;temp=temp->right){right1[i++]=temp->value;lenRight++;} for (int i=lenLeft-1;i>=0;i--) {printf("%6d",left1[i]); }printf("%6d",root->value);for (int i=0;i<lenRight;i++){printf("%6d",right1[i]);}}
debug 总结:
#define后不能加分号
//#define MAXNUM 10;//error C2143: syntax error : missing ']' before ';//宏定义后不应有该分号;
struct 变量分配完内存空间后,要记得初始化
TNode * NewNode=(TNode*)malloc(sizeof(TNode));NewNode->value=N;NewNode->left=NULL;// 要初始化啊NewNode->right=NULL;
否则下述比较总为TRUE
temp=root;
while (N >= temp->value &&temp->left!=NULL ||N<temp->value &&temp->right!=NULL){
for( int i,temp) 意味着temp重新被定义为int型,将覆盖原始的temp 引用
TNode* temp;int i;int lenLeft=0;int lenRight=0;//for(int i=0,temp=L->left;temp!=NULL;temp=temp->left){ //error C2227: left of '->value' must point to class/struct/union/generic type , // type is 'int' 重新定义的int i=0,temp=L->left 将使得temp为int 型for( i=0,temp=L->left;temp!=NULL;temp=temp->left){//输出结果
96 77 63 45 32 21 17
63(root)
77 45
96 32
21
17
0 0
- 基于树的排序,降序输出
- 输出大于3位数的降序数
- 实现对数组的降序排序
- java-Collections对象的降序排序--实例
- java-Collections对象的降序排序--实例
- 为NSString添加降序排序的category
- Dom-表格数据的排序,升序,降序
- hadoop的WordCount按照value降序排序
- unity3dC#的List升序降序排序
- 快速排序的升序和降序
- hdu 1285 基于入度数排序输出的拓扑排序
- 排序,block,降序排序
- 数组的排序(冒泡排序降序) 指针
- 合并两个输入的字符串按降序输出
- 超长数列中的n个整数排序,要求能够把从指定位置m开始的n个数排列成降序,并输出完整的数列
- ArrayList排序 降序排列
- Java数组降序排序
- 堆排序(降序)
- 伙伴们快去学习《代码大全》吧
- 读“程序员生存定律”笔记
- 使用VirtualBox安装Minix3
- Java设计模式菜鸟系列(七)命令模式建模与实现
- 数字电路设计之恢复余数除法器的verilog实现
- 基于树的排序,降序输出
- SDUTOJ 2116 数据结构实验之链表一:顺序建立链表
- JDBC和ODBC
- BZOJ 2115 Wc2011 Xor DFS+高斯消元
- 【无线互联】IOS开发之KYCircleMenu框架
- python学习第二章----列表list和元组tuple
- Acdream 1129 Strong Defence
- Extjs4.0 之Ext.Class 属性详解 (alias/mixins /uses/requires/singleton等属性)
- BASE64编码和解码(VC源代码)