高分求 二叉树哪里有问题?

hencechen 2008-12-01 10:26:37
二叉树无法被建立,而且当递归进入build函数的时候值传递完全错误?


#ifndef BTREE_H
#define BTREE_H


#include <iostream>
using namespace std;



class node
{
public:
node();
int data;//数据域
node *lchild;//左子树指针
node *rchild;//右子树指针
};


node::node()
{
data=NULL;
lchild=NULL;
rchild=NULL;
}


class BTree
{
public:
//递归建立二叉树
BTree(int val[],int l);
node *build (int val[],int i);
//前序遍历
void printbef();
void printbef(node *p);
//中序遍历
void printmid();
void printmid(node *p);
//后序遍历
void printlas();
void printlas(node *p);
//获取结点数目
int getnum();
int getnum(node *p);
//获取二叉树高度
int getheight();
int getheight(node *p);
//判断两棵二叉树是否相等
bool equl(node *p1,node *p2);
bool equl(BTree t2);
private:
node *root;//根结点
int len;//元素个数
};


BTree::BTree(int val[],int l)
{
int len=l;
root=build(val,1);
}

node *BTree::build(int val[],int num)
{

if(num<=len)
{
node *p=new node;
p->data=val[num-1];
int l=num*2;
int r=num*2+1;
p->lchild = build(val,l);
p->rchild = build(val,r);
return p;
}
return NULL;
}

void BTree::printbef()
{
printbef(root);
}

void BTree::printbef (node *p)
{
if(p!=NULL)
{
cout<<p->data<<" ";
printbef(p->lchild);
printbef(p->rchild);
}
}


void BTree::printmid()
{
printmid(root);
}

void BTree::printmid (node *p)
{
if(p!=NULL)
{
printbef(p->lchild);
printbef(p->rchild);
cout<<p->data<<" ";
}
}

void BTree::printlas()
{
printlas(root);
}

void BTree::printlas (node *p)
{
if(p!=NULL)
{
printbef(p->lchild);
printbef(p->rchild);
cout<<p->data<<" ";
}
}

int BTree::getnum ()
{
return getnum(root);
}

int BTree::getnum (node *p)
{
if(p!=NULL)
{
return (getnum(p->lchild )+getnum(p->rchild )+1);
}
return 0;
}

int BTree::getheight ()
{
return getheight(root);
}

int BTree::getheight (node *p)
{
if(NULL==p)
return 0;
int max;
max=getheight(p->lchild );
if(max<getheight(p->rchild ))
max=getheight(p->rchild );
return (max+1);
}

bool BTree::equl (BTree t2)
{
return equl(this->root ,t2.root);
}

bool BTree::equl (node *p1,node *p2)
{
if(p1!=p2)
return false;
else
{
if((NULL==p1) &&(NULL==p2))
return true;
if((NULL==p1) || (NULL==p2))
return false;
if(p1->data !=p2->data )
return false;
if(equl(p1->lchild ,p2->lchild )&&equl(p1->rchild ,p2->rchild ))
return true;
}
return false;
}

#endif

void main()
{
int a[]={1,2,4,23,54,234};
BTree tree(a,6);
tree.printbef();

}

...全文
148 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
hencechen 2008-12-27
  • 打赏
  • 举报
回复
我后来自己也发现了,不过还是给分了。
是的,后面的递归函数应该设置成私有的会更好
elegant87 2008-12-12
  • 打赏
  • 举报
回复
我认为把像这样的node *build (int val[],int i);函数改为私有成员更好!
这些函数只是在类的公有成员函数调用的!在类的外部是不能被访问的!
这纯属于C++啊!
elegant87 2008-12-12
  • 打赏
  • 举报
回复
呵呵!写的很好啊!知道我是谁吧!
真巧!在这见你了!
xxgamexx 2008-12-05
  • 打赏
  • 举报
回复
确实是这样的

代码写的也蛮好看 学习~
wzyzb 2008-12-05
  • 打赏
  • 举报
回复
写的不错 递归真是奇妙啊 错误楼上的改了
dreamsdark 2008-12-05
  • 打赏
  • 举报
回复
类构造函数初始化:
BTree::BTree(int val[],int l)
{
int len=l;//并不是给类成员len赋值,而是定义了一个新的成员变量len。出了函数就没有了。
root=build(val,1);
}
修改后的代码如下:
class node
{
public:
node();
int data;//数据域
node *lchild;//左子树指针
node *rchild;//右子树指针
};


node::node()
{
data=NULL;
lchild=NULL;
rchild=NULL;
}


class BTree
{
public:
//递归建立二叉树
BTree(int val[],int a);
node *build (int val[],int i);
//前序遍历
void printbef();
void printbef(node *p);
//中序遍历
void printmid();
void printmid(node *p);
//后序遍历
void printlas();
void printlas(node *p);
//获取结点数目
int getnum();
int getnum(node *p);
//获取二叉树高度
int getheight();
int getheight(node *p);
//判断两棵二叉树是否相等
bool equl(node *p1,node *p2);
bool equl(BTree t2);
// void setlen(int len);
private:
node *root;//根结点
int len;//元素个数
};


BTree::BTree(int val[],int a)
:len(a)
{
// setlen(1);;
root=build(val,1);
}

node *BTree::build(int val[],int num)
{
if(num<=len)
{
node *p=new node;
p->data=val[num-1];
int l=num*2;
int r=num*2+1;
p->lchild = build(val,l);
p->rchild = build(val,r);
return p;
}
return NULL;
}

void BTree::printbef()
{
printbef(root);
}

void BTree::printbef (node *p)
{
if(p!=NULL)
{
cout<<p->data<<" ";
printbef(p->lchild);
printbef(p->rchild);
}
}


void BTree::printmid()
{
printmid(root);
}

void BTree::printmid (node *p)
{
if(p!=NULL)
{
printbef(p->lchild);
printbef(p->rchild);
cout<<p->data<<" ";
}
}

void BTree::printlas()
{
printlas(root);
}

void BTree::printlas (node *p)
{
if(p!=NULL)
{
printbef(p->lchild);
printbef(p->rchild);
cout<<p->data<<" ";
}
}

int BTree::getnum ()
{
return getnum(root);
}

int BTree::getnum (node *p)
{
if(p!=NULL)
{
return (getnum(p->lchild )+getnum(p->rchild )+1);
}
return 0;
}

int BTree::getheight ()
{
return getheight(root);
}

int BTree::getheight (node *p)
{
if(NULL==p)
return 0;
int max;
max=getheight(p->lchild );
if(max<getheight(p->rchild ))
max=getheight(p->rchild );
return (max+1);
}

bool BTree::equl (BTree t2)
{
return equl(this->root ,t2.root);
}

bool BTree::equl (node *p1,node *p2)
{
if(p1!=p2)
return false;
else
{
if((NULL==p1) &&(NULL==p2))
return true;
if((NULL==p1) || (NULL==p2))
return false;
if(p1->data !=p2->data )
return false;
if(equl(p1->lchild ,p2->lchild )&&equl(p1->rchild ,p2->rchild ))
return true;
}
return false;
}

void main()
{
int a[]={1,2,4,23,54,234};
BTree tree(a,6);
tree.printbef();

}

myjie0527 2008-12-01
  • 打赏
  • 举报
回复
void BTree::printmid (node *p)
{
if(p!=NULL)
{
printmid(p->lchild);
cout<<p->data<<" ";
printmid(p->rchild);
}
}

void BTree::printlas (node *p)
{
if(p!=NULL)
{
printlas(p->lchild);
printlas(p->rchild);
cout<<p->data<<" ";
}
}
brookmill 2008-12-01
  • 打赏
  • 举报
回复
BTree::BTree(int val[],int l)
{
// int len=l;
len = l;
root=build(val,1);
}

65,211

社区成员

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

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