数据结构第五章

数据结构第五章_第1张图片
数据结构第五章_第2张图片
数据结构第五章_第3张图片
数据结构第五章_第4张图片
数据结构第五章_第5张图片
数据结构第五章_第6张图片

双亲孩子表示法
孩子兄弟表示法
二叉树基本性质
二叉树层序操作

二叉树代码
#include
using namespace std;
template
struct binode
{
datatype data;
binode*lchild,*rchild;
};
template
class bitree
{
public:
bitree(){root=creat(root);}
~bitree(){release(root);}
void preorder(){preorder(root);}
void inorder(){inorder(root);}
void postorder(){postorder(root);}
int depth(){depth(root);}
private:
binode*root;
binode*creat(binode*bt);
void release(binode*bt);
void preorder(binode*bt);
void inorder(binode*bt);
void postorder(binode*bt);
int depth(binode*root);
};
int n=0,m=0;
template//前序遍历
void bitree::preorder(binode*bt)
{
if(btNULL) return;
else
{
n++;
cout if(bt->lchild
NULL&&bt->rchildNULL)
m++;
preorder(bt->lchild);
preorder(bt->rchild);
}
}
template//中序遍历
void bitree::inorder(binode*bt)
{
if(bt
NULL) return;
else
{
inorder(bt->lchild);
cout inorder(bt->rchild);
}
}
template//后序遍历
void bitree::postorder(binode*bt)
{
if(btNULL) return;
else
{
postorder(bt->lchild);
postorder(bt->rchild);
cout }
}
template//二叉链表算法
binode*bitree::creat(binode*bt)
{
datatype ch;
cin>>ch;
if(ch
’#’)bt=NULL;
else
{
bt=new binode;
bt->data=ch;
bt->lchild=creat(bt->lchild);
bt->rchild=creat(bt->rchild);
}
return bt;
}
template//释放二叉链表算法
void bitree::release(binode*bt)
{
if(bt!=NULL)
{
release(bt->lchild);
release(bt->rchild);
delete bt;
}
}
template//深度
int bitree::depth(binode*root)
{
if(rootNULL)
return 0;
else
{
int hl=depth(root->lchild);
int hr=depth(root->rchild);
if(hl>hr)
return (hl+1);
else
return (hr+1);
}
}
int main()
{
bitreesj;
sj.preorder();
cout< sj.inorder();
cout< sj.postorder();
cout< cout<<“节点个数为:”;
if(n
0)
cout<<“NULL”< else
cout< cout<<“叶子节点个数为:”;
if(m==0)
cout<<“NULL”< else
cout< cout<<“深度:”< return 0;
}

你可能感兴趣的:(数据结构第五章)