数据结构树的基本操作_树的各类基本操作(数据结构)
#include "iostream"
/*
輸入:ABC DE G F
ABC DE G F
*/
#include "bits/stdc++.h"
using namespace std;
typedef struct bitnode
{
char data;
bitnode *lchild,*rchild;
} *bintree;
bintree creatree(bintree &root)//先序創(chuàng)建樹
{
char a=getchar();
if(a==' ')
{
root=NULL;
return root;
}
else
{
root=(bintree)malloc(sizeof(bitnode));
root->data=a;
creatree(root->lchild);
creatree(root->rchild);
return root;
}
}
void PreOrderTraverse(bintree root)//InOrderTraverse中序 PostOrderTraverse 倒序
{
if(root==NULL)
{
return ;
}
else
{
PreOrderTraverse(root->lchild);
PreOrderTraverse(root->rchild);
cout<data<
}
}
int CountDegreeOne(bintree root)//度為一節(jié)點的個數(shù)
{
if(root==NULL)
return 0;
else
{
int a=CountDegreeOne(root->lchild);
int b=CountDegreeOne(root->rchild);
if(root->lchild&&!root->rchild||root->rchild&&!root->lchild)
return a+b+1;
else
return a+b;
}
}
int DeepTree(bintree root)//樹的深度
{
if(root==NULL)
return 0;
else
{
int a=DeepTree(root->lchild);
int b=DeepTree(root->rchild);
if(a>=b)
return a+1;
else
return b+1;
}
}
int same(bintree root,bintree root1)//判斷兩棵樹是否相同
{
if(root&&!root1||root1&&!root||!root&&!root1)
{
return 0;
}
else
{
if(root->data==root1->data)
{
int a=same(root->lchild,root1->lchild);
int b=same(root->rchild,root1->rchild);
if(a==b==1)
return 1;
}
else
return 0;
}
}
int main()
{
bintree root;
bintree root1;
creatree(root);
getchar();
creatree(root1);
PreOrderTraverse(root);
cout<
cout<
if(same(root,root1))
cout<
else
cout<
return 0;
}
總結(jié)
以上是生活随笔為你收集整理的数据结构树的基本操作_树的各类基本操作(数据结构)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: ps怎么做发光的灯泡(ps怎么做发光的灯
- 下一篇: 定位怎么居中(定位怎么居中打开)