|
发表于 2009-10-31 01:16:43
|
显示全部楼层
IP:江苏扬州
改了一下,你试试先:
#include<iostream.h>
struct BTreeNode { int data; BTreeNode* left; BTreeNode* right; };
class BTree { public: void Insert(int data,BTreeNode*& root); void Display(BTreeNode *root); };
void BTree::Insert(int data,BTreeNode*& root) { if(root==NULL) { root=new BTreeNode; root->left=root->right=NULL; root->data=data; } else { if(data<root->data) Insert(data,root->left); else Insert(data,root->right); } }
void BTree::Display(BTreeNode *root) { if(root!=NULL) { Display(root->left); cout<<root->data<<" "; Display(root->right); } }
void main() { BTree tree; BTreeNode *root = NULL;
tree.Insert(20,root); tree.Insert(52,root); tree.Insert(42,root); tree.Insert(75,root); tree.Insert(1,root); tree.Insert(5,root); tree.Insert(6,root); tree.Insert(9,root);
tree.Display(root); } |
|