找回密码
 注册
搜索
热搜: 回贴
  • 前程无忧官网首页 有什么好的平台可以
  • 最新的销售平台 互联网营销的平台有哪
  • 制作网页的基本流程 网页制作和网页设
  • 【帝国CMS】输出带序号的列表(数字排
  • 网站建设公司 三一,中联,极东泵车的
  • 织梦 建站 织梦网站模版后台怎么更改
  • 云服务官网 哪些网站有免费的简历模板
  • 如何建网站要什么条件 建网站要用什么
  • 吉林市移动公司电话 吉林省退休人员网
  • 设计类毕业论文 网站设计与实现毕业论
查看: 933|回复: 9

二叉树???帮忙!!

[复制链接]
发表于 2009-10-31 01:16:38 | 显示全部楼层 |阅读模式 IP:江苏扬州
我本人写了个二叉树,用递归的方法,但是我遇到问题了,我定义不了头节点,郁闷,大家帮忙看看把,谢谢了!!
#include<iostream.h> class Node { friend class TREE; int DATA; Node *LEFT; Node *RIGHT; }; class TREE { private: Node *ROOT; public: TREE() { ROOT=0; } void Insert(int data,Node *root) { if(root==0) { root=new Node; root->LEFT=root->RIGHT=0; root->DATA=data; } else { if(data<root->DATA) Insert(data,root->LEFT); else Insert(data,root->RIGHT); } } void Display(Node *root) { if(root!=NULL) { Display(root->LEFT); cout<<root->DATA<<endl; Display(root->RIGHT); } } }; int main() { TREE T; Node *root=0; T.Insert(20,root); //我想让20成为头节点,但我不知道怎么搞? T.Insert(52,root); T.Insert(42,root); T.Insert(75,root); T.Insert(1,root); T.Insert(5,root); T.Insert(6,root); T.Insert(9,root); T.Display(root); return 0; }
发表于 2009-10-31 01:16:40 | 显示全部楼层 IP:江苏扬州
写了些问题,不过还没找到你问的问题原因。
#include<iostream.h>
class Node //其实考虑到内存效率,建议使用struct { friend class TREE; int DATA; Node *LEFT; Node *RIGHT; };
class TREE { private: Node *ROOT; //多余的定义,因为参数里面已经有定义了 public: TREE() { ROOT=0; //地址怎么给赋个0? } void Insert(int data,Node *root) //函数体内容最好写在类外面 { if(root==0) //判断NULL吧? { root=new Node; root->LEFT=root->RIGHT=0; //赋NULL root->DATA=data; } else { if(data<root->DATA) Insert(data,root->LEFT); else Insert(data,root->RIGHT); } }
void Display(Node *root) //函数体内容最好写在类外面 { if(root!=NULL) { Display(root->LEFT); cout<<root->DATA<<endl; Display(root->RIGHT); } } };
void main() { TREE T; Node *root=0; //Node类没有定义构造,你赋干啥
T.Insert(20,root); //你传个参数每次都是root,同一个起点可能会出错的 T.Insert(52,root); T.Insert(42,root); T.Insert(75,root); T.Insert(1,root); T.Insert(5,root); T.Insert(6,root); T.Insert(9,root); T.Display(root); }
回复

使用道具 举报

发表于 2009-10-31 01:16:41 | 显示全部楼层 IP:江苏扬州
另提个意见,类的成员函数的定义内容不要在类里面写,类里做个声明就行。
回复

使用道具 举报

发表于 2009-10-31 01:16:42 | 显示全部楼层 IP:江苏扬州
你在main函数有 Node *root = NULL; 在类构造又一个,在函数参数又定义一个 Node *root ,总共有三个,你要哪个?
回复

使用道具 举报

发表于 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); }
回复

使用道具 举报

发表于 2009-10-31 01:16:44 | 显示全部楼层 IP:江苏扬州
如果还不行,把 void Insert(int data,BTreeNode*& root); 参数中的 root 改成 & 。
回复

使用道具 举报

发表于 2009-10-31 01:16:45 | 显示全部楼层 IP:江苏扬州
把void Insert(int data,Node* root)中的指针参数改为引用void Insert(int data,Node*& root)
应该就OK了。
不过就象live41说的,我也建议你把类的成员函数,在外面定义。
回复

使用道具 举报

发表于 2009-10-31 01:16:46 | 显示全部楼层 IP:江苏扬州
^_^!!哈哈!!
我太高兴了,实在是太谢谢 楼上的两位 解了我心中好久的困惑!
对于live41的意见我会接受的,但是我想知道为什么要那样子呢??
解释一下好吗??
------------------
立志做一个好的程序员!!
回复

使用道具 举报

发表于 2009-10-31 01:16:47 | 显示全部楼层 IP:江苏扬州
因为在类中定义的函数将自动转换成内联函数,当类的成员函数为短函数的,这当然很好。
可是当成员函数为一个比较大的函数的,它内联的开销将比调用它还要大。
所以,比较复杂的成员函数建议都在类外定义。
回复

使用道具 举报

发表于 2009-10-31 01:16:48 | 显示全部楼层 IP:江苏扬州
以下是引用devil8283在2004-10-31 22:47:29的发言:
因为在类中定义的函数将自动转换成内联函数,当类的成员函数为短函数的,这当然很好。
可是当成员函数为一个比较大的函数的,它内联的开销将比调用它还要大。
所以,比较复杂的成员函数建议都在类外定义。
而且如果内联中遇到switch的话将隐式自动转为普通函数。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|小黑屋|最新主题|手机版|微赢网络技术论坛 ( 苏ICP备08020429号 )

GMT+8, 2024-9-29 15:27 , Processed in 0.251156 second(s), 13 queries , Gzip On, MemCache On.

Powered by Discuz! X3.5

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表