65,210
社区成员
发帖
与我相关
我的任务
分享
#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();
}
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();
}