请各位大牛们帮忙看看这个二叉树错在哪里了。。。
#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。
如图所示:

请问有什么解决方案么。想了大半天了。。百思不得其解。。。
...全文
相关推荐
USACO大牛写的前三十个的代码
常见的数据结构如数组、链表、栈、队列、树(二叉树、平衡树、堆)、图等,在解决实际问题时都有其独特的应用。例如,栈可以用来实现回溯算法,队列用于广度优先搜索,二叉树则常用于搜索和排序问题。
杨弋大牛的线段树论文(pdf)
- **特点:** - 完全二叉树形式:线段树通常采用完全二叉树的形式进行构建。 - 节点含义:每个节点代表一个区间或线段,根节点代表整个区间。
ACM大牛总结的线段树专辑,超经典的-1
"ACM大牛总结的线段树专辑,包含线段树的基本概念、优化技巧和典型应用,适合ACM竞赛者学习和提升。"线段树是一种高效的数据结构,用于处理动态区间查询和更新问题。在ACM竞赛中,线段树是必备的数据
算法(第四版)
在数据结构方面,会详细讲解树结构,如二叉树、平衡二叉树(AVL树和红黑树)、B树和B+树,这些在数据库索引和文件系统中有着广泛应用。
ACM算法资料
**数据结构**:包括链表、队列、栈、树(二叉树、平衡树AVL、红黑树等)、哈希表等,它们是解决问题的基础。8.


