数据结构实验之分类二叉树的构建
#include <iostream.h>
#include <string.h>
typedef struct
{//定义Student结构类型,即传说中的EType
int num;
char name[10];
}Student;
Student student[100];//定义一个student数组
typedef struct TreeNode//二叉树结点
{
Student data;
TreeNode *LChild;
TreeNode *RChild;
}BinaryTreeNode;
char re_choose[]={"\n选择非法,请输入正确的编号...\n"};
void MakeBinaryTree(BinaryTreeNode *root,BinaryTreeNode *left,BinaryTreeNode *right)
{//构造一棵二叉树
root->LChild=left;
root->RChild=right;
}
BinaryTreeNode *MakeNode(Student &x)
{//构造节点
BinaryTreeNode *ptr;
ptr=new BinaryTreeNode;
if(!ptr) return NULL;
ptr->data=x;
ptr->LChild=NULL;
ptr->RChild=NULL;
return ptr;
}
BinaryTreeNode *SortBinaryTreeInsert (BinaryTreeNode *BT, Student &x)
{//求如果不重复出现,则插入结点x
BinaryTreeNode *p;
BinaryTreeNode *parent = NULL; //指向p的双亲
p=BT;
while (p)
{
parent = p;
if (x.num < p->data.num)
p=p->LChild;
else
if (x.num>p->data.num)
p = p->RChild;
else
return p; //重复出现,即相等值结点出现
}
// 找到插入点,为x申请一个空间填入其值,并将该结点连接至 parent
BinaryTreeNode *q = new BinaryTreeNode;
q ->data = x;
q->LChild=NULL;
q->RChild=NULL;
if (BT)
{// 原树非空
if (x.num < parent ->data.num)
parent ->LChild = q;
else
parent ->RChild = q;
}
else // 插入到空树中
BT = q;
return BT;
}
bool SortBinaryTreeSearch (BinaryTreeNode *BT, Student &x, int &Searchnum)
{//求查找关键字为Searchnum的结点值x
BinaryTreeNode *p = BT;
while (p)
if (Searchnum<p->data.num)
p = p->LChild;
else
if (Searchnum>p->data.num)
p = p->RChild;
else
{
x = p->data;
return true;
}
return false;
}
//删除分类二叉树中关键字为Searchnum的结点算法(SortBinaryTreeDelete)
bool SortBinaryTreeDelete (BinaryTreeNode *BT, int &Searchnum)
{// 删除关键字值为Searchnum 的结点
BinaryTreeNode *p=BT, *parent = NULL; // parent指向p的双亲
BinaryTreeNode *s, *ps; // ps指向s的双亲
while (p&&p->data.num!=Searchnum)
{// 查找删除结点
parent = p;
if (Searchnum < p->data.num)
p = p->LChild;
else
p = p->RChild;
}
if (!p)
return false; // 没有删除结点
// 对分类二叉树重构
if (p->LChild && p->RChild)
{// 被删除结点存在两个子树,在p的左子树中查找最大元素(最右子孙)
s = p->LChild;
ps = p;
while (s->RChild)
{// s推进到p的左子树中最大元素(最右子孙)
ps = s;
s = s->RChild;
}
p->data = s->data; //左子树中最大元素(最右子孙)值移到p
p = s;parent = ps; //p指向左子树中最大元素, parent指向s双亲
}
if (p->LChild) // p最多只有一个孩子
s = p->LChild;
else
s = p->RChild;
if (p == BT)
BT = s;
else
{// 判断p是parent左孩子,还是右孩子
if (p == parent->LChild)
parent->LChild = s;
else
parent->RChild = s;
}
delete p;
return true;
}
void InOrder(BinaryTreeNode *BT)
{//二叉树的中序遍历递归算法
if (BT)
{
InOrder(BT->LChild);
cout<<" "<<"学号: "<<BT->data.num<<'\t'<<"名字 "<<BT->data.name<<'\t'<<endl; //访问二叉树的结点
InOrder(BT->RChild);
}
}
void Producer_Information() //作者信息
{
cout<<" *************************************************"<<endl;
cout<<" 分类二叉树的构建"<<endl;
cout<<" **************************************************"<<endl;
}
void Menu() //菜单函数
{
cout <<"\n\t\t"<<"请选择以下一个功能:"<<endl;
cout <<"\n\t\t"<<"1.显示二叉树的结构和分类二叉树的构建结果."<<endl;
cout <<"\t\t2.分类二叉树中查找关键字." << endl;
cout <<"\t\t3.分类二叉树中删除关键字."<<endl;
cout <<"\t\t0.退出.\n"<<endl;
}
int main()
{
int i,a,Searchnum;;
BinaryTreeNode *treeNode[10];
char name[20][20]={"LEBRON","IVERSON","NUSH","DUNCAN","YAO","KOBY","PARK","ROY","ALLAN","PUAL"};
int num[10]={23,3,13,21,11,24,17,7,20,34};
for(i=0;i<10;i++)
{
student[i].num=num[i];
strcpy(student[i].name,name[i]);
}
for(i=0;i<10;i++)
{
treeNode[i]=MakeNode(student[i]);
}
MakeBinaryTree(treeNode[0],treeNode[1],treeNode[2]);
MakeBinaryTree(treeNode[1],treeNode[3],treeNode[4]);
MakeBinaryTree(treeNode[2],treeNode[5],treeNode[6]);
MakeBinaryTree(treeNode[3],treeNode[7],treeNode[8]);
MakeBinaryTree(treeNode[4],NULL,treeNode[9]);
BinaryTreeNode *BT;
Student x;
BT=NULL;
//构建分类二叉树
for(i=0;i<10;i++)
{
x.num=student[i].num;
strcpy(x.name,student[i].name);
BT=SortBinaryTreeInsert(BT,x);
}
Producer_Information();
Menu();
while(1)
{
cout<<"请输入功能编号: "; cin>>a;
switch(a)
{
case 1 ://显示二叉树的结构
cout<<"This is the binarytree"<<endl;
cout<<"\t\t 23.LEBRON\n";
cout<<"\t\t |--------|--------|\n";
cout<<"\t\t 3.IVERSON 13.NUSH \n";
cout<<"\t\t |----|----| |----|----|\n";
cout<<"\t\t 21.DUNCAN 11.YAO 24.KOBY 17.PARK \n";
cout<<"\t\t |----|----| |---|\n";
cout<<"\t\t 7.ROY 20.ALLAN 34.PUAL \n";
cout<<endl;
cout<<"构建分类二叉树:"<<endl;
InOrder(BT);//二叉树的中序遍历递归算法
cout<<endl;
break;
case 2 ://浏览前序遍历结构
//分类二叉树中查找一个元素
cout<<"请输入分类二叉树中查找关键字的学号:"<<endl;
cin>>Searchnum;
if(SortBinaryTreeSearch(BT,x,Searchnum))//如果找到,打印结果
cout<<"查找结果:"<<endl;
cout<<" "<<"关键字: "<<x.num<<" "<<"名字: "<<x.name<<endl;
break;
case 3://浏览中序遍历结构
//删除分类二叉树中一个元素
cout<<"请输入要删除分类二叉树中关键字的学号: ";
cin>>Searchnum;
SortBinaryTreeDelete (BT,Searchnum);
//打印删除后的分类二叉树
InOrder(BT);
break;
case 0:
return 0;
break;
default :
cout <<re_choose<<endl;
break;
}
}
}
转载于:https://www.cnblogs.com/arronliao/archive/2012/07/12/2587859.html