SDUT 2128 树结构练习——排序二叉树的中序遍历
2012-07-19 10:45
274 查看
题目链接
目测二叉堆就是用的排序二叉树。然后还恶搞一下AC代码。
恶搞版
目测二叉堆就是用的排序二叉树。然后还恶搞一下AC代码。
#include <stdio.h> #include <string.h> #include <stdlib.h> int num[1001]; int n,z; struct tree { int data; struct tree *left; struct tree *right; }; struct tree *build() { struct tree *p; p = (struct tree *)malloc(sizeof(struct tree)); p -> data = num[1]; p -> left = NULL; p ->right = NULL; return p; } void search(struct tree *head,int x) { struct tree *p; if(head -> data > x) { if(head -> left != NULL) search(head -> left,x); else { p = (struct tree *)malloc(sizeof(struct tree)); p -> data = x; p -> left = NULL; p ->right = NULL; head ->left = p; } } else { if(head -> right != NULL) search(head -> right,x); else { p = (struct tree *)malloc(sizeof(struct tree)); p -> data = x; p -> left = NULL; p ->right = NULL; head -> right = p; } } } void midshow(struct tree *k) { if(k) { midshow(k -> left); if(z) { printf("%d",k -> data); z = 0; } else printf(" %d",k ->data); midshow(k -> right); } else return ; } int main() { int i; struct tree *head; while(scanf("%d",&n)!=EOF) { z = 1; for(i = 1; i <= n; i ++) { scanf("%d",&num[i]); if(i == 1) head = build(); else search(head,num[i]); } midshow(head); printf("\n"); } return 0; }
恶搞版
#include <stdio.h> #include <stdlib.h> int p[1001]; int cmp(const void *a,const void *b) { return *(int *)a - *(int *)b; } int main() { int n,i; while(scanf("%d",&n)!=EOF) { for(i = 0; i <= n-1; i ++) scanf("%d",&p[i]); qsort(p,n,sizeof(p[0]),cmp); for(i = 0; i <= n-1; i ++) { if(i == 0) printf("%d",p[i]); else printf(" %d",p[i]); } printf("\n"); } return 0; }
相关文章推荐
- SDUT 2128 树结构练习——排序二叉树的中序遍历
- SDUT 2128 树结构练习——排序二叉树的中序遍历
- SDUT2128树结构练习——排序二叉树的中序遍历
- SDUT 2128 树结构练习——排序二叉树的中序遍历
- SDUT 2128 树结构练习 排序二叉树的中序遍历
- sdut 2128 树结构练习——排序二叉树(BST)的中序遍历
- 树结构练习——排序二叉树的中序遍历 SDUT
- 树结构练习——排序二叉树的中序遍历---2128
- SDUTOJ 2128 树结构练习——排序二叉树的中序遍历
- SDUTOJ(2128)树结构练习——排序二叉树的中序遍历
- SDUT-树结构练习——排序二叉树的中序遍历
- OJ2128树结构练习——排序二叉树的中序遍历
- SDUTOJ 2128 树结构练习——排序二叉树的中序遍历
- 树结构练习——排序二叉树的中序遍历(sdut_2128)
- 树结构练习——排序二叉树的中序遍历
- 数据结构之二叉树 树结构练习——排序二叉树的中序遍历 (排序建树+中序遍历)
- 树结构练习——排序二叉树的中序遍历
- 树结构练习——排序二叉树的中序遍历
- sdut 2128 排序二叉树的中序遍历
- 树结构练习——排序二叉树的中序遍历