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

Breeze_Chen Breeze_Chen 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区

#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。
如图所示:

请问有什么解决方案么。想了大半天了。。百思不得其解。。。
...全文

相关推荐

评论 7
收藏 已收藏
分享
返回顶部
Breeze_Chen
C++ 语言 65,214 成员 250,514 内容