一个二叉数 创建的同时排序 然后左遍历 用C++写的 虽然有一个错误 但是我感觉好象还有更深曾的错误 希望大家救小弟一命 谢谢大家了
#include<iostream.h>
class tree;
class node
{
private:
int age;
double grade;
node *left,*right;
friend class tree;
};
class tree
{
private:
node *root;
public:
tree(){root=NULL}
tree(int sage,int sgrade)
{
age=sage;
grade=sgrade;
}
void creat_tree()
{
if(x->age!=jiesu)
{
node *newnode=new node;
cout<<" Plase Input age :"<<endl;
cin>>InAge;
if(InAge!=jiesu)
{
cout<<" Plase Input grade:"<<endl;
cin>>InGrade;
x=new Node(InAge,InGrade);
}
newnode=x;
newnode->left=newnode->right=NULL;
if(root==NULL)
root=newnode;
else
{
tree *back;
tree *current=root;
while(current!=NULL)
{
back=current;
if(current->grade<x->grade)
current=current-left;
else
current=current->right;
}
if(back->grade>x->grade)
{back-left=newnode;}
else
{back->right=newnode;}
}
}
}
}
void inorder(tree *tmp)
{
if(tmp!=NULL)
{
inorder(tmp-left);
cout<<tmp-age<<" "<<tmp->grade<<endl;
inorder(tmp-right);
}
}
};
void main()
{
tree A;
for(int i=0;i<=9;i++)
{
A.creat_tree();
}
A.inorder(root);
}
一个让我差点自杀的程序