| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2014 人关注过本帖
标题:二叉树???帮忙!!
只看楼主 加入收藏
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
 问题点数:0 回复次数:13 
二叉树???帮忙!!

我本人写了个二叉树,用递归的方法,但是我遇到问题了,我定义不了头节点,郁闷,大家帮忙看看把,谢谢了!!

#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; }

搜索更多相关主题的帖子: 二叉树 root Node TREE DATA 
2004-10-31 18:30
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

写了些问题,不过还没找到你问的问题原因。

#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); }

[此贴子已经被作者于2004-10-31 19:57:56编辑过]

2004-10-31 18:42
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
另提个意见,类的成员函数的定义内容不要在类里面写,类里做个声明就行。
2004-10-31 19:55
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

你在main函数有 Node *root = NULL; 在类构造又一个, 在函数参数又定义一个 Node *root ,总共有三个,你要哪个?

[此贴子已经被作者于2004-10-31 20:02:08编辑过]

2004-10-31 20:00
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

改了一下,你试试先:

#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); }

2004-10-31 20:13
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 

如果还不行,把 void Insert(int data,BTreeNode*& root); 参数中的 root 改成 & 。

2004-10-31 20:15
devil8283
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2004-10-2
收藏
得分:0 

把void Insert(int data,Node* root)中的指针参数改为引用void Insert(int data,Node*& root)

应该就OK了。

不过就象live41说的,我也建议你把类的成员函数,在外面定义。

[此贴子已经被作者于2004-10-31 21:25:43编辑过]


曾经我不成熟,不过——现在我也成熟不 到哪里去。在我眼中,人生就像一场脱俗的梦, 谁最先醒来,谁就会最失落。 My dream will go on. —— Goon ---------------------
2004-10-31 21:20
corrupt
Rank: 2
等 级:新手上路
威 望:3
帖 子:535
专家分:0
注 册:2004-9-29
收藏
得分:0 

^_^!!哈哈!!

我太高兴了,实在是太谢谢 楼上的两位 解了我心中好久的困惑!

对于live41的意见我会接受的,但是我想知道为什么要那样子呢??

解释一下好吗??

------------------

立志做一个好的程序员!!


2004-10-31 21:41
devil8283
Rank: 1
等 级:新手上路
帖 子:46
专家分:0
注 册:2004-10-2
收藏
得分:0 

因为在类中定义的函数将自动转换成内联函数,当类的成员函数为短函数的,这当然很好。

可是当成员函数为一个比较大的函数的,它内联的开销将比调用它还要大。

所以,比较复杂的成员函数建议都在类外定义。


曾经我不成熟,不过——现在我也成熟不 到哪里去。在我眼中,人生就像一场脱俗的梦, 谁最先醒来,谁就会最失落。 My dream will go on. —— Goon ---------------------
2004-10-31 22:47
live41
Rank: 10Rank: 10Rank: 10
等 级:贵宾
威 望:67
帖 子:12442
专家分:0
注 册:2004-7-22
收藏
得分:0 
以下是引用devil8283在2004-10-31 22:47:29的发言:

因为在类中定义的函数将自动转换成内联函数,当类的成员函数为短函数的,这当然很好。

可是当成员函数为一个比较大的函数的,它内联的开销将比调用它还要大。

所以,比较复杂的成员函数建议都在类外定义。

而且如果内联中遇到switch的话将隐式自动转为普通函数。
2004-11-01 09:01
快速回复:二叉树???帮忙!!
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015563 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved