📄 bintree.cpp
字号:
#include "StdAfx.h"
#include ".\bintree.h"
#include <string>
#include <iomanip>
//#include <afx.h>
#include <fstream>
#include <iostream>
using namespace std;
/*BinTree::BinTree(void)
{
}*/
BinTree::~BinTree(void)
{
}
void BinTree::CreateBtree(BinNode* T)
{
char Name[30];
char Num[15];
int Score;
cout<<"请输入姓名:";
cin>>Name;
cout<<"请深入学号:";
cin>>Num;
cout<<"请输入成绩:";
cin>>Score;
T=new BinNode();
strcpy(T->stInfo.Name,Name);
strcpy(T->stInfo.Num,Num);
T->stInfo.Score=Score;
cout<<"是否继续创建节点:是:Y 否:N\n";
cin>>tag;
if(tag!='N')
{
CreateBtree(T->lChild);
CreateBtree(T->rChild);
}
else
{
cout<<"一个节点已经创建完\n";
}
}
void BinTree::EditInfo()
{
BinNode* edtNode=Find(root);
cout<<"please input the Name:\n";
cin>>edtNode->stInfo.Name;
cout<<"please enter the Number:\n";
cin>>edtNode->stInfo.Num;
cout<<"please write the Score:\n";
cin>>edtNode->stInfo.Score;
}
BinNode* BinTree::Find(BinNode* R)
{
BinNode* reNode;
BinNode *p=R;
int base;
cout<<"请输入您要根据什么查找:";
cout<<"1:学号 2: 姓名\n";
cin>>base;
if(base==1)
{
char Num[15];
cout<<"请输入学号\n";
cin>>Num;
if(strcmp(p->stInfo.Num,Num)==0)
reNode=p;
else
{
Find(p->lChild);
Find(p->rChild);
}
}
else if(base==2)
{
char Name[30];
cout<<"请输入姓名:\n";
cin>>Name;
if(strcmp(p->stInfo.Name,Name)==0)
reNode=p;
else
{
Find(p->lChild);
Find(p->rChild);
}
}
else
cout<<"The selection you hava made is valid \n";
Show(reNode);
return reNode;
}
bool BinTree::Insert(StuInfo st,BinNode* T)
{
if(T==NULL)
{
T=new BinNode(st);
if(T==NULL)
cout<<"the memory is not Distributed properly";
}
else if(st.Score<T->stInfo.Score)
Insert(st,T->lChild);
else if(st.Score>T->stInfo.Score)
Insert(st,T->rChild);
return true;
}
void BinTree::Show(BinNode *T)
{
cout<<"查找结果:\n";
cout<<"姓名"<<setw(4)<< "学号"<<setw(4)<< "成绩\n";
cout<<T->stInfo.Name<<setw(4);
cout<<T->stInfo.Num<<setw(4);
cout<<setw(4)<<T->stInfo.Score<<endl;
}
void BinTree::preOrder(BinNode * T) //前序遍历
{
if(T !=NULL)
{
Show(T);
preOrder(T->lChild);
preOrder(T->rChild);
}
}
void BinTree::inOrder(BinNode *T)
{
if(T !=NULL)
{
preOrder(T->lChild);
Show(T);
preOrder(T->rChild);
}
}
void BinTree::postOrder(BinNode *T)
{
if(T !=NULL)
{
preOrder(T->lChild);
preOrder(T->rChild);
Show(T);
}
}
int BinTree::Leave(BinNode* t)
{
if(NULL==t)
return 0; //空树返回0
else if((NULL==t->lChild )&& (NULL==t->rChild))
return 1; //就一个根节点
else
return (Leave(t->lChild)+Leave(t->rChild)); //递归
}
int BinTree::Size(BinNode *t)
{
if(NULL==t)
return 0;
else
return 1+Size(t->lChild)+Size(t->rChild);
}
int BinTree::Depth(BinNode* t)
{
if(NULL==t)
return -1;
else if ((NULL==t->lChild) && (NULL==t->rChild))
return 0;
else
return Depth(t->lChild)>Depth(t->rChild) ? Depth(t->lChild):Depth(t->rChild);
}
bool BinTree::Delete(BinNode *t)
{
if(NULL!=t)
delete t;
else
{
Delete(t->lChild);
Delete(t->rChild);
}
return true;
}
bool BinTree::SaveInfo(BinNode* t)
{
/* CFile myFile;
CFileException fileException;
char *fileName="D:\\MyCoderesource\\VC++\\BinaryTreeEXstInfo.txt";
if ( !myFile.Open( fileName, CFile::modeCreate | CFile::modeReadWrite, &fileException ) )
{
TRACE( "Can't open file %s, error = %u\n",fileName, fileException.m_cause );
}
else
{
if(t!=NULL)
{
myFile.Write(t->stInfo.Name,sizeof(t->stInfo.Name));
myFile.Write(t->stInfo.Num,sizeof(t->stInfo.Num));
myFile.Write((char*)(&(t->stInfo.Score)),sizeof(t->stInfo.Score));
int x=t->stInfo.Score;
//myFile.op
SaveInfo(t->lChild);
SaveInfo(t->rChild);
}
return true;
}*/
static ofstream ofs;
ofs.open("stuInfo.txt",ios_base::out);
if(t!=NULL)
{
ofs.write(t->stInfo.Name,sizeof(t->stInfo.Name));
ofs.write(t->stInfo.Num,sizeof(t->stInfo.Num));
ofs<<t->stInfo.Score;
SaveInfo(t->lChild);
SaveInfo(t->rChild);
}
else
return true;
}
void BinTree::Load(BinNode* t)
{
static ifstream ifs; //为了在每次递归调用中不又从文件开始读取
ifs.open("stuInfo.txt",ios_base::in);
//BinNode *bNode;
if(!ifs && t!=NULL)
{
ifs.read(t->stInfo.Name,sizeof(t->stInfo.Name));
ifs.read(t->stInfo.Num,sizeof(t->stInfo.Num));
ifs>>t->stInfo.Score;
Load(t->lChild);
Load(t->rChild);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -