



[size=24px][size=13px]
H:\数据结构与算法课程实习\基础部分框架--3\P5.CPP In function 'BiTNode* init1(BiTNode*)':[/size][/size]
157 28 H:\数据结构与算法课程实习\基础部分框架--3\P5.CPP [Error] invalid conversion from 'int' to 'int*' [-fpermissive]
91 5 H:\数据结构与算法课程实习\基础部分框架--3\P5.CPP [Note] initializing argument 1 of 'int init0(int*)'
/*
源文件名:P5.cpp
功能:二叉树操作
1、实现二叉树的创建;
2、用递归方法分别先序、中序、后序遍历以Tree为根指针的二叉树;
3、编写递归算法,计算二叉树中叶子结点的数目;
4、编写递归算法,计算二叉树的深度;
5、编写递归算法,将二叉树中所有结点的左、右子树相互交换;
6、使用数组elem中的随机数序列(以0表示结束,不包括0),生成以Tree为根指针的二叉排序树;
7、在以Tree为根指针的二叉排序树中查找结点;
8、从以Tree为根指针的二叉排序树中删除结点(适用各种位置的结点);
9、用非递归算法,先序遍历以Tree为根指针的二叉树;
提示:用数组 BiTNode *stack[max] 构成堆栈,利用这个堆栈实现功能。
10、对以Tree为根指针的二叉树,从根结点开始,逐层从左到右输出各结点的数据。
*/
//#include <iostream.h>
//#include <iomanip.h>
#include <conio.h>
#include <stdio.h>
#include <process.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
using namespace std;
struct BiTNode //二叉树结点类型,定义二叉树左右指针结构体
{
int data; //数据
int tem1,tem2; //辅助数据(实习题不用)
BiTNode *left; //左子树指针
BiTNode *right; //右子树指针
};
BiTNode MYTREE;
BiTNode *Tree; //本程序操作的二叉树 根指针!!!
const int max=100;
int elem[max]; //存放原始数据
int init0(int list[]);
void showBinTree(BiTNode *Tree);
BiTNode *init1(BiTNode *Tree); //该指针函数从键盘输入个数和随机数种子,以Tree为根指针,生成指定结点个数的二叉树,供其他程序使用
void wait(){
printf("\n请按任意键继续\n") ;
getch();
}
int main()
{
int list[max]={};//我看别人是不需要这一个数组定义和初始化的,只用到elem数组的初始化
//可能是因为我在选择程序中有用到这个参数吧
Tree = &MYTREE;
MYTREE.left=NULL;
MYTREE.right=NULL;
char choice;
while (1)
{
system("cls");
printf( "\n\n\n\n");
printf( "\t\t 二叉树操作 \n");
printf( "\t\t======================================");
printf( "\n\n");
printf( "\t\t 1:初始化 \n");
printf( "\t\t 2:显示 \n");
printf( "\t\t 0:退出 \n");
printf( "\n");
printf( "\t\t请选择:" );
choice = getch();
system("cls");
switch(choice)
{
case '1':
init0(list);
break;
case '2':
showBinTree(Tree);
break;
case '0':
exit(0);
}
}
}
void BENULL(BiTNode *T) //用来把儿子指向变成null,这个函数在前面就没有声明
{
T->left=NULL;
T->right=NULL;
}
int init0(int list[]){//先序创建二叉树,函数从键盘输入个数和随机数种子,在数组elem中生成指定个数的数据,
//供其他程序使用!!!,0表示数据结束
int n;
// BiTNode *p,*q;
while (1)
{
printf("输入元素个数(0- 100 ):" );
scanf("%d",&n);
if (n >= 0 && n <= 100)
break;
printf("\n");
}
int i,value;
while (1)
{
printf( "输入随机数种子(0-32767):" );
scanf("%d",&i);
if (i >= 0 && i <= 32767)
break;
printf("\n");
}
/* //从线性表中删除并释放原有的结点,使其成为空表,不是链表不需要这几步操作
p=DLList;
while (p->next!=NULL)
{
q=p->next;
p->next=q->next;
free(q);
}*/
srand(i); //指定随机数种子,相同的种子将产生相同的数据序列
rand();
for (i = 0; i < n; i++)
{
list[i] = rand() % 999 +1;
return list[i];
}
/*//向线性表插入length个新结点,不是链表不需要这几步操作
//但貌似和此次二叉树的步骤有相似之处
for (int j=1;j<=length;j++)
{
p=new Node;
p->next=DLList->next;
DLList->next=p;
p->data=rand() % 10000;
}*/
/*else{//这是我从网上找来的生成二叉树的步骤
T=new BiTNode;
T->data=ch;
CreateBiTree(T->left);
CreateBiTree(T->right);
} */
}
/* T=new BiTNode;
T->data=list[i];
init1(T->left);
init1(T->right);*/
BiTNode *init1(BiTNode *Tree){
int value;
BiTNode *T=new BiTNode;
T->data=init0(value);
init1(T->left);
init1(T->right);
}
void Preorder(BiTNode *T)//先序输出
{
if(T)
{
if(T->data==NULL);
else{printf("%d ",T->data);} /*访问结点*/
Preorder(T->left);
Preorder(T->right);
}
}
void Postorder(BiTNode *T)//中序输出
{
if(T)
{
Postorder(T->left);
Postorder(T->right);
if(T->data==NULL);
else{printf("%d ",T->data);} /*访问结点*/
}
}
void Inorder(BiTNode *T)//后序输出
{
if(T)
{
Inorder(T->left);
if(T->data==NULL);
else{printf("%d ",T->data);} /*访问结点*/
Inorder(T->right);
}
}
void show_in_order(BiTNode *T)
{
printf("\n先序遍历:");
Preorder(T);
printf("\n中序遍历:");
Inorder(T);
printf("\n后序遍历:");
Postorder(T);
wait();//wait()会暂时停止进程的执行,直到有信号来到或子进程结束。
//如果在调用wait()时子进程已经结束,则wait()会立即返回子进程结束状态值。
}
/*BiTNode *Generate(int n)
{
int nl;
if (n==0)
return NULL;
BiTNode *P = new BiTNode;
P->data = rand() % 999 +1;
nl=rand() % (n);
P->left = Generate(nl);
P->right = Generate(n-1-nl);//这个递归不是很懂什么意思
return P;
}
void generate(BiTNode *Tree)// 测试用,树的生成
{
printf("rrrrrrrrrrrrrrrrrrrrrrrrrrrr");
BiTNode *t;
t= Tree;
t->data = 11;
t->left =(BiTNode*) malloc(sizeof(BiTNode));
t->left->data=22;
BENULL(t->left);
t->right=(BiTNode*) malloc(sizeof(BiTNode));
t->right->data = 33;
BENULL( t->right);
t->left->left =(BiTNode*) malloc(sizeof(BiTNode));
t->left->left->data=44;
BENULL(t->left->left);
t->right->left=(BiTNode*) malloc(sizeof(BiTNode));
t->right->left->data=55;
BENULL(t->right->left);
t->right->right=(BiTNode*) malloc(sizeof(BiTNode));
t->right->right->data=66;
BENULL(t->right->right);
wait();
}
*/
//void showBinTree(BiTNode *Tree){//二叉树的展示 ,用递归方法分别先序、中序、后序遍历以Tree为根指针的二叉树;
//以下函数在本程序所在的位置生成文本文件Map.txt,其中显示以Tree为根指针的二叉树
//}
//#include "BinT.h"// 这个是干什么的 ? 如不注释掉这个就会自己另外生成一个小文件,并且提示我某函数重复定义
//本实验的各种要求:
//先序、中序、后序遍历以Tree为根指针的二叉树
//使用数组elem中的随机数序列(以0表示结束,不包括0),生成以Tree为根指针的二叉排序树
//在以Tree为根指针的二叉排序树中查找结点
//从以Tree为根指针的二叉排序树中删除结点(适用各种位置的结点)
//对以Tree为根指针的二叉树,从根结点开始,逐层从左到右输出各结点的数据
//提示:用数组 BiTNode *queue[max] 构成队列,利用这个队列实现功能
//*1、随机生成二叉树。 2、生成并保存先(后)序、中序输出序列。 3、按照保存的一对输出序列恢复出二叉树。 4、生成先(后)序、中序输出序列。 5、比较两对输出序列。
//*不用递归,先序、中序、后序遍历以Tree为根指针的二叉树
//提示:用数组 BiTNode *stack[max] 构成堆栈,利用这个堆栈实现功能
//*用缩入格式的多行文本表示以Tree为根指针的二叉树,例如:
// 324
// 123
// 746
// 690
// 567
//*用广义表表示以Tree为根指针的二叉树,例如
// (324(123(746()())(690()()))(567()()))
//*使用数组elem中的随机数序列(以0表示结束,不包括0),生成赫夫曼树,计算平均带权路径长度