第一范文网 - 专业文章范例文档资料分享平台

数据结构二叉排序树的实现 (用顺序和二叉链表作存储结构 )课程设计

来源:用户分享 时间:2025/5/15 21:04:47 本文由loading 分享 下载这篇文档手机版
说明:文章内容仅供预览,部分内容可能不全,需要完整文档或者需要复制内容,请下载word后使用。下载word有问题请添加微信号:xxxxxxx或QQ:xxxxxx 处理(尽可能给您提供完整文档),感谢您的支持与谅解。

{

if(ptr->rl()==NULL&&ptr->rr()==NULL) ptr=NULL;

else if(ptr->rr()==NULL) ptr=ptr->rl(); else

ptr=ptr->rr(); } private: int data;

node *left; node *right; };

五、程序实现

1、调入文件 #include 2、主函数 int main() { int t,i=0,j;

cout<<\ 计科一班 杨 旭(1010311114)\

10 cout<<\二叉排序树T的输入:\ cout<<\输入数字个数(结点个数):\ cin>>t;

cout<<\输入\个数字,数字之间用空格隔开:\ cin>>j;

node *x=new node(j); for(;i>j; x->insert(x,j); }

cout<<\中序遍历为:\

x->inorder(x); //作中序遍历 cout<<\

cout<<\二叉排序树T的元素查找和删除:\ cout<<\输入操作(当输入-1时程序结束):\ cin>>j; while(j!=-1) {

node *t=x->find(x,j); //定位结点 if(t!=NULL)

{

node *&y=x->findy(x,j); x->dele(y); cout<<\中序遍历为:\ x->inorder(x); }

else cout<<\无\

cout<<\输入操作(当输入-1时程序结束):\ cin>>j; } return 0; }

六、程序编码

#include using namespace std;

class node { public:

node(int i):data(i),left(NULL),right(NULL){}

void inorder(node *&root) //中序遍历,符合升序输出 {

if(root!=NULL) {

inorder(root->left); cout<data<<' '; inorder(root->right); } }

void insert(node *&ptr,int item) //在查找树中插入元素 {

if(ptr==NULL) ptr=new node(item); else if(itemdata) insert(ptr->left,item); else insert(ptr->right,item); }

node *find(node *&ptr,int item) //在查找树中查找元素,找到返回所在结点指针,找不到返回空指针。 {

if(ptr==NULL)

return NULL; if(ptr->data==item) return ptr; else if(itemdata) find(ptr->left,item); else find(ptr->right,item); }

node *&findy(node *&ptr,int item) //在查找树中查找肯定存在的元素,并返回其引用 {

if(ptr->data==item) return ptr; else if(itemdata) findy(ptr->left,item); else findy(ptr->right,item); }

node* rl(){return left;} node* rr(){return right;}

void dele(node *&ptr) //删除值为item所在结点 {

if(ptr->rl()==NULL&&ptr->rr()==NULL)

数据结构二叉排序树的实现 (用顺序和二叉链表作存储结构 )课程设计.doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.diyifanwen.net/c8rxmr97glj3qhtz4wkc8_2.html(转载请注明文章来源)
热门推荐
Copyright © 2012-2023 第一范文网 版权所有 免责声明 | 联系我们
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:xxxxxx 邮箱:xxxxxx@qq.com
渝ICP备2023013149号
Top