×

二叉树的遍历例题 二叉树 数据结构

谁能找几道数据结构的二叉树的先,中,后遍历的题?数据结构 (c语言版)胡学纲 课后习题 答案谢谢了,大神帮忙啊

admin admin 发表于2022-06-28 00:08:38 浏览108 评论0

抢沙发发表评论

谁能找几道数据结构的二叉树的先,中,后遍历的题


下面我以一个题目来说明(我博客中的),至于算法,我相信,你的课本里面已经讲的很详细了。
题目描述输入二叉树的先序遍历序列和中序遍历序列,输出该二叉树的后序遍历序列。输入第一行输入二叉树的先序遍历序列;第二行输入二叉树的中序遍历序列。输出输出该二叉树的后序遍历序列。示例输入ABDCEFBDAECF示例输出DBEFCA

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48

#include 《iostream》
#include 《cstring》
#define MAX 50+3
using namespace std;
typedef char Elem_Type;
typedef struct BiTree
{
Elem_Type data;//数据
struct BiTree *Lchild;//左孩子
struct BiTree *Rchild;//右孩子
}BiTree; //要查找的元素 查找的地方 数组的长度
int Search_Num(Elem_Type num,Elem_Type *array,int len)
{
for(int i=0; i《len; i++)
if(array[i] == num)
return i;
//return -1;//没有找到
} //前序遍历 中序遍历 中序数组长度
BiTree *Resume_BiTree(Elem_Type *front,Elem_Type *center,int len)
{
if(len 《= 0)
return NULL;
BiTree *temp = new BiTree;
temp-》data = *front;
int index = Search_Num(*front,center,len);
temp-》Lchild = Resume_BiTree(front+1,center,index);
temp-》Rchild = Resume_BiTree(front+index+1,center+index+1,len-index-1);
return temp;
}
void PostOrderTraverse(BiTree *root)//后序遍历
{
if( root != NULL)
{

数据结构 (c语言版)胡学纲 课后习题 答案谢谢了,大神帮忙啊


数据结构课程第一章部分习题解答 第一章 绪论 1-4.什么是抽象数据类型?试用C++的类声明定义“复数”的抽象数据类型。要求 (1) 在复数内部用浮点数定义它的实部和虚部。 (2) 实现3个构造函数:缺省的构造函数没有参数;第二个构造函数将双精度浮点数赋给复数的实部,虚部置为0;第三个构造函数将两个双精度浮点数分别赋给复数的实部和虚部。 (3) 定义获取和修改复数的实部和虚部,以及+、-、*、/等运算的成员函数。 (4) 定义重载的流函数来输出一个复数。 【解答】 抽象数据类型通常是指由用户定义,用以表示应用问题的数据模型。抽象数据类型由基本的数据类型构成,并包括一组相关的服务。 //在头文件complex.h中定义的复数类 #ifndef _complex_h_ #define _complex_h_ #include class comlex { public: complex ( ){ Re = Im = 0; } //不带参数的构造函数 complex ( double r ) { Re = r; Im = 0; } //只置实部的构造函数 complex ( double r, double i ) { Re = r; Im = i; } //分别置实部、虚部的构造函数 double getReal ( ) { return Re; } //取复数实部 double getImag ( ) { return Im; } //取复数虚部 void setReal ( double r ) { Re = r; } //修改复数实部 void setImag ( double i ) { Im = i; } //修改复数虚部 complex & operator = ( complex & ob) { Re = ob.Re; Im = ob.Im; } //复数赋值 complex & operator + ( complex & ob ); //重载函数:复数四则运算 complex & operator – ( complex & ob ); complex & operator * ( complex & ob ); complex & operator / ( complex & ob ); friend ostream & operator 《《 ( ostream & os, complex & c ); //友元函数:重载《《 private: double Re, Im; //复数的实部与虚部 }; #endif //复数类complex的相关服务的实现放在C++源文件complex.cpp中 #include #include #include “complex.h” complex & complex :: operator + ( complex & ob ) { //重载函数:复数加法运算。 complex * result = new complex ( Re + ob.Re, Im + ob.Im ); return *result; } complex & complex :: operator – ( complex & ob ) { //重载函数:复数减法运算 complex *result = new complex ( Re – ob.Re, Im – ob.Im ); return * result; } complex & complex :: operator * ( complex & ob ) { //重载函数:复数乘法运算 complex *result = new complex ( Re * ob.Re – Im * ob.Im, Im * ob.Re + Re * ob.Im ); return *result; } complex & complex :: operator / ( complex & ) { //重载函数:复数除法 查看更多答案》》

数据结构(c语言版)题目求答案


3.28
void InitCiQueue(CiQueue&Q)//初始化循环链表表示的队列Q
{
Q=(CiLNode*)malloc(sizeof(CiLNode));
Q-》next=Q;
}//InitCiQueue
voidEnCiQueue(CiQueue&Q,int x)//把元素x插入循环列表表示的队列Q,Q指向队尾元素,Q-》next指向头结点,Q-》next-》next指向队尾元素
{
p=(CiLNode*)malloc(sizeof(CiLNode));
p-》data=x;
p-》next=Q-》next;//直接把p加在Q的后面
Q-》next=p;
Q=p;//修改尾指针
}
Status DeCiQueue(CiQueue&Q,int x)//从循环链表表示的队列Q头部删除元素x
{
if(Q==Q-》next)return INFEASIBLE;//队列已空
p=Q-》next-》next;
x=p-》data;
Q-》next-》next=p-》next;
free(p);
rturn OK;
}//DeCiqueue

3.31

int Palindrome_Test()
{
InitStack(S);InitQueue(Q);
while((c=getchar())!=’@’)
{
Push(S,c);EnQueue(Q,c);
}
while(!StackEmpty(S))
{
pop(S,a);DeQueue(Q,b);
if(a!=b)return ERROR;
}
return OK;
}
-二叉树