日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二叉树的深度优先搜索c语言,C语言 二叉树按层打印、深度优先遍历、二叉树是否对称...

發(fā)布時間:2025/4/16 编程问答 60 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉树的深度优先搜索c语言,C语言 二叉树按层打印、深度优先遍历、二叉树是否对称... 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

#include//二叉樹節(jié)點

typedef struct tree{

int data;

struct tree *pLeft;

struct tree *pRight;

}Tree;

//隊列

typedef struct queue{

struct node *pFront;

struct node *pBehind;

int size;

}Queue;

//棧

typedef struct stack{

struct node *pBottom;

struct node *pTop;

int size;

}Stack;

//普通節(jié)點

typedef struct node{

struct tree * data;

struct node * next;

}Node;

//生成二叉搜索樹

Tree * create(Tree * pRoot, int val){

if(pRoot == NULL){

pRoot = (Tree *)malloc(sizeof(Tree));

pRoot->data = val;

pRoot->pLeft = NULL;

pRoot->pRight = NULL;

}else{

if(val < pRoot->data){

pRoot->pLeft = create(pRoot->pLeft, val);

}else{

pRoot->pRight = create(pRoot->pRight, val);

}

}

return pRoot;

}

//循環(huán)遍歷生成二叉樹

Tree * createTree(Tree * pRoot){

for(int i=1; i<=15; i++){

int val = rand()%100+1000;

pRoot = create(pRoot, val);

}

return pRoot;

}

//中序遍歷二叉樹

void tarverse(Tree * pRoot){

if(pRoot != NULL){

tarverse(pRoot->pLeft);

printf("%d\n", pRoot->data);

tarverse(pRoot->pRight);

}

}

//前序遍歷二叉樹

void pretarverse(Tree * pRoot){

if(pRoot != NULL){

printf("%d\n", pRoot->data);

tarverse(pRoot->pLeft);

tarverse(pRoot->pRight);

}

}

//隊列初始化

Queue * init(){

Queue * pQueue = (Queue *)malloc(sizeof(Queue));

if(pQueue == NULL){

printf("init queue failed!\n");

exit(0);

}

pQueue->pFront = NULL;

pQueue->pBehind = NULL;

pQueue->size = 0;

return pQueue;

}

//判斷隊列是否為空

int empty(Queue * pQueue){

if(pQueue->size == 0){

return 1;

}

return 0;

}

//隊列中添加數(shù)據(jù)

void in(Queue * pQueue, Tree * pTree){

Node * pNode = (Node *)malloc(sizeof(Node));

if(pNode == NULL){

printf("queue in data failed\n");

exit(0);

}

pNode->data = pTree;

pNode->next = NULL;

if(empty(pQueue)){

pQueue->pFront = pNode;

}else{

pQueue->pBehind->next = pNode;

}

pQueue->pBehind = pNode;

pQueue->size += 1;

}

//隊列中移除并返回數(shù)據(jù)

Tree * out(Queue * pQueue){

if(empty(pQueue)){

printf("queue is empty\n");

exit(0);

}

Node * pNode = pQueue->pFront;

pQueue->pFront = pNode->next;

pQueue->size -= 1;

Tree * pTree = pNode->data;

delete pNode;

pNode = NULL;

return pTree;

}

Tree * pop(Queue * pQueue){

if(empty(pQueue)){

printf("queue is empty!\n");

exit(0);

}

Tree * pNode = pQueue->pBehind->data;

return pNode;

}

void qtarverse(Queue * pQueue){

if(empty(pQueue)){

printf("queue is empty!\n");

exit(0);

}

Node * pNode = pQueue->pFront;

while(pNode != NULL){

printf("%d\n", pNode->data);

pNode = pNode->next;

}

}

//二叉樹按層排印

void printLevel(Tree * pRoot){

if(pRoot == NULL){

printf("binary tree is empty\n");

exit(0);

}

Tree * pTree;

Queue * pQueue = init();

in(pQueue, pRoot);

while(!empty(pQueue)){

pTree = out(pQueue);

printf("%d\n", pTree->data);

if(pTree->pLeft != NULL){

in(pQueue, pTree->pLeft);

}

if(pTree->pRight != NULL){

in(pQueue, pTree->pRight);

}

}

}

Stack * stackInit(){

Stack * pStack = (Stack *)malloc(sizeof(Stack));

if(pStack == NULL){

printf("init stack is failed!\n");

exit(0);

}

pStack->pBottom = NULL;

pStack->pTop = NULL;

pStack->size = 0;

return pStack;

}

int stackEmpty(Stack * pStack){

if(pStack->size == 0){

return 1;

}

return 0;

}

void stackIn(Stack * pStack, Tree * pTree){

Node * pNode = (Node *)malloc(sizeof(Node));

if(pNode == NULL){

printf("stack in number failed!\n");

exit(0);

}

pNode->data = pTree;

if(stackEmpty(pStack)){

pNode->next = NULL;

pStack->pBottom = pNode;

pStack->pTop = pNode;

}else{

pNode->next = pStack->pTop;

pStack->pTop = pNode;

}

pStack->size += 1;

}

Tree * stackOut(Stack * pStack){

if(stackEmpty(pStack)){

printf("stack is empty\n");

exit(0);

}

Node * pNode = pStack->pTop;

Tree * pTree = pNode->data;

pStack->pTop = pNode->next;

pStack->size -= 1;

delete pNode;

pNode = NULL;

return pTree;

}

//二叉樹深度優(yōu)先遍歷

void dfs(Tree * pRoot){

if(pRoot == NULL){

printf("binary tree is empty!\n");

exit(0);

}

Stack * pStack = stackInit();

stackIn(pStack, pRoot);

while(!stackEmpty(pStack)){

Tree * pTree = stackOut(pStack);

printf("%d\n", pTree->data);

if(pTree->pRight != NULL){

stackIn(pStack, pTree->pRight);

}

if(pTree->pLeft != NULL){

stackIn(pStack, pTree->pLeft);

}

}

}

//判斷二叉樹是否對稱

int judgeTreeIsSym(Tree * pRoot01, Tree * pRoot02){

if(pRoot01 == NULL && pRoot02 == NULL){

return 1;

}

if(pRoot01 == NULL || pRoot02 == NULL){

return 0;

}

if(judgeTreeIsSym(pRoot01->pLeft, pRoot02->pRight) && judgeTreeIsSym(pRoot01->pRight, pRoot02->pLeft)){

return 1;

}

return 0;

}

int main(){

Tree * pRoot = NULL;

pRoot = createTree(pRoot);

tarverse(pRoot);

printf("\n\n");

printLevel(pRoot);

printf("\n\n");

dfs(pRoot);

return 0;

}

總結(jié)

以上是生活随笔為你收集整理的二叉树的深度优先搜索c语言,C语言 二叉树按层打印、深度优先遍历、二叉树是否对称...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。