日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 >

数据结构树的基本操作_树的各类基本操作(数据结构)

發布時間:2023/12/3 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 数据结构树的基本操作_树的各类基本操作(数据结构) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

#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)//先序創建樹

{

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)//度為一節點的個數

{

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;

}

總結

以上是生活随笔為你收集整理的数据结构树的基本操作_树的各类基本操作(数据结构)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。