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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

二叉树的链式结构递归遍历实现

發布時間:2024/4/19 编程问答 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 二叉树的链式结构递归遍历实现 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

二叉樹的鏈式結構遞歸遍歷實現

1.???? BinTree.h文件

#ifndef__BINTREE_H__

#define__BINTREE_H__

#include<stdio.h>

#include<stdlib.h>

// typedef關鍵字是用來重命名數據類型的 這里重命名了數結點中有效數據的類型

typedef intBinTreeElemType;

// 定義了樹的結點類型??

typedef structBinTreeNode

{

?????? BinTreeElemType data;?????????????????? // 1. 結點中有效數據

?????? struct BinTreeNode *leftChild;?????? // 2. 結點的左孩子指針

?????? struct BinTreeNode *rightChild;???? // 3. 結點的右孩子指針

}BinTreeNode;

?

// 定義了樹的結構

typedef structBinTree

{

?????? struct BinTreeNode *root;????????????? // 1. 樹根的指針

?????? char refvalue;???????????????????????????????? // 2. 定義了結點為空的標志位

}BinTree;

?

// 函數的聲明

intinitBinTree(struct BinTree *);

intcreateBinTree(struct BinTree *);

?

intpreOrder(struct BinTree *tree);

int inOrder(structBinTree *);

intpostOrder(struct BinTree);

?

#endif

2.???? BinTree.c文件

#include"BinTree.h"

?

/*

?*? 函數名 : initBinTree

?*? 函數參數 : struct BinTree *tree(樹結構的地址)

?*? 函數的功能 :在我們創建一顆二叉樹的時候,對二叉樹進行初始化

?*???????????????? 1.將根結點的指針 指向為NULL 避免野指針

?*???????????????? 2.使用 '#' 作為空結點的標志

?*? 函數的返回值 : return 0

?*/

intinitBinTree(struct BinTree *tree)

{

?????? tree->root = NULL;

?????? tree->refvalue = '#';

??????

?????? return 0;

}

?

/*

?*? 函數名 :createBinTree

?*? 函數參數 :struct BinTree *tree(樹結構的地址)

?*? 函數的功能 :創建一顆二叉樹這個方法是給用戶調用的

?*? 函數的返回值 :return 0

?*

?*/

intcreateBinTree(struct BinTree *tree)

{

?????? __createBinTree(tree,&(tree->root));

??????

?????? return 0;

}

?

/*

?*? 函數名 :__createBinTree

?*? 函數參數 :參數1 struct BinTree *tree (樹的地址)

?*????????????參數2 struct BinTreeNode **t(二重指針 自己理解)

?*? 函數功能 : 這個方法是內部的方法 被createBinTree函數調用

?*? 函數返回值 : return 0

?*/

int__createBinTree(struct BinTree *tree, struct BinTreeNode **t)

{

?????? // 測試字符串 ABC##DE##F##G#H##

?????? char item;

?????? scanf("%c", &item);

??????

?????? if(item == tree->refvalue)

?????? {

????????????? *t = NULL;

?????? }

?????? else

?????? {

????????????? *t = (struct BinTreeNode*)malloc(sizeof(struct BinTreeNode));

????????????? (*t)->data = item;

????????????? (*t)->leftChild =(*t)->rightChild = NULL;

?????????????

????????????? __createBinTree(tree,&((*t)->leftChild));

????????????? __createBinTree(tree,&((*t)->rightChild));

?????? }

??????

?????? return 0;

}

?

/*

?*? 函數名 :preOrder

?*? 函數參數 :struct BinTree *tree (樹的地址)

?*? 函數功能 :遞歸前序遍歷二叉樹

?*? 函數返回值 :return 0

?*/

intpreOrder(struct BinTree *tree)

{

?????? printf("遞歸前序遍歷二叉樹 : ");

??????

?????? __preOrder(tree->root);

??????

?????? printf("\n");

?????? return 0;

}

?

/*

?*? 函數名 :__preOrder

?*? 函數參數 :struct BinTreeNode *t (樹中結點的指針)

?*? 函數功能 :該函數是內部函數被preOrder函數調用

?*? 函數返回值 :return 0

?*/

int__preOrder(struct BinTreeNode *t)

{

?????? if(t == NULL)

?????? {

?????? ?????? return0;

?????? }

?????? else

?????? {

????????????? printf("%c",t->data);

????????????? __preOrder(t->leftChild);

????????????? __preOrder(t->rightChild);

?????? }

??????

}

?

// 中序遍歷二叉樹

int inOrder(structBinTree *tree)

{

?????? printf("遞歸中序遍歷二叉樹 : ");

??????

?????? __inOrder(tree->root);

??????

?????? printf("\n");

?????? return 0;

}

?

?

int __inOrder(structBinTreeNode *t)

{

?????? if(t == NULL)

?????? {

????????????? return 0;

?????? }

?????? else

?????? {

????????????? __inOrder(t->leftChild);

????????????? printf("%c",t->data);

????????????? __inOrder(t->rightChild);

?????? }

??????

}

?

?

// 后序遍歷二叉樹

intpostOrder(struct BinTree *tree)

{

?????? printf("遞歸中序遍歷二叉樹 : ");

??????

?????? __postOrder(tree->root);

??????

?????? printf("\n");

?????? return 0;

}

?

?

int__postOrder(struct BinTreeNode *t)

{

?????? if(t == NULL)

?????? {

????????????? return 0;

?????? }

?????? else

?????? {

????????????? __postOrder(t->leftChild);

????????????? printf("%c",t->data);

????????????? __postOrder(t->rightChild);

?????? }

??????

}

3.???? main.c文件

#include"BinTree.h"

int main(int argc,char &argv)

{

struct BinTree tree;

// 初始化二叉樹

initBinTree(&tree);

// 創建二叉樹

createBinTree(&tree);

// 遞歸先序遍歷

preOrder(&tree);

// 遞歸中序遍歷

inOrder(&tree);

// 遞歸后序遍歷

postOrder(&tree);

return 0;

}

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

?

總結

以上是生活随笔為你收集整理的二叉树的链式结构递归遍历实现的全部內容,希望文章能夠幫你解決所遇到的問題。

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