北大自考的一道上机题--做出来有分

asvaboy1980 2002-11-07 10:25:50
输入二叉树的先序序列和中序序列。由此创建一课二叉树,输出该二叉树的后序序列和该二叉树的高度??

有高手吗???谢谢
...全文
92 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
pgood 2002-11-11
  • 打赏
  • 举报
回复
树的高度在自考教材一是把根结点定义为1,而更多的书上是把根结点定义为0,如果想按自考教材的,应把最终返回值加1
asvaboy1980 2002-11-09
  • 打赏
  • 举报
回复
树的高度有问题
asvaboy1980 2002-11-08
  • 打赏
  • 举报
回复
说谁都会说,代码的实现,这里还有没有高手?????前天不是有人说自考的题很容易吗????怎么没有一个人能贴出代码????

晕倒,楼上的几个都是菜鸟~~~~~~分只给贴代码的人
bluefee 2002-11-08
  • 打赏
  • 举报
回复
这种题也要贴
FishCrazy 2002-11-08
  • 打赏
  • 举报
回复
呜~~~~~~~~~
来不及了,我也会,我也想要~~~~~~~
huanshilang 2002-11-08
  • 打赏
  • 举报
回复
厉害啊
pgood 2002-11-08
  • 打赏
  • 举报
回复
那分给我吧!
// createbin.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include"conio.h"
#include"malloc.h"
#include"string.h"
#define MAX(a,b) a>b?a:b

struct BINTREE
{
char ch;
struct BINTREE *left;
struct BINTREE *right;
};

BINTREE *CreateBinaryTree(char *szMidString,char *szBackString,int nLength)
{
if(nLength==0)
return NULL;
BINTREE *pRoot=(BINTREE *)malloc(sizeof (BINTREE));

char chTemp=szBackString[nLength-1];
pRoot->ch=chTemp;

int nPosition=strchr(szMidString,chTemp)-szMidString;
// if(nLength==1)
// {
// pRoot->left=NULL;
// pRoot->right=NULL;
// }
// else if(nPosition==nLength-1)
// pRoot->right=NULL;
// else if(nPosition==0)
// pRoot->left=NULL;
// else
// {
pRoot->left=CreateBinaryTree(szMidString,szBackString,nPosition);
pRoot->right=CreateBinaryTree(szMidString+nPosition+1,szBackString+nPosition,nLength-nPosition-1);
// }
return pRoot;
}
void DisplayFrontOrder(struct BINTREE *pRoot)
{
if(pRoot==NULL)
return;
printf("%c",pRoot->ch);
DisplayFrontOrder(pRoot->left);
DisplayFrontOrder(pRoot->right);
}

int GetLevel(struct BINTREE *pRoot)
{
if (pRoot == NULL)
return 0;
else
return MAX(GetLevel(pRoot->left), GetLevel(pRoot->right)) + 1;
}

void DeleteBinTree(struct BINTREE *pRoot)
{
if(pRoot==NULL)
return;
DeleteBinTree(pRoot->left);
DeleteBinTree(pRoot->right);
delete pRoot;
}
int main(int argc, char* argv[])
{
char szMidString[100],szBackString[100];
printf("please input Mid order:");
scanf("%s",szMidString);
printf("please input Back order:");
scanf("%s",szBackString);
struct BINTREE *pRoot=CreateBinaryTree(szMidString,szBackString,strlen(szMidString));

printf("this is the result of front order :\n");
DisplayFrontOrder(pRoot);
printf("\nthe level of binary tree is %d\n",GetLevel(pRoot));

DeleteBinTree(pRoot);

getch();
return 0;
}

===================================================
天下自考一家人(我花了1:40做出此题)
Crystal_arrow 2002-11-07
  • 打赏
  • 举报
回复
先序就是先根再左后右,中序就是先左中右,后序就是先左再右最后根

就按这种思路编就行!!!
merlinran 2002-11-07
  • 打赏
  • 举报
回复
笔试时出现这个题,你会做吗?好像是必考的题。
把想法提炼成算法,就成了。
xunknown 2002-11-07
  • 打赏
  • 举报
回复
找数据结构的书就有很多例子了啊。

70,023

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧