📄 a二叉排序树的操作.cpp
字号:
#include <iostream.h>
struct Tree
{
int data;
Tree *lc,*rc;
};
Tree* CreateSortTree()
{
Tree *root;
Tree *p,*q;
int x;
root=NULL;
cout<<"建立二叉排序树,输入数据,以-1结束:"<<endl;
cin>>x;
while(x!=-1)
{
p=new Tree;
p->data=x;
p->lc=NULL;
p->rc=NULL;
if(root==NULL)
root=p;
else
{
q=root;
while(q!=NULL)
{
if(x<q->data)
if(q->lc!=NULL)
q=q->lc;
else
{
q->lc=p;
q=NULL;
}
else
if(q->rc!=NULL)
q=q->rc;
else
{
q->rc=p;
q=NULL;
}
}
}
cin>>x;
}
return root;
}
void InOrder(Tree *bt)
{
if(bt==NULL)
return;
InOrder(bt->lc);
cout<<bt->data<<" ";
InOrder(bt->rc);
}
void main()
{
Tree *root;
root=CreateSortTree();
cout<<"中序遍历:";
InOrder(root);
cout<<endl;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -