求二叉树的深度:
int BiTree::getDepth(BiNode *bt)
{
queue<BiNode*> h;
if(root!=NULL)
h.push(root);//将根指针放在队列里面
int layer=0;
while(!h.empty())
{
int len=h.size();//长度
while(len)
{
BiNode* node=h.front();
h.pop();
if(node->lchild)
h.push(node->lchild);
if(node->rchild)
h.push(node->rchild);
len--;
}
layer++;
}
return layer;
}
方法二:
int BiTree::getDepth(BiNode *bt)
{
/* 简单一点的方法
if(NULL)
return ;
else
{
left=getdepth(lchild);
right=getdepth(right);
int depth=max(left,right)+1;
return depth;
}
求二叉树的节点数
int BiTree::NodeCount(BiNode* bt)
{
if(bt==NULL)
{
return 0;
}
else
{
return NodeCount(bt->lchild)+NodeCount(bt->rchild)+1;
}
}
将一个二叉树存储到一维数组中
给定扩展二叉树的前序序列,构建二叉树(以二叉链表方式存储),然后将这个二叉链表存储为一维数组,其编号采用完全二叉树的编号模式。
完全二叉树的下标就是一维数组中元素的下标
//将二叉树存储到一维数组中
void BiTree::toArray(char biTreeArray[],int length,int *count)
{
int front=-1,rear=-1; //采用顺序队列,并假定不会溢出
int num; //用来控制二叉数组的下标
NodeNum Q[QS]; //定义队列
NodeNum nodenum; //临时变量
BiNode *p; //指向当前处理的树结点
//数组的初始化
for(int i=0; i<length; i++)
{
biTreeArray[i]=0;
}
if(root==NULL) //二叉树为空,算法结束
{
return;
}
nodenum.node=root;
nodenum.number=1; //根结点编号为1,存入数组时,需在编号基础上减1
Q[++rear]=nodenum;
//当队列非空时,进行层序遍历
while(front!=rear)
{
nodenum=Q[++front]; //出队
//当前结点值存入临时变量,并将出队的结点存入二叉树组
p=nodenum.node; //p指向当前结点
num=nodenum.number; //获取树结点编号
biTreeArray[num-1]=nodenum.node->data; //存入数组
//如果有左孩子,则左孩子入队
if(p->lchild!=NULL)
{
nodenum.node=p->lchild;
nodenum.number=2*num; //计算编号
Q[++rear]=nodenum; //左孩子入队
*count=nodenum.number; //count记录字符串最大的存储序号
}
//如果有右孩子,则右孩子入队
if(p->rchild!=NULL)
{
nodenum.node=p->rchild;
nodenum.number=2*num+1;//根结点为n=1,存入数组时,在编号基础上减1
Q[++rear]=nodenum; //右孩子入队
*count=nodenum.number; //count记录字符串最大的存储序号
}
}
}
//输出一维数组
void print(char BiTreeArray[],int length)
{
for(int i=0; i<length; i++)
{
if(BiTreeArray[i])
cout<<BiTreeArray[i];
else
cout<<"^";
}
}
方法二:(不要看这种方法,没想好,不会写,以后在改)
void BiTree::showarray(BiNode *root)
{
/*一种方法
if(!=NULL)
{
cout<<;
sum++;
if(sum==num)
break;
}
else
{
cout<<'^";
push(p=NULL);
push(p=NULL);
continue;
}
if(lchild!=NULL)
{
push(lchild)
}
else
{
push(p=NULL);
}
*/
/*另一种方法
linktoarray(char arr[],max,length)
{
队列入队时,入的是结构体(地址node和相应的编号)
数组初始化 0
临时变量.node=root;
a.number=1;
队列Q[++rear]=a.number;
arr[num-1]=num.node->data;
while(不为空)
{
a=q[front++];
pnode=a.node;用pnode指向a的地址
num=a.number;
存入数组:
arr[num-1]=pnode->data;
if(左孩子不为空)
{
nodenum.node=pnode->lchild;
nodenum.number=2*num;//右孩子为2*num+1;
q[++rear]=nodenum;
count=nodenum.number;//count为现在最大的存储序号
}
}
这种情况下加一个打印函数
如果结点data为0的话就打印^,
*/
}
写的稀烂,这个代码不对
class BiTree
{
private:
BiNode *root; //指向根结点的头指针
static int num=0;public:
BiTree()
{ num=0;
root = creat(root);//调函数构建二叉树
}
//前序构建二叉树
BiNode *BiTree::creat(BiNode *bt)
{
char ch;
cin>>ch;
if(ch=='#')
bt=NULL;
else
{
num++;
bt=new BiNode;
bt->data=ch;
bt->lchild=creat(bt->lchild);
bt->rchild=creat(bt->rchild);
}
return bt;
}//将二叉树存储到一维数组中
void BiTree::showarray(BiNode *bt)
{
stack<BiNode*> s;
s.push(bt);
int sum=0;
while(!s.empty())
{
BiNode *p=s.top();
s.pop();if(p!=NULL)
{
cout<<bt->data;
sum++;
if(sum==num)
break;
}else
{
cout<<"^";
BiNode *p=NULL;
s.push(p);
s.push(p);
continue;
}
if (bt->lchild != NULL)
{
s.push(bt->lchild);}
else
{
s.push(NULL);}
}}
int main()
{
BiTree T;
T.showarray(T.getRoot());
return 0;
}
主函数写法:
int main()
{
BiTree tree;
/* cout << "\n前序遍历二叉树"<<endl;
// tree.preOrder(tree.getRoot());
//
// cout << "\n中序遍历二叉树"<<endl;
// tree.inOrder(tree.getRoot());
// cout<<endl;
//
// cout << "\n后序遍历二叉树"<<endl;
// tree.postOrder(tree.getRoot());
//
//
// cout << "\n层序遍历二叉树"<<endl;
// tree.leverOrder(tree.getRoot());
//
// cout<<"\n二叉树的深度:\n";
// int len=tree.getDepth(tree.getRoot());
// cout<<len;
int node=tree.NodeCount(tree.getRoot());
//
*/ cout<<"二叉树的结点数是:"<<endl;
cout<<node<<endl;
tree.showarray(tree.getRoot());
// cout<<"\n删除的结果为:"<<endl;
return 0;
}