下载此文档

建立一个排序二叉树.doc


文档分类: | 页数:约5页 举报非法文档有奖
1/5
下载提示
  • 1.该资料是网友上传的,本站提供全文预览,预览什么样,下载就什么样。
  • 2.下载该文档所得收入归上传者、原创者。
  • 3.下载的文档,不会出现我们的网址水印。
1/5 下载此文档
文档列表 文档介绍
建立一个排序二叉树,并给出中序遍历该排序二叉树的结果
#include ""
#include<iostream>
using namespace std;
//-----------排序二叉树节点--------------//
struct tree //定义二叉树节点结构
{
int data;   //节点数据域
tree *right,*left; //右,左子树指针
};
//-----------排序二叉树类----------------//
class Btree
{
tree *root;//根节点
public:
Btree()
{
   root=NULL;//根节点在构造函数里初始化
}
void create_btree(int);//创建排序二叉树
void display() //显示进行中序遍历排序后的数据
{
   inorder(root);//调用中序遍历排序函数
   cout<<endl;
}
void inorder(tree *);//进行中序遍历排序
};
void Btree::create_btree(int t)
{
tree *newnode=new tree;//创建新的节点存入数据并插入二叉树中
newnode->data =t;        //存入数据t
newnode->left =NULL;    
newnode->right =NULL;
if(root==NULL) //当是根节点为空时即二叉树中没有任何数据时
{
   root=newnode; //根节点为新节点
}
else         //当有二叉树中拥有数据后
{
   tree *back;
   tree *current;
   current=root;
    while(current!=NULL)
   {
    back=current; //记住current的父节点
            if(current->data>t)
     current=current->left;
    else current=current->right ;
   }
   if(back->data>t)
    back->left =newnode;
   else back->right =newnode;
}
}
void Btree::inorder (tree* tmp)//采用递归进行中序遍历
{
if(tmp!=NULL)
{
   inorder(tmp->left);
   cout<<tmp->data<<" ";
   inorder(tmp->right );
}
}
int main(int argc, char* argv[])
{
    Btree A;
int arr[]={7,4,1,5,12,8,13,11};
cout<<"建立排序二叉树顺序:"<<endl;
for(int i=0;i<8;i++)
{
   cout<<arr[i]<<" ";
   (a

建立一个排序二叉树 来自淘豆网www.taodocs.com转载请标明出处.