📄 sorttree.cpp
字号:
// SortTree.cpp : Defines the entry point for the console application.
//
/**--------------------------------------------------------------------
功能:这是一个控制台程序,实现一个异质的排序二叉树,每个节点可以是人,老师,学生
作者:陈海 GS0721526,参考唐发根老师《数据结构》二叉树、排序二叉树有关章节,在此表示感谢
日期:2007-11
------------------------------------------------------------------------*/
#include "stdafx.h"
#include<afx.h>
void print_init_data(char* [][2] ,int,int);
/*----------------------------------------------------
main function
------------------------------------------------------*/
void main(int argc, char* argv[])
{
char *professor_data[PROFESSOR_ROWS][PROFESSOR_COLS]={ //定义 老师,包含ID,名字
{"12", "shenxueping" }, //ID,name
{"11", "yuancangzhou" },
{"13", "yaoshuzhen" },
{"15","professor15"},
{"20","Professor20"},
{"18","professor18"},
{"22","professor22"},
{"23","professor23"},
{"19","professor19"},
{"21","professor21"}
};
char *student_data[STUDENT_ROWS][STUDENT_COLS]={ //定义 学生,包含ID,名字 ;ID跟在老师 不重复
{"06", "chenhai" }, //ID,name
{"04", "zhangshuang" },
{"05", "liupeng", },
{"15","student15"},
{"00","student00"},
{"08","student08"},
{"02","student02"},
{"07","student07"},
{"06","student06"},
{"10","student10"}
};
cout<< "\nBefore sorting:" ;
cout<< "\n==========================================================\n" ;
print_init_data( professor_data ,PROFESSOR_ROWS,PROFESSOR_COLS); //打印教授初始数据
print_init_data( student_data ,STUDENT_ROWS,STUDENT_COLS); //打印学生初始数据
//将老师信息读入老师对象,并按ID排序的,插入树
//将学生信息读入老师对象,并按ID排序的,插入树
cout<< "\nBinary Sort Tree Begins,CopyRigth@2007 GS0721526陈海!" ;
cout<< "\n==========================================================\n" ;
BinarySortTree* tree=new BinarySortTree(); //使用tree对象来管理树
Node* root = NULL; //把第一个节点作为根记住
//创建树,并设定数根指针
for(int iProfessor=0;iProfessor<PROFESSOR_ROWS;iProfessor++){
Professor* professor=new Professor();
professor->setId( professor_data[iProfessor][0] );
professor->setName( professor_data[iProfessor][1]);
Node* node=new Node();
node->person = professor;
node->lchild = NULL;
node->rchild = NULL; //这里的左右指针必须置位空,方便树的判断
if(node != NULL )
root=tree->generateTree(root,node);
}
//创建树,并设定数根指针
for(int iStudent=0;iStudent<STUDENT_ROWS;iStudent++){
Student* student=new Student();
student->setId( student_data[iStudent][0] );
student->setName( student_data[iStudent][1]);
Node* node=new Node();
node->person = student;
node->lchild = NULL;
node->rchild = NULL; //这里的左右指针必须置位空,方便树的判断
if(node != NULL )
root=tree->generateTree(root,node);
}
cout<<"========================================="<<endl;
cout<<"The tree after sorting(depth:"<<tree->getDepth(root)<<"):"<<endl;
cout<<"========================"<<endl ;
if(root != NULL )
tree->listTree( root ); //打印排序树
root=tree->deleteNode(root,"20"); //删除一个节点
cout<<"The tree after deleting 20(depth:"<<tree->getDepth(root)<<"):"<<endl;
cout<<"========================"<<endl ;
if(root != NULL )
tree->listTree( root ); //删除后打印 排序树
cout<<"========================================="<<endl;
cout<<"end of deleting display"<<endl;
cout<<"========================================="<<endl;
cout<<"Destroying the tree..."<<endl;
cout<<"======================="<<endl;
if(root != NULL )
root = tree->destroyTree(root); //销毁树,释放资源
cout<<"========================================="<<endl;
cout<<"CopyRight@ GS0721526陈海"<<endl;
cout<<"=============================="<<endl;
}
/**---------------------------------
打印初始数据
-------------------------------------*/
void print_init_data(char* person[][2] ,int rows,int cols ){
for(int i=0; i<rows;i++){
for (int j=0; j<cols;j++){
cout<< person[i][j] <<",";
}
cout<<endl;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -