把我的 C 作业贴出来 实验四 树——1
来源:互联网 发布:数据的预处理技术 编辑:程序博客网 时间:2024/05/21 08:58
实验四 树
一、 实验目的
1.熟悉二叉树的链式存储结构
2.掌握二叉树的建立、深度优先递归遍历等算法
3.能够利用遍历算法实现一些应用
二、实验内容
1.已知二叉树采用二叉链表存储结构,编写一个算法交换二叉树所有左、右子树的位置,即结点的左子树变为结点的右子树,右子树变为左子树。(文件夹:习题12_14)
//二叉链表的结构类型定义.h
const int maxsize=1024;
typedef char datatype;
typedef struct node
{
datatype data;
struct node *lchild,*rchild;
}bitree;
//二叉树的建立.h
bitree * creattree()
{
char ch;
bitree*Q[maxsize];
int front,rear;
bitree*root,*s;
root=NULL;
front=1;rear=0;
while((ch=getchar())!='#')
{
s=NULL;
if(ch!='@')
{
s=new bitree;
s->data=ch;
s->lchild=NULL;
s->rchild=NULL;
}
rear++;
Q[rear]=s;
if(rear==1)root=s;
else
{
if(s&&Q[front])
if(rear%2==0)Q[front]->lchild=s;
else Q[front]->rchild=s;
if(rear%2==1)front++;
}
}
return root;
}
//二叉树的输出.h
//按照先序序列输出
using std::cout;
void preorder(bitree*p)
{
if(p!=NULL)
{
cout<<p->data;
if(p->lchild!=NULL||p->rchild!=NULL)
{
cout<<"(";
preorder(p->lchild);
if(p->rchild!=NULL)cout<<",";
preorder(p->rchild);
cout<<")";
}
}
}
//交换左右子树.h
void swap( bitree * R )
{
bitree * p;
if( R == NULL )return;
p = R->lchild;
R->lchild = R->rchild;
R->rchild = p;
swap( R->lchild );
swap( R->rchild );
return;
}
//交换左右子树的主程序文件.cpp
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include"二叉链表的结构类型定义.h"
#include"二叉树的建立.h"
#include"二叉树的输出.h"
#include"交换左右子树.h"
using namespace std;
int main()
{
bitree*pb;
pb=creattree();
preorder(pb);
cout<<endl;
swap(pb);
preorder(pb);
cout<<endl;
system("PAUSE");
return 0;
}
- 把我的 C 作业贴出来 实验四 树——1
- 把我的 C 作业贴出来 实验四 树——2
- 把我的 C 作业贴出来 实验三 数组——1
- 把我的 C 作业贴出来 实验二 栈和队列 ——2
- 把我的 C 作业贴出来 实验三 数组——2
- 把我的 C 作业贴出来 实验一 线性表
- 把我的 C 作业贴出来 实验五 图
- 把我的 C 作业贴出来 实验二 栈和队列
- 实验四的实验作业
- 把我的js库(xiliuhu.js)贴出来,做个备份!
- 实验四课后作业
- 实验四——树和二叉树的实验1
- 【哈工大C作业实验】:13-1作业题
- C++实验2—作业
- c++第三次实验—作业
- C++实验四—分数的累加
- c++实验四—分数的累加
- [Java初学] 第一次作业 - 将输入的四位数分开打印出来
- 把我的 C 作业贴出来 实验二 栈和队列 ——2
- 把我的 C 作业贴出来 实验三 数组——1
- 走过的一年
- 用PHP制作的意见反馈表
- 把我的 C 作业贴出来 实验三 数组——2
- 把我的 C 作业贴出来 实验四 树——1
- [软工杂志]软件工程电子杂志讨论组——讨论eMag_SoftEng杂志的草创期的一切事宜
- 最新成果
- Visual C++ 编译器选项
- 雅可比(Jacobi)迭代算法的C++实现
- [ZT]大侠宝典 :ORACLE 常用第三方软件介绍
- 把我的 C 作业贴出来 实验四 树——2
- 单精度浮点数在vc++6.0中内存格式研究
- Java编写的计算器程序及源代码