请各位大牛们帮忙看看这个二叉树错在哪里了。。。

Breeze_Chen 2014-05-16 02:08:39

#include <iostream>
using namespace std;

class BinaryChainTree;

class BinaryChainTreeNode
{
friend BinaryChainTree;
private:
char data;
BinaryChainTreeNode* leftChild;
BinaryChainTreeNode* rightChild;
public:
BinaryChainTreeNode()
{
leftChild = NULL;
rightChild = NULL;
}

char getData()
{
return this->data;
}
};

class BinaryChainTree
{
private:
BinaryChainTreeNode* root;

public:
BinaryChainTree()
{
root = NULL;
}

BinaryChainTreeNode *getRoot()
{
return root;
}

void creat(BinaryChainTreeNode* t)
{
char ch;
cin>>ch;
if(ch == '#')
{
t = NULL;
}
else
{
t = new BinaryChainTreeNode;
t->data = ch;
creat(t->leftChild);
creat(t->rightChild);
}
}

bool isEmpty()
{
return (root == NULL);
}

int depth(BinaryChainTreeNode* p)
{
int left,right;
if(p == NULL)
{
return 0;
}
else
{
left = depth(p->leftChild);
right = depth(p->rightChild);
}
return ((left > right) ? left : right) + 1;
}

void clear(BinaryChainTreeNode* p)
{
delete p;
}

int count(BinaryChainTreeNode* p)
{
if(p == NULL)
return 0;
else
{
return (count(p->leftChild)+1) + (count(p->rightChild)+1);
}
}

void inorder(void (*Visit)(BinaryChainTreeNode* u),BinaryChainTreeNode* p)
{
if(p)
{
inorder(Visit,p->leftChild);
Visit(p);
inorder(Visit,p->rightChild);
}
}

void postorder(void (*Visit)(BinaryChainTreeNode* u),BinaryChainTreeNode* p)
{
if(p)
{
postorder(Visit,p->leftChild);
postorder(Visit,p->rightChild);
Visit(p);
}
}

void preorder(void (*Visit)(BinaryChainTreeNode* u),BinaryChainTreeNode* p)
{
if(p)
{
Visit(p);
preorder(Visit,p->leftChild);
preorder(Visit,p->rightChild);
}
}
};

这个是二叉树的实现的.h文件
下面是测试文件

#include"binaryChainTree.h"

void visit(BinaryChainTreeNode *t)
{
cout << t->getData();
}

int main()
{
BinaryChainTree tree;
tree.creat(tree.getRoot());
cout<<"Is empty? "<<tree.isEmpty()<<endl;
cout<<"Depth : "<<tree.depth(tree.getRoot())<<endl;
tree.preorder(visit,tree.getRoot());
tree.postorder(visit,tree.getRoot());
return 0;
}

现在遇到的最大的问题是执行文件后无法创建一个树,就是root一直都是NULL。
如图所示:

请问有什么解决方案么。想了大半天了。。百思不得其解。。。
...全文
139 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Breeze_Chen 2014-05-23
  • 打赏
  • 举报
回复
引用 5 楼 oYinGui1 的回复:
[quote=引用 3 楼 u013284981 的回复:] [quote=引用 2 楼 zcdabing 的回复:] 格式有问题 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的
但是从逻辑上来看这两个没有问题啊。。递归的去创建左孩子和右孩子。。[/quote] 请注意!函数形参的传值,如果你传的是指针,要修改指针的内容,那在C里就传指针的指针,C++里就传指针的引用!你可以回想一下刚开始学习的时候应该写过的交换两值吧,传进去的形参其实是个副本而已。[/quote] 恩!我试了,确实是这样,谢谢~
Breeze_Chen 2014-05-23
  • 打赏
  • 举报
回复
引用 4 楼 zcdabing 的回复:
[quote=引用 3 楼 u013284981 的回复:] [quote=引用 2 楼 zcdabing 的回复:] 格式有问题 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的
但是从逻辑上来看这两个没有问题啊。。递归的去创建左孩子和右孩子。。[/quote] 我意思是私有成员没法直接赋值, private: BinaryChainTreeNode* leftChild; void creat(BinaryChainTreeNode* t) { char ch; cin>>ch; if(ch == '#') { t = NULL; } else { t = new BinaryChainTreeNode; t->data = ch; creat(t->leftChild); creat(t->rightChild); } } 如果你还不懂

class A
{
private: 
    int m_nNum;
}

A a;
a.m_nNum= 10;//这么做是不行的

[/quote] 我用的是友元,可以访问的。。我觉得楼下说的是对的,而且改了以后确实是对的
Morrisss_ 2014-05-17
  • 打赏
  • 举报
回复
引用 3 楼 u013284981 的回复:
[quote=引用 2 楼 zcdabing 的回复:] 格式有问题 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的
但是从逻辑上来看这两个没有问题啊。。递归的去创建左孩子和右孩子。。[/quote] 请注意!函数形参的传值,如果你传的是指针,要修改指针的内容,那在C里就传指针的指针,C++里就传指针的引用!你可以回想一下刚开始学习的时候应该写过的交换两值吧,传进去的形参其实是个副本而已。
zcdabing 2014-05-16
  • 打赏
  • 举报
回复
引用 3 楼 u013284981 的回复:
[quote=引用 2 楼 zcdabing 的回复:] 格式有问题 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的
但是从逻辑上来看这两个没有问题啊。。递归的去创建左孩子和右孩子。。[/quote] 我意思是私有成员没法直接赋值, private: BinaryChainTreeNode* leftChild; void creat(BinaryChainTreeNode* t) { char ch; cin>>ch; if(ch == '#') { t = NULL; } else { t = new BinaryChainTreeNode; t->data = ch; creat(t->leftChild); creat(t->rightChild); } } 如果你还不懂

class A
{
private: 
    int m_nNum;
}

A a;
a.m_nNum= 10;//这么做是不行的

Breeze_Chen 2014-05-16
  • 打赏
  • 举报
回复
引用 2 楼 zcdabing 的回复:
格式有问题 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的
但是从逻辑上来看这两个没有问题啊。。递归的去创建左孩子和右孩子。。
zcdabing 2014-05-16
  • 打赏
  • 举报
回复
格式有问题 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的 class BinaryChainTreeNode { private: char data; BinaryChainTreeNode* leftChild; BinaryChainTreeNode* rightChild; creat(t->leftChild); creat(t->rightChild); left = depth(p->leftChild); right = depth(p->rightChild); 可能还有别的
zcdabing 2014-05-16
  • 打赏
  • 举报
回复

class BinaryChainTreeNode
{
private:
    char data;
    BinaryChainTreeNode* leftChild;
    BinaryChainTreeNode* rightChild;


            creat(t->leftChild);
            creat(t->rightChild);

            left = depth(p->leftChild);
            right = depth(p->rightChild);

可能还有别的

64,635

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧