一个关于二叉树初始化调试无法读取内存的问题
这说明这个二叉树根本没有联系在一起吧,
#pragma once
#include<iostream>
#include<string>
template <class T>
struct treenode
{
T data;
bool ltag;
bool rtag;
treenode<T> *lchild;
treenode<T> *rchild;
};
template <class T>
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
void middle(treenode<T> *t);
void clue();
private:
void createtree(treenode<T> *t);
treenode<T> *head,*pre,*p,*t;
T c;
};
template<class T>
BinaryTree<T>::BinaryTree()
{
createtree(t);
clue();
}
template<class T>
BinaryTree<T>::~BinaryTree()
{
}
template<class T>
void BinaryTree<T>::middle(treenode<T>* t)
{
if (t)
{
middle(t->lchild);
if (!t->lchild)
{
t->ltag = 1;
t->lchild = pre;
}
if (!pre->rchild)
{
pre->rtag = 1;
pre->rchild = t;
}
pre = t;
middle(t->rchild);
}
}
template<class T>
void BinaryTree<T>::clue()
{
treenode<T> *s = new treenode<T>;
s->ltag = 0;
s->rtag = 1;
s->rchild = head;
if (!head)
{
s->lchild = s;
}
else
{
s->lchild = head;
pre = s;
middle(head);
pre->rchild = p;
pre->rtag = 1;
s->rchild = pre;
}
}
int mbf=1;//我是实在没办法才搞了这个东西,它存在的目的是为了让93行的if只在开始的时候执行一次
template<class T>
void BinaryTree<T>::createtree(treenode<T> *t)
{
std::cin >> c;
if ('#' == c)
{
t = NULL;
}
else
{
t = new treenode<T>;
if (mbf==1)
{
head = t;
mbf--;
}
t->data = c;
t->ltag = 0;
t->rtag = 0;
createtree(t->lchild);
createtree(t->rchild);
}
}
还有main
#include<iostream>
#include"标头.h"
#include<string>
int main()
{
BinaryTree<char> shu;
return 0;
} 在createtree方法里下了断点,在生成了左右孩子之后,head的左右孩子指针显示无法读取内存 很懵逼 ”在生成了左右孩子之后,head的左右孩子指针显示无法读取内存”
那是因为你没有修改了head的左右孩子指针
101和102行
createtree(t->lchild);
createtree(t->rchild);
告诉我,要修改一个变量的内容,如何做?
对,没错,要传地址
明白了的话自己改代码吧
#include<iostream>
#include<string>
template <class T>
struct treenode
{
T data;
bool ltag;
bool rtag;
treenode<T> *lchild;
treenode<T> *rchild;
};
template <class T>
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
void middle(treenode<T> *t);
void clue();
private:
void createtree(treenode<T> *t);
treenode<T> *head, *pre, *p, *t;
T c;
};
template<class T>
BinaryTree<T>::BinaryTree()
{
createtree(t);
clue();
}
template<class T>
BinaryTree<T>::~BinaryTree()
{
}
template<class T>
void BinaryTree<T>::middle(treenode<T> *t)
{
if(t)
{
middle(t->lchild);
if(!t->lchild)
{
t->ltag = 1;
t->lchild = pre;
}
if(!pre->rchild)
{
pre->rtag = 1;
pre->rchild = t;
}
pre = t;
middle(t->rchild);
}
}
template<class T>
void BinaryTree<T>::clue()
{
treenode<T> *s = new treenode<T>;
s->ltag = 0;
s->rtag = 1;
s->rchild = head;
if(!head)
{
s->lchild = s;
}
else
{
s->lchild = head;
pre = s;
middle(head);
pre->rchild = p;
pre->rtag = 1;
s->rchild = pre;
}
}
template<class T>
void SetVal(treenode<T> *t)
{
t = NULL;
}
template<class T>
void SetVal(treenode<T> **t)
{
*t = NULL;
}
int mbf = 1;//我是实在没办法才搞了这个东西,它存在的目的是为了让93行的if只在开始的时候执行一次
template<class T>
void BinaryTree<T>::createtree(treenode<T> *t)
{
std::cin >> c;
if('#' == c)
{
t = NULL;
}
else
{
t = new treenode<T>;
if(mbf == 1)
{
head = t;
mbf--;
}
t->data = c;
t->ltag = 0;
t->rtag = 0;
//debug
std::cout << t->lchild << std::endl;
SetVal(t->lchild);
std::cout << t->lchild << std::endl;
SetVal(&t->lchild);
std::cout << t->lchild << std::endl;
exit(1);
createtree(t->lchild);
createtree(t->rchild);
}
}
int main(void)
{
BinaryTree<char> shu;
return 0;
}
0
CDCDCDCD
CDCDCDCD
00000000
请按任意键继续. . .
人造人 发表于 2018-4-1 15:08
head和t是指向treenode结构的指针,声明一块treenode类型的内存让t指向他,让head也指向那块内存
然后把t->lchild传入递归,t->lchild就变成新的t了吧,然后声明一块treenode内存让t->lchild指向他我是这样想的,lchild是指针啊,不明白,我还想过t是指针,*t才是指向内存的本身,然后改成了createtree((*t)->lchild);createtree((*t)->rchild);然后报错了
未灭时间 发表于 2018-4-1 22:21
head和t是指向treenode结构的指针,声明一块treenode类型的内存让t指向他,让head也指向那块内存
然后把 ...
建议你深入学习C语言的指针部分
这样应该就可以了吧
#include<iostream>
#include<string>
template <class T>
struct treenode
{
T data;
bool ltag;
bool rtag;
treenode<T> *lchild;
treenode<T> *rchild;
};
template <class T>
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
void middle(treenode<T> *t);
void clue();
private:
void createtree(treenode<T> **t);
treenode<T> *head, *pre, *p, *t;
T c;
};
template<class T>
BinaryTree<T>::BinaryTree()
{
createtree(&t);
clue();
}
template<class T>
BinaryTree<T>::~BinaryTree()
{
}
template<class T>
void BinaryTree<T>::middle(treenode<T> *t)
{
if(t)
{
middle(t->lchild);
if(!t->lchild)
{
t->ltag = 1;
t->lchild = pre;
}
if(!pre->rchild)
{
pre->rtag = 1;
pre->rchild = t;
}
pre = t;
middle(t->rchild);
}
}
template<class T>
void BinaryTree<T>::clue()
{
treenode<T> *s = new treenode<T>;
s->ltag = 0;
s->rtag = 1;
s->rchild = head;
if(!head)
{
s->lchild = s;
}
else
{
s->lchild = head;
pre = s;
middle(head);
pre->rchild = p;
pre->rtag = 1;
s->rchild = pre;
}
}
int mbf = 1;//我是实在没办法才搞了这个东西,它存在的目的是为了让93行的if只在开始的时候执行一次
template<class T>
void BinaryTree<T>::createtree(treenode<T> **t)
{
std::cin >> c;
if('#' == c)
{
*t = NULL;
}
else
{
*t = new treenode<T>;
if(mbf == 1)
{
head = *t;
mbf--;
}
(*t)->data = c;
(*t)->ltag = 0;
(*t)->rtag = 0;
createtree(&(*t)->lchild);
createtree(&(*t)->rchild);
}
}
int main(void)
{
BinaryTree<char> shu;
return 0;
}
另外参考这个代码
这是百度的
#include <cstdio>
#include <cstdlib>
#define ElemType char
//节点声明,数据域、左孩子指针、右孩子指针
typedef struct BiTNode {
char data;
struct BiTNode *lchild, *rchild;
}BiTNode, *BiTree;
//先序建立二叉树
BiTree CreateBiTree() {
char ch;
BiTree T;
scanf("%c", &ch);
if(ch == '#')T = NULL;
else {
T = (BiTree)malloc(sizeof(BiTNode));
T->data = ch;
T->lchild = CreateBiTree();
T->rchild = CreateBiTree();
}
return T;//返回根节点
}
//先序遍历二叉树
void PreOrderTraverse(BiTree T) {
if(T) {
printf("%c", T->data);
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
}
}
//中序遍历
void InOrderTraverse(BiTree T) {
if(T) {
PreOrderTraverse(T->lchild);
printf("%c", T->data);
PreOrderTraverse(T->rchild);
}
}
//后序遍历
void PostOrderTraverse(BiTree T) {
if(T) {
PreOrderTraverse(T->lchild);
PreOrderTraverse(T->rchild);
printf("%c", T->data);
}
}
int main(void) {
BiTree T;
T = CreateBiTree();//建立
PreOrderTraverse(T);//输出
return 0;
}
这一次修改,去掉了
“int mbf = 1;//我是实在没办法才搞了这个东西,它存在的目的是为了让93行的if只在开始的时候执行一次”
#include<iostream>
#include<string>
template <class T>
struct treenode
{
T data;
bool ltag;
bool rtag;
treenode<T> *lchild;
treenode<T> *rchild;
};
template <class T>
class BinaryTree
{
public:
BinaryTree();
~BinaryTree();
void middle(treenode<T> *t);
void clue();
private:
void createtree(treenode<T> **t);
treenode<T> *head, *pre, *p, *t;
T c;
};
template<class T>
BinaryTree<T>::BinaryTree()
{
head = NULL;
createtree(&head);
clue();
}
template<class T>
BinaryTree<T>::~BinaryTree()
{
}
template<class T>
void BinaryTree<T>::middle(treenode<T> *t)
{
if(t)
{
middle(t->lchild);
if(!t->lchild)
{
t->ltag = 1;
t->lchild = pre;
}
if(!pre->rchild)
{
pre->rtag = 1;
pre->rchild = t;
}
pre = t;
middle(t->rchild);
}
}
template<class T>
void BinaryTree<T>::clue()
{
treenode<T> *s = new treenode<T>;
s->ltag = 0;
s->rtag = 1;
s->rchild = head;
if(!head)
{
s->lchild = s;
}
else
{
s->lchild = head;
pre = s;
middle(head);
pre->rchild = p;
pre->rtag = 1;
s->rchild = pre;
}
}
template<class T>
void BinaryTree<T>::createtree(treenode<T> **t)
{
std::cin >> c;
if('#' == c)
{
*t = NULL;
}
else
{
*t = new treenode<T>;
(*t)->data = c;
(*t)->ltag = 0;
(*t)->rtag = 0;
createtree(&(*t)->lchild);
createtree(&(*t)->rchild);
}
}
int main(void)
{
BinaryTree<char> shu;
return 0;
}
人造人 发表于 2018-4-1 23:01
这一次修改,去掉了
“int mbf = 1;//我是实在没办法才搞了这个东西,它存在的目的是为了让93行的if只在开 ...
非常感谢,
页:
[1]