二叉排序树(面试四)
2016-06-13 19:57
330 查看
/*
面试(四)
开发环境:Visual Studio 2008
开发语言:C语言
要 求:
下列程序中的TreeSort函数功能:对任意已存在的二叉树进行排序后生成一个新的二叉排序树。 优化TreeSort函数:实现对任意已存的二叉树进行排序(不允许在其过程中生成新树)。
时 间:15-20分钟
得分标准:
考核标准: 口述算法思路不清晰者(0分)
考核小组:张一涛
批注评语:
面试(四)
开发环境:Visual Studio 2008
开发语言:C语言
要 求:
下列程序中的TreeSort函数功能:对任意已存在的二叉树进行排序后生成一个新的二叉排序树。 优化TreeSort函数:实现对任意已存的二叉树进行排序(不允许在其过程中生成新树)。
时 间:15-20分钟
得分标准:
考核标准: 口述算法思路不清晰者(0分)
考核小组:张一涛
批注评语:
#include <stdio.h> #include <stdlib.h> typedef struct BTree { char data; struct BTree *Lchild,*Rchild; }Tree; char data[100]; int k=0; Tree *CreateTree(Tree *Root,char *str) { Tree *s[100],*p; int i=0,k=0,top=0; Root=NULL; p=NULL; for(i=0;str[i]!='\0';i++) { if(str[i]=='(') { s[top++]=p;k=1;} else if(str[i]==')') top--; else if(str[i]==',') k=2; else { p=(Tree *)malloc(sizeof(Tree)); p->data=str[i]; p->Lchild=NULL; p->Rchild=NULL; if(Root==NULL) Root=p; else { if(k==1) s[top-1]->Lchild=p; else s[top-1]->Rchild=p; } } } return Root; } void InOrder(Tree *r) { Tree *s[100],*p=r; int top=-1; while(p!=NULL||top!=-1) { while(p!=NULL) { s[++top]=p; p=p->Lchild; } if(top!=-1) { p=s[top]; printf("%c ",p->data); top--; p=p->Rchild; } } } void FreeTree(Tree *R) { if(R!=NULL) { FreeTree(R->Lchild); FreeTree(R->Rchild); free(R); } } void OutputByLayer(Tree *R) { Tree *Stack[100]; int cur =0,last = 0,tmp; int high=0; if(R==NULL) return ; Stack[last++]=R; while(cur < last) { tmp=last; while(cur < tmp) { printf("%c ",Stack[cur]->data); data[k++]=Stack[cur]->data; if(Stack[cur]->Lchild!=NULL) Stack[last++]=Stack[cur]->Lchild; if(Stack[cur]->Rchild!=NULL) Stack[last++]=Stack[cur]->Rchild; ++cur; } printf("\n"); high++; } printf("The Tree is Node:%d\n",last); printf("The Tree is high:%d\n",high); data[k]='\0'; } Tree *TreeSort(Tree *R) { Tree *p,*q,*t; int i; int flag; if(k!=0||R==NULL) { t=(Tree *)malloc(sizeof(Tree)); t->data=data[0]; t->Lchild=NULL; t->Rchild=NULL; R=t; } for(i=1;i<k;i++) { t=(Tree *)malloc(sizeof(Tree)); t->data=data[i]; t->Lchild=NULL; t->Rchild=NULL; p=R; q=R; while(p!=NULL) { q=p; if(t->data>=p->data) { flag=1; p=p->Rchild; continue; } if(t->data<p->data) { flag=0; p=p->Lchild; } } if(flag==0) q->Lchild=t; else q->Rchild=t; } return R; } void main() { Tree *Root=NULL; Tree *NewRoot=NULL; printf("a(b(c,d(1,2)),e(,f(1,3)))\n"); Root=CreateTree(Root,"a(b(c,d(1,2)),e(,f(1,3)))"); printf("\nOutput By Layer:\n"); OutputByLayer(Root); printf("\n"); printf("\n**********Binary-Sort-Tree**********\n"); printf("LayerTree:\t%s\n",data); NewRoot=TreeSort(NewRoot); printf("OrderTree:\t"); InOrder(NewRoot); printf("\n"); printf("\nFreeTree!\n"); FreeTree(Root); FreeTree(NewRoot); }
相关文章推荐
- 面试总结之指针
- 面试题44:扑克牌的顺子
- Android面试题积累二
- 一位软件工程师的7年总结
- 两天三场Java实习生面试总结
- 剑指offer面试题-求整数二进制中1的个数
- leetcode 131. Palindrome Partitioning-回溯算法
- 面试题11 :O(1)删除单链表节点
- 看程序员是如何把自动化做到极致的![转载]
- 程序员面试金典(三)--数组和字符串
- JAVA实现链表面试题
- 面试纪要
- java并发编程(二十四)--面试题合集
- 程序员如何花式赚外快?
- 约瑟夫环问题的链表实现
- 十个程序员必备的网站推荐
- 奋斗吧,程序员——第五十四章 坐拥美人君莫笑,古来征战几人回
- hive面试题目:表大概有2T左右,对表数据转换
- 面试题10 二进制中1的个数
- 剑指offer---面试题6重建二叉树